
等级: 新手上路
- 注册:
- 2021-9-1
- 曾用名:
|
from vnpy_ctastrategy import ( CtaTemplate, StopOrder, TickData, BarData, TradeData, OrderData, BarGenerator, ArrayManager,)
from vnpy.trader.constant import Intervalclass TrendStrategy (CtaTemplate):
author = "" am_daily = None ema10 = 0.0 is_bull = None is_bear = None today_high = None today_low = None last_bar:BarData = None daily_first_bar = None last_open_interest = 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,interval=Interval.MINUTE) 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_daily.update_tick(tick) self.bg.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ if self.last_bar is None: # 第一次运行 self.daily_first_bar = bar self.today_low = bar.low_price self.today_high = bar.high_price self.last_open_interest = bar.open_interest self.last_bar = bar return elif bar.datetime.timestamp() - self.last_bar.datetime.timestamp() > 150: # 当日第一条分钟线 # 处理前一日数据 self.today_high = max(self.today_high, bar.high_price) self.today_low = min(self.today_low, bar.low_price) self.last_open_interest = bar.open_interest last_daily_bar = BarData(self.daily_first_bar.gateway_name, self.daily_first_bar.symbol, self.daily_first_bar.exchange, bar.datetime, Interval.DAILY, 1.0, 1.0, self.daily_first_bar.open_interest, self.daily_first_bar.open_price, self.today_high,
self.today_low, bar.close_price,) self.am.update_bar(last_daily_bar) self.ema10 = self.am.ema(10, False) if bar.close_price > self.ema10: # 趋势处于多头 self.is_bull = True self.is_bear = False elif bar.close_price < self.ema10: # 趋势处于空头 self.is_bull = False self.is_bear = True else: self.is_bull = False self.is_bear = False # 更新当日数据 self.daily_first_bar = bar self.last_bar = bar return elif bar.datetime.timestamp() - self.daily_first_bar.datetime.timestamp() < 20: # 每日前20分钟不交易 self.last_bar = bar return else: self.last_bar = bar num_matched_buy = 0 num_matched_sell = 0 is_buy = False is_sell = False if self.is_bull: # 多头趋势 num_matched_buy = num_matched_buy + 1 if bar.close_price > self.daily_first_bar.open_price: # 当前价高于开盘价 num_matched_buy = num_matched_buy + 1 if bar.open_interest > self.last_open_interest: num_matched_buy = num_matched_buy + 1 if num_matched_buy >= 3: is_buy = True is_sell = False elif self.is_bear: num_matched_sell = num_matched_sell + 1 if bar.close_price < self.daily_first_bar.open_price: # 当前价高于开盘价 num_matched_sell = num_matched_sell + 1 if bar.open_interest < self.last_open_interest: num_matched_sell = num_matched_sell + 1 if num_matched_sell >= 3: is_buy = False is_sell = True else: is_buy = False is_sell = False if self.pos == 0: # 只有未持仓时才开仓 if is_buy: self.buy(bar.close_price, 1) elif is_sell: self.short(bar.close_price, 1) elif self.pos > 0: # 判断是否平仓 if num_matched_buy < 3: self.sell(bar.close_price, abs(self.pos)) elif self.pos < 0: # 判断是否平仓 if num_matched_sell < 3: self.cover(bar.close_price, abs(self.pos)) 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
|
|