Skip to content
Discussion options

You must be logged in to vote

The error is expected when stop_loss = 0 with this expression:

self.buy(sl=(1 - self.stop_loss / 100) * price)

If stop_loss is zero, then sl == price. For a long order, Backtesting.py requires the stop-loss to be below the entry/limit price, not equal to it. So the validation fails because the order is effectively saying "enter long and stop out at the same price".

If you do not want to use a stop-loss, pass sl=None instead of calculating a zero-distance stop:

price = self.data.Close[-1]

sl = None if self.stop_loss == 0 else (1 - self.stop_loss / 100) * price

self.buy(sl=sl)

A couple of small code notes:

  • Use self.data.Close[-1] for the current bar's close, not self.data.Close, which is…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kernc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants