You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
backtesting.lib combing barssince and cross? #1259
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.
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()
Activity