Skip to content

backtesting.lib combing barssince and cross? #1259

Open

Description

@kbs-code

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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