|
Hello, I have following next() in my strategy - I am running it with - with stop_loss = 0, it throws following error - What am I doing wrong? Thank you. |
Replies: 1 comment
|
The error is expected when self.buy(sl=(1 - self.stop_loss / 100) * price)If If you do not want to use a stop-loss, pass 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:
So the short version is: |
The error is expected when
stop_loss = 0with this expression:If
stop_lossis zero, thensl == 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=Noneinstead of calculating a zero-distance stop:A couple of small code notes:
self.data.Close[-1]for the current bar's close, notself.data.Close, which is…