def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
am = self.am
am.update_bar(bar)
if not am.inited:
return
close = self.am.close_array
fast_macd,slow_macd,macd = talib.MACD(close,self.fast_window,
self.slow_window,
self.siganl_window)
self.fast_macd0 = fast_macd[-1]
self.fast_macd1 = fast_macd[-2]
self.slow_macd0 = slow_macd[-1]
self.slow_macd1 = slow_macd[-2]
cross_over = self.fast_macd0 > self.slow_macd0 and self.fast_macd1 < self.slow_macd1
cross_below = self.fast_macd0 < self.slow_macd0 and self.fast_macd1 > self.slow_macd1
if self.pos == 0:
if cross_over:
self.buy(bar.close_price, self.fixed_size, True)
self.long_stop = bar.close_price - self.stoploss
elif cross_below:
self.short(bar.close_price, self.fixed_size, True)
self.short_stop = bar.close_price + self.stoploss
self.short_protfit = bar.close_price - self.startstop
elif self.pos > 0:
if bar.low_price < self.long_stop:
self.sell(self.long_stop, abs(self.pos), True)
elif cross_below:
self.sell(bar.close_price, abs(self.pos), True)
elif self.pos < 0:
if bar.high_price > self.short_stop:
self.cover(self.short_stop, abs(self.pos), True)
elif bar.low_price < self.short_protfit:
self.cover(self.short_protfit, abs(self.pos), True)
elif cross_over:
self.cover(bar.close_price, abs(self.pos), True)
self.put_event()
数据端口Wind,MCAD金叉做多死叉做空,设置止损止盈,总是迸发成交,如何处理