1.逛社区学习了大佬的合成日线的方法,但跑进程序以后打印数据仍为一小时为数据周期间隔打印
2.数据获取没问题但成交记录为空,是什么原因
3.源代码如下:
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
from vnpy.trader.constant import Interval
class Double111(CtaTemplate):
fast_window=30
# 快速均线
slow_window=60
# 慢速均线
x_min=30 #交易周期
lots=1 #开仓手数
save=20#止损参数
startstop=100#开始止盈
stoploss=20#开始止损
daily_window=0#日线参数
#变量
fast_ma=0
fast_ma_pre = 0
slow_ma = 0
slow_ma_pre = 0
price=0 #tick的实时价格
bartime="" #时间的显示
highest=0
lowest=0
avg_buy_price=0
avg_sell_price=0
#固定止损 移动止盈
daily_ma=0
parameters = ["fast_window","slow_window","x_min","lots" ]
variables = ["fast_ma","fast_ma_pre","slow_ma","slow_ma_pre","price","bartime"
]
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()
self.bg_daily = BarGenerator(self.on_bar,1, self.on_daily_bar, Interval.DAILY)
self.am_daily = ArrayManager(size=20)
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(30)
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):
"""
Callback of new tick data update.
"""
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)
self.bg_daily.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:
if bar.close_price>self.daily_ma:
self.buy(bar.close_price,self.lots)
self.highest = bar.close_price
self.avg_buy_price= bar.close_price
print(bar.close_price,bar.datetime,"开单")
#死叉开空
elif self.fast_ma_pre > self.slow_ma_pre and self.fast_ma < self.slow_ma:
if bar.close_price<self.daily_ma:
self.short(bar.close_price, self.lots)
self.lowest = bar.close_price
self.avg_sell_price = bar.close_price
print(bar.close_price, bar.datetime, "空单")
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)
print(bar.close_price, bar.datetime, "多单止损")
# 多单止盈
elif self.highest>self.avg_buy_price+self.startstop:
if bar.close_price<self.highest-self.stoploss:
self.sell(bar.close_price,self.lots)
print(bar.close_price, bar.datetime, "多单止盈")
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)
print(bar.close_price, bar.datetime, "空单止损")
#空单止盈
elif self.lowest<self.avg_sell_price-self.startstop:
if bar.close_price>self.lowest+self.stoploss:
self.cover(bar.close_price,self.lots)
print(bar.close_price, bar.datetime, "空单止盈")
self.put_event()
def on_daily_bar(self, bar: BarData):
self.cancel_all()
am=self.am_daily
am.update_bar(bar)
if not am.inited:
return
self.daily_ma=am.close_array[-self.daily_window:-1].mean()
print(bar.close_price,bar.datetime)
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
4.对应bg文件utility合成日线改写部分:
if self.interval == Interval.MINUTE:
if not (bar.datetime.minute + 1) % self.window:
finished = True
elif self.last_bar and str(bar.datetime)[-8:] == '10:14:00':
finished = True
self.interval_count = 0
elif self.interval == Interval.HOUR:
if self.last_bar and bar.datetime.hour != self.last_bar.datetime.hour:
if self.window == 1:
finished =True
else:
self.interval_count +=1
if not self.interval_count%self.window:
finished=True
self.interval_count = 0
elif self.interval == Interval.DAILY:
if self.last_bar and str(bar.datetime)[-14:-6]=="14:59:00":
if self.window == 1:
finished = True
self.interval_count=0
self.on_window_bar(self.window_bar)
self.window_bar = None