大神们请教下,我在测试简单的双均线时发现经常出现单边连续信号,哪位大神给有空帮忙看下只是什么问题导致的呢
源码如下:
`
class EmaStrategy(CtaTemplate):
""""""
fast_window = 30
slow_window = 150
fast_ema0 = 0.0
fast_ema1 = 0.0
slow_ema0 = 0.0
slow_ema1 = 0.0
parameters = ["fast_window", "slow_window"]
variables = ["fast_ema0", "fast_ema1", "slow_ema0", "slow_ema1"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super(EmaStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bg = BarGenerator(
self.on_bar,
window=3,
on_window_bar=self.on_3m_bar
)
self.am = ArrayManager(self.slow_window + 1)
def on_init(self):
"""策略初始化"""
self.load_bar(10) # use_database=True
def on_start(self):
"""策略开始"""
def on_stop(self):
"""停止策略"""
def on_tick(self, tick: TickData):
""""""
self.bg.update_tick(tick)
def on_bar(self, bar: BarData):
"""推送1m bar 合成策略所需的时间颗粒的bar"""
self.bg.update_bar(bar)
def on_3m_bar(self, bar: BarData):
""""""
am = self.am
am.update_bar(bar)
if not am.inited:
return
# 计算指标
fast_ema = am.ema(self.fast_window, True)
self.fast_ema0 = fast_ema[-1]
self.fast_ema1 = fast_ema[-2]
slow_ema = am.ema(self.slow_window, True)
self.slow_ema0 = slow_ema[-1]
self.slow_ema1 = slow_ema[-2]
# 信号产生条件
cross_upward = self.fast_ema0 > self.slow_ema0 and self.fast_ema1 < self.slow_ema1
cross_down = self.fast_ema0 < self.slow_ema0 and self.fast_ema1 > self.slow_ema1
if cross_upward:
print(bar.datetime, "金叉")
elif cross_down:
print(bar.datetime, "死叉")
`
print结果: