我想建一个,今日开盘价+周期内最大ATR的策略
on_bar这么写,报错最小价格变动不能为0
还有其他获取今日开盘价的写法吗
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.cancel_all()
am = self.am
am.update_bar(bar)
self.bars.append(bar)
if not am.inited or len(self.bars) <= 2:
return
else:
self.bars.pop(0)
last_bar = self.bars[-2]
atr_array = am.atr(self.atr_length, array=True)
self.atr_max=max(atr_array[-self.atr_length:])
if last_bar.datetime.date() != bar.datetime.date(): #开盘
self.open_price=bar.open_price
if self.pos == 0:
self.long_entry = self.open_price + self.k1 * self.atr_max
self.short_entry = self.open_price - self.k1 * self.atr_max
self.buy(self.long_entry, self.fixed_size, True)
self.short(self.short_entry, self.fixed_size, True)
elif self.pos > 0:
self.long_stop = self.open_price - self.k2 * self.atr_max
self.sell(self.long_stop, abs(self.pos), True)
elif self.pos < 0:
self.short_stop = self.open_price + self.k2 * self.atr_max
self.cover(self.short_stop, abs(self.pos), True)
self.put_event()