Skip to content

Commit

Permalink
BUG: Fix computing indicator start offset for 2d indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 23, 2020
1 parent f54fd76 commit 1715c91
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ def run(self, **kwargs) -> pd.Series:

# Skip first few candles where indicators are still "warming up"
# +1 to have at least two entries available
start = 1 + max((np.isnan(indicator.astype(float)).argmin()
start = 1 + max((np.isnan(indicator.astype(float)).argmin(axis=-1).max()
for _, indicator in indicator_attrs), default=0)

# Disable "invalid value encountered in ..." warnings. Comparison
Expand Down

3 comments on commit 1715c91

@jamhot1
Copy link

@jamhot1 jamhot1 commented on 1715c91 Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Have a question about a problem that may be relating to this.
I wanted to pre calc TP and SL order levels:
ex.
def init(self):
#some indicators....
self.level_A = self.data.Low - self.ATR2 # pre calc's level for SL
self.level_B = self.data.High + self.ATR
2 # pre calc's level for SL

def next(self):
if not self.position
# entry logic bla bla
self.buy(sl = self.level_A, tp= self.level_B)

I get the following error
AssertionError: For long orders should be: SL (108.79414877189056) < BUY PRICE (110.525) < TP (109.16585122810945)

yet if I manually add the SL and TP level formula I get no issues. Seems it is calling incorrect values from the array?

@kernc
Copy link
Owner Author

@kernc kernc commented on 1715c91 Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamhot1 Since you're not passing an explicit price to self.buy(), it uses next bar's Open price, which happens to be above your TP. So upon long order filling, TP price (109) is lower than BUY PRICE (110), but for long orders it should be TP > BUY PRICE.

Please consider testing/using #47! It should fix a lot of issues an be somewhat of an improvement still. 😅

@jamhot1
Copy link

@jamhot1 jamhot1 commented on 1715c91 Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Still trying to get my head around the mechanics of it. much appreciate the pointers

Please sign in to comment.