运行一个固定止损、移止盈的双均线策略,在回测加载完数据后突然就终止了。 出错显示如下:
file " vnpy\app\cta_strategy\backtesting.py", line 286, in run_backtesting
self.callback(data)
TypeError: 'NoneType' object is not callable
不知为何会出错至此,完整策略的代码为:
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
from vnpy.trader.constant import Interval
class Demo02(CtaTemplate):
fast_window = 30
slow_window = 60
x_min = 15
lots = 1
save = 20
startstop = 100
stoploss = 40
# 变量
fast_ma = 0
fast_ma_pre = 0
slow_ma = 0
slow_ma_pre = 0
price = 0
bartime =""
avg_buy_price = 0
avg_sell_price = 0
highest = 0
lowest = 0
parameters = ["fast_window", "slow_window", "x_min", "lots", "stoploss", "startstop", "save"]
variables = ["fast_ma", "fast_ma_pre", "slow_ma", "slow_ma_pre", "highest", "lowest","bartime", "price"]
def __init__(self, cta_engine, strategy_name ,vt_symbol, setting):
super().__init__(cta_engine, strategy_name,vt_symbol,setting)
self.bg_x = BarGenerator(self.on_bar, self.x_min, self.on_x_bar, Interval.MINUTE)
self.am_x = ArrayManager()
def on_int(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(10)
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
def on_tick(self, tick: TickData):
self.bg_x.update_tick(tick)
self.price = tick.last_price
self.put_event()
def on_bar(self, bar:BarData):
self.bg_x.update_bar(bar)
def on_x_bar(self, bar:BarData):
self.cancel_all()
am =self.am_x
am.update_bar(bar)
if not am.inited:
return
## ma30 的计算
self.fast_ma = am.close_array[- self.fast_window:-1].mean()
# 之前的ma30的计算
self.fast_ma_pre = am.close_array[- self.fast_window-1 :-2].mean()
# ma60的计算
self.slow_ma = am.close_array[-self.slow_window:-1].mean()
# 之前ma60的计算
self.slow_ma_pre = am.close_array[-self.slow_window-1:-2].mean()
if self.pos ==0:
# 金叉开多
if self.fast_ma_pre < self.slow_ma_pre and self.fast_ma > self.slow_ma:
self.buy(bar.close_price, self.lots)
self.highest = bar.close_price
self.avg_buy_price = bar.close_price
# 死叉开空
elif self.fast_ma_pre > self.slow_ma_pre and self.fast_ma < self.slow_ma:
self.short(bar.close_price, self.lots)
self.lowest = bar.close_price
self.avg_sell_price = bar.close_price
elif self.pos >0:
# 持有多单的最高价记录
self.highest = max(bar.high_price, self.highest)
# 多单止损的逻辑
if bar.close_price < self.avg_buy_price - self.save:
self.sell(bar.close_price, self.lots)
# 多单止盈的逻辑
elif self.highest > self.avg_buy_price + self.startstop:
if bar.close_price < self.highest - self.stoploss:
self.sell(bar.close_price, self.lots)
elif self.pos < 0:
# 持有 空单的最低价记录
self.lowest = min(bar.low_price, self.lowest)
## 空单止损的逻辑
if bar.close_price > self.avg_sell_price + self.save:
self.cover(bar.close_price, self.lots)
#空单止盈
elif self.lowest < self.avg_sell_price - self.startstop:
if bar.close_price < self.lowest + self.stoploss:
self.cover(bar.close_price, self.lots)
self.put_event()
def on_order(self, order: OrderData):
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