For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
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[0] > self.volume[-1] and self.volume[-1] > self.volume[-2]: self.log('SELL CREATED, Close: %.2f, Two consecutive day volume UP, ' % self.dataclose[0]) self.order = self.sell() else: valid = self.data.datetime.date(0) + datetime.timedelta(days=1) #self.log('close: %.2f' % self.dataclose[0]) price1 = self.dataclose[0] * 1.005 if (self.datas[0].high[0] > 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[0], self.datas[0].high[0], 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.