最近发现CTA策略的回测带的策略是即做多有做空的,自己写了个单均线的策略但是并不生效,具体代码如下:
from vnpy_ctastrategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
class SingleMaStrategy(CtaTemplate):
author = " "
ma_window = 20
ma0 = 0.0
ma1 = 0.0
close0 = 0.0
close1 = 0.0
parameters = ["ma_window"]
variables = ["ma0", "ma1", "close0", "close1"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bg = BarGenerator(self.on_bar)
self.am = ArrayManager()
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(20)
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")
self.put_event()
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
self.put_event()
def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg.update_tick(tick)
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
am = self.am
am.update_bar(bar)
if not am.inited:
return
ma = am.sma(self.ma_window, array=True)
self.ma0 = ma[-1]
self.ma1 = ma[-2]
close = self.am.close_array
self.close0 = close[-1]
self.close1 = close[-2]
cross_over = self.close0 > self.ma0 and self.close1 < self.ma1
cross_below = self.close0 < self.ma0 and self.close1 > self.ma1
if cross_over:
if self.pos == 0:
self.buy(bar.close_price, 1)
elif cross_below:
if self.pos > 0:
self.sell(bar.close_price, 1)
self.put_event()
def on_order(self, order: OrderData):
"""
Callback of new order data update.
"""
pass
def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
self.put_event()
def on_stop_order(self, stop_order: StopOrder):
"""
Callback of stop order update.
"""
pass
这样写没办法执行不知道为什么,请各位大神指教一下谢谢