kernc / kernc/backtesting.py

backtesting.lib combing barssince and cross?

Open
#1,259 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in backtesting.lib with the barssince and cross entry points, then reproduce the reported numpy.bool_ TypeError using the examples in the issue. Check existing tests or documentation for these helpers and define a testable outcome for combining them. Done means the supported behavior is covered and the usage is no longer ambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.