stop order issue with 1 or 2 days valid no working
I met a problem when I code the stoploss. my code looks like:
def next(self) ... ... if self.params.modelno == 3: if not self.position: if self.volume > self.volume[-1] and self.volume[-1] > self.volume[-2]: self.log('SELL CREATED, Close: %.2f, Two consecutive day volume UP, ' % self.dataclose) self.order = self.sell() else: valid = self.data.datetime.date(0) + datetime.timedelta(days=1) #self.log('close: %.2f' % self.dataclose) price1 = self.dataclose * 1.005 if (self.datas.high > price1): print("**************************** this should be a stop price") if price1 > 0.0 : self.order = self.buy(size=abs(self.position.size), price=price1 , exectype=bt.Order.Stop, valid=valid) self.log('stop price: %.2f, Valid: %s, close= %.2f, high=%.2f, close-1=%.2f' % (price1, valid, self.dataclose, self.datas.high, self.dataclose[-1]))
The logic is defined as follow the above 0.5% of the latest close price. And the stop order is valid for just one day long, but the execution result is that the stop order is never executed even the price high has touched the stop level.
The execution statistics is like:
When I set the valid timedelta to 0 or 1,
valid = self.data.datetime.date(0) + datetime.timedelta(days=0)there is no stop hit.
But if I change the timedelta to days=2,
valid = self.data.datetime.date(0) + datetime.timedelta(days=0)it seems working alright.
What's the problem please?
It doesn't seem to be a problem. Your timeframe is daily and the resolution plays an important role:
timedelta(0)- You are basically expiring the order on creation. No surprise.
timedelta(1)- You ask for the order to expire on the next day. The next day is actually the first chance for the order to be evaluated. The broker tries to evaluate and sees you asked for the order to be expired.
@backtrader Thank you for this answer as I was having a similar problem. So if I want an order to be valid for the next 1-minute bar, I should use
It would be great if the docs could note this issue (e.g., in https://www.backtrader.com/docu/order/). Is there a way for me to submit a pull request to help improve the docs? I didn't see any docs on github at https://github.com/mementum/backtrader so I am asking here.