Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backtesting.lib combing barssince and cross? #1259

Open
kbs-code opened this issue Apr 1, 2025 · 0 comments
Open

backtesting.lib combing barssince and cross? #1259

kbs-code opened this issue Apr 1, 2025 · 0 comments

Comments

@kbs-code
Copy link

kbs-code commented Apr 1, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant