from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager
)
class Demo02(CtaTemplate):
author = ""
止盈点 = 3
止损点 = 3
parameters = ["止盈点", "止损点"]
variables = []
# 持仓成本价
trade_price = 0
# 计划止盈平仓价格
plan_win_price = 0
# 计划止损平仓价格
plan_lost_price = 0
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):
self.load_tick(1)
def on_start(self):
print("策略开始")
def on_stop(self):
print("策略结束")
def on_tick(self, tick: TickData):
if self.pos == 0:
if tick.datetime.hour > 14 and tick.datetime.minute > 40:
return
# 如果卖1价为整数压力位,则用xxx9价格建仓做空
if (tick.ask_price_1%10 == 0) and (tick.ask_price_1-tick.bid_price_1)==1:
self.short(tick.bid_price_1, 1)
self.plan_win_price = tick.bid_price_1 - self.止盈点
self.plan_lost_price = tick.bid_price_1 + self.止损点
self.trade_price = tick.bid_price_1
elif self.pos < 0:
# 如果到收盘时间或者至止盈止损价,平仓空单
if (tick.datetime.hour == 14 and tick.datetime.minute > 58) or (tick.ask_price_1 >= self.plan_lost_price) or (tick.ask_price_1 <= self.plan_win_price):
self.cover(tick.ask_price_1, 1)
self.put_event()
def on_bar(self, bar: BarData):
self.put_event()
def on_trade(self, trade: TradeData):
self.put_event()
def on_order(self, order: OrderData):
self.put_event()
def on_stop_order(self, stop_order: StopOrder):
self.put_event()
就这么简单的一个tick级别的策略,就是逢卖1价整数,做空,结果无论怎么测试,都是很大的盈利,数据乱的。