Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

backtesting.lib combing barssince and cross? #1259

Open
@kbs-code

Description

Currently in the documentation, it says barssince can be used like this:

def barssince
(
condition, default=inf)
Return the number of bars since condition sequence was last True, or if never, return default.
>>> barssince(self.data.Close > self.data.Open)
3

I have tried to use 2 versions of this
One:

if barrsince(self.data.EMA_9 > self.data.EMA_50) > 1

This works but the algo will re-enter a long position even if the bars crossed many bars ago which is something I don't want.

if barrsince(self.data.EMA_9 > self.data.EMA_50) < 5 

Something I'm trying to mitigate the issue from the first example.

barssince(cross(self.data.EMA_9 > self.data.EMA_50)) > 1

This code doesn't work because I get the following error:

barssince(condition, default)
def barssince(condition: Sequence[bool], default=np.inf) -> int:
 """
 Return the number of bars since `condition` sequence was last `True`,
 or if never, return `default`.
 (...)
 3
 [ """
---> return next(compress(range(len(condition)), reversed(condition)), default)
TypeError: object of type 'numpy.bool_' has no len()

Is it possible to combine barrsince with cross?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /