def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(TurtleSignalStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg = BarGenerator(self.on_bar) self.am = ArrayManager()
def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(KingKeltnerStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg = BarGenerator(self.on_bar, 5, self.on_5min_bar) self.am = ArrayManager()
def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(BollChannelStrategy, self).__init__( cta_engine, strategy_name, vt_symbol, setting ) self.bg = BarGenerator(self.on_bar, 15, self.on_15min_bar) self.am = ArrayManager()
def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(DualThrustStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg = BarGenerator(self.on_bar) self.am = ArrayManager() self.bars = []
def __init__(self, cta_engine, strategy_name, vt_symbol, setting): super(wma_tp_01, self).__init__(cta_engine, strategy_name, vt_symbol, setting) #===k-bar合成管理功能函数 self.bg = BarGenerator(self.on_bar, self.zhouqi, self.on_3min_bar) #===运行态,保存3min_bar功能函数 self.am = ArrayManager(size=150)
class BollChannelStrategy(CtaTemplate): """""" author = "用Python的交易员" boll_window = 18 boll_dev = 3.4 cci_window = 10 atr_window = 30 sl_multiplier = 5.2 fixed_size = 1 boll_up = 0 boll_down = 0 cci_value = 0 atr_value = 0 intra_trade_high = 0 intra_trade_low = 0 long_stop = 0 short_stop = 0 parameters = ["boll_window", "boll_dev", "cci_window", "atr_window", "sl_multiplier", "fixed_size"] variables = ["boll_up", "boll_down", "cci_value", "atr_value", "intra_trade_high", "intra_trade_low", "long_stop", "short_stop"] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(BollChannelStrategy, self).__init__( cta_engine, strategy_name, vt_symbol, setting ) self.bg = BarGenerator(self.on_bar, 15, self.on_15min_bar) self.am = ArrayManager() def on_init(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): """ Callback of new tick data update. """ self.bg.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg.update_bar(bar) def on_15min_bar(self, bar: BarData): """""" self.cancel_all() am = self.am am.update_bar(bar) if not am.inited: return self.boll_up, self.boll_down = am.boll(self.boll_window, self.boll_dev) self.cci_value = am.cci(self.cci_window) self.atr_value = am.atr(self.atr_window) if self.pos == 0: self.intra_trade_high = bar.high_price self.intra_trade_low = bar.low_price if self.cci_value > 0: self.buy(self.boll_up, self.fixed_size, True) elif self.cci_value < 0: self.short(self.boll_down, self.fixed_size, True) elif self.pos > 0: self.intra_trade_high = max(self.intra_trade_high, bar.high_price) self.intra_trade_low = bar.low_price self.long_stop = self.intra_trade_high - self.atr_value * self.sl_multiplier self.sell(self.long_stop, abs(self.pos), True) elif self.pos < 0: self.intra_trade_high = bar.high_price self.intra_trade_low = min(self.intra_trade_low, bar.low_price) self.short_stop = self.intra_trade_low + self.atr_value * self.sl_multiplier self.cover(self.short_stop, abs(self.pos), True) 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
class TurtleSignalStrategy(CtaTemplate): """""" author = "用Python的交易员" entry_window = 20 exit_window = 10 atr_window = 20 fixed_size = 1 entry_up = 0 entry_down = 0 exit_up = 0 exit_down = 0 atr_value = 0 long_entry = 0 short_entry = 0 long_stop = 0 short_stop = 0 parameters = ["entry_window", "exit_window", "atr_window", "fixed_size"] variables = ["entry_up", "entry_down", "exit_up", "exit_down", "atr_value"] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(TurtleSignalStrategy, self).__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("策略启动") 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.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.cancel_all() self.am.update_bar(bar) if not self.am.inited: return self.entry_up, self.entry_down = self.am.donchian(self.entry_window) self.exit_up, self.exit_down = self.am.donchian(self.exit_window) if not self.pos: self.atr_value = self.am.atr(self.atr_window) self.long_entry = 0 self.short_entry = 0 self.long_stop = 0 self.short_stop = 0 self.send_buy_orders(self.entry_up) self.send_short_orders(self.entry_down) elif self.pos > 0: self.send_buy_orders(self.long_entry) sell_price = max(self.long_stop, self.exit_down) self.sell(sell_price, abs(self.pos), True) elif self.pos < 0: self.send_short_orders(self.short_entry) cover_price = min(self.short_stop, self.exit_up) self.cover(cover_price, abs(self.pos), True) self.put_event() def on_trade(self, trade: TradeData): """ Callback of new trade data update. """ if trade.direction == Direction.LONG: self.long_entry = trade.price self.long_stop = self.long_entry - 2 * self.atr_value else: self.short_entry = trade.price self.short_stop = self.short_entry + 2 * self.atr_value def on_order(self, order: OrderData): """ Callback of new order data update. """ pass def on_stop_order(self, stop_order: StopOrder): """ Callback of stop order update. """ pass def send_buy_orders(self, price): """""" t = self.pos / self.fixed_size if t < 1: self.buy(price, self.fixed_size, True) if t < 2: self.buy(price + self.atr_value * 0.5, self.fixed_size, True) if t < 3: self.buy(price + self.atr_value, self.fixed_size, True) if t < 4: self.buy(price + self.atr_value * 1.5, self.fixed_size, True) def send_short_orders(self, price): """""" t = self.pos / self.fixed_size if t > -1: self.short(price, self.fixed_size, True) if t > -2: self.short(price - self.atr_value * 0.5, self.fixed_size, True) if t > -3: self.short(price - self.atr_value, self.fixed_size, True) if t > -4: self.short(price - self.atr_value * 1.5, self.fixed_size, True)
class wma_tp_01(CtaTemplate): """""" #=== 注明所需要的变量 # 交易时间设置 DAY_START = dt.time(9, 50) DAY_END = dt.time(16, 00) close_pos_time = dt.time(hour=15, minute=59) trading_time_hours = [9, 10, 11, 12, 13, 14, 15, 16] trading_time_mins = [50, 5] is_trading = False orderid_condition = [] orderid_sl = [] load_days = 3 ma_len, bd = 60, 4 dk_len = 20 acc = 7 stop_loss = 100 zhouqi = 3 parameters = ['ma_len', 'bd', "dk_len", 'acc', 'zhouqi'] up, do, me = 0, 0, 0 acc0 = -10 variables = [] open_long_price = 0 open_short_price = 0 close_long_price = 0 close_short_price = 0 # 止盈止损价预设 stop_loss_price = 0 stop_yingli_price = 0 records = ['up', 'do', 'stop_loss_price'] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): super(wma_tp_01, self).__init__(cta_engine, strategy_name, vt_symbol, setting) #===k-bar合成管理功能函数 self.bg = BarGenerator(self.on_bar, self.zhouqi, self.on_3min_bar) #===运行态,保存3min_bar功能函数 self.am = ArrayManager(size=150) # === 新的bar更新之后,怎么操作: def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg.update_bar(bar) #=== 每个5min——bar怎么操作: def on_3min_bar(self, bar: BarData): """""" self.am.update_bar(bar) am = self.am if not am.inited: return # === 交易订单处理 for orderid in self.orderid_condition: self.cancel_order(orderid) self.orderid_condition.clear() # 指标计算 me = talib.WMA(am.close, self.ma_len) # me_2 = talib.MA(am.high, self.ma_len) # me_3 = talib.MA(am.low, self.ma_len) # 计算指标波动率 atr = am.atr(self.ma_len) up = me + atr * self.bd do = me - atr * self.bd mo = (am.close[-1] - am.close[self.dk_len]) canshu = [me, up, do, atr] stop_loss_condition, self.stop_loss_price = self.stop_loss_func( am=am, canshu=canshu, bar=bar) # 判断在?交易时间段 if 0 == self.if_can_trading(bar): return else: pass # 准备变量 per_pos = 1 # 每次开仓手数(平仓) # 开平价格设置:追入买进 open_long_price = am.close[-1] + 10 open_short_price = am.close[-1] - 10 close_long_price = am.close[-1] - 10 close_short_price = am.close[-1] + 10 # === 整理仓位 === 是否要进行,盘尾日内平仓 if self.pos != 0 and self.if_today_clsoe(bar): if self.pos > 0: orderid = self.sell(bar.close_price - 1, abs(self.pos)) self.orderid_condition.extend(orderid) # 尾部添加orderid elif self.pos < 0: orderid = self.cover(bar.close_price + 1, abs(self.pos)) self.orderid_condition.extend(orderid) return self.cancel_all() # 清仓 # === 计算开平,止盈止损,条件 # open_long_condition = False # open_short_condition = False # close_long_condition = False # close_short_condition = False # stop_loss_condition = False # stop_yingli_condition = False if self.pos == 0: open_long_condition = am.close[-1] > up[-1] and am.close[-2] <= up[ -2] and mo >= 0 open_short_condition = am.close[-1] < do[-1] and am.close[ -2] >= do[-2] and mo <= 0 # close_long_condition = am.close[-1] < me[-1] and am.close[-2] >= me[-2] #平多 # close_short_condition = am.close[-1] > me[-1] and am.close[-2] <= me[-2] # 平多 # === 判断开平仓 if open_long_condition: self.buy(open_long_price, per_pos, stop=False) elif open_short_condition: self.sell(open_short_price, per_pos, stop=False) else: # print('无交易') pass elif self.pos > 0: close_long_condition = am.close[-1] < me[-1] and am.close[ -2] >= me[-2] open_short_condition = am.close[-1] < do[-1] and am.close[ -2] >= do[-2] and mo <= 0 stop_yingli_condition = False # === 判断止盈止损 if stop_loss_condition: self.sell(close_long_price, per_pos, stop=False) else: # === 判断开平仓 if open_short_condition: self.sell(open_short_price, per_pos * 2, stop=False) elif close_long_condition: self.sell(close_long_price, per_pos, stop=False) else: pass elif self.pos < 0: close_short_condition = am.close[-1] > me[-1] and am.close[ -2] <= me[-2] open_long_condition = am.close[-1] > up[-1] and am.close[-2] <= up[ -2] and mo >= 0 # === 判断止盈止损 if stop_loss_condition: self.buy(close_short_price, per_pos, stop=False) # === 判断开平仓 else: if open_long_condition: self.buy(open_long_price, per_pos * 2, stop=False) elif close_short_condition: self.buy(close_short_price, per_pos, stop=False) else: # print('无交易') pass # print(bar.datetime, '当前仓位:', self.pos) # print(stop_loss_condition) else: pass self.up, self.do, self.me = up[-1], do[-1], me[-1] self.put_event() #===初始化策略 def on_init(self): """ Callback when strategy is inited. """ # ===提前加载k_bar self.load_bar(days=self.load_days, callback=self.init_bar) #过滤bar, def init_bar(self, bar: BarData): if bar.datetime.replace(second=0) >= dt.datetime.now( get_localzone()).replace(second=0) - dt.timedelta(minutes=1): self.bg.bar = bar else: self.on_bar(bar) #=== 启动策略执行操作 def on_start(self): """ Callback when strategy is started. """ #=== 策略停止执行操作 def on_stop(self): """ Callback when strategy is stopped. """ #=== 每个tick怎么操作:策略主逻辑。 def on_tick(self, tick: TickData): """ Callback of new tick data update. """ self.bg.update_tick(tick) #=== 如果有断网等情况,,恢复数据和参数等数据 def on_recover(self, variables): for n, v in variables.items(): setattr(self, n, v) #===下单之后,返回的结果:》订单状态 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. """ #=== 停止单的信息状态返回 def on_stop_order(self, stop_order: StopOrder): """ Callback of stop order update. """ pass #=== 计算当天的开盘时间与收盘时间,返回是否开始交易 def if_can_trading( self, bar: BarData, ): # 计算当天的开盘时间与收盘时间 if (dt.time(bar.datetime.hour, bar.datetime.minute) >= self.DAY_START) and\ (dt.time(bar.datetime.hour,bar.datetime.minute) <= self.DAY_END): is_trading = True else: is_trading = False return is_trading #=== 判断今日尾盘是否清仓 def if_today_clsoe(self, bar): return bar.datetime.time() >= self.close_pos_time def stop_loss_func(self, am, canshu, bar, acc0=-10): me = canshu[0] stop_loss_price_shift = self.stop_loss_price if self.pos > 0: if am.close[-1] > am.open[-1]: self.acc0 += self.acc self.stop_loss_price = me[-1] + self.acc0 else: self.stop_loss_price = self.stop_loss_price self.stop_loss_price = max(stop_loss_price_shift, self.stop_loss_price) if am.close[-1] < self.stop_loss_price: return True, self.stop_loss_price else: return False, self.stop_loss_price elif self.pos < 0: if am.close[-1] < am.open[-1]: self.acc0 += self.acc self.stop_loss_price = me[-1] - self.acc0 else: self.stop_loss_price = self.stop_loss_price self.stop_loss_price = min(stop_loss_price_shift, self.stop_loss_price) if am.close[-1] > self.stop_loss_price: return True, self.stop_loss_price else: return False, self.stop_loss_price elif self.pos == 0: self.stop_loss_price = me[-1] self.acc0 = acc0 return False, self.stop_loss_price else: print('情况出错了') self.stop_loss_price = me[-1] self.acc0 = acc0 return False, self.stop_loss_price def open_triopen_trigger(self, ): # 记录trend的时间周期 self.time_go if self.trend == 1: self.time_go pass elif self.trend == -1: pass elif self.trend == 0: pass else: pass
class AtrRsiStrategy(CtaTemplate): """""" author = "用Python的交易员" atr_length = 22 atr_ma_length = 10 rsi_length = 5 rsi_entry = 16 trailing_percent = 0.8 fixed_size = 1 atr_value = 0 atr_ma = 0 rsi_value = 0 rsi_buy = 0 rsi_sell = 0 intra_trade_high = 0 intra_trade_low = 0 parameters = [ "atr_length", "atr_ma_length", "rsi_length", "rsi_entry", "trailing_percent", "fixed_size" ] variables = ["atr_value", "atr_ma", "rsi_value", "rsi_buy", "rsi_sell"] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(AtrRsiStrategy, self).__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.rsi_buy = 50 + self.rsi_entry self.rsi_sell = 50 - self.rsi_entry 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): """ Callback of new tick data update. """ self.bg.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.cancel_all() am = self.am am.update_bar(bar) if not am.inited: return atr_array = am.atr(self.atr_length, array=True) self.atr_value = atr_array[-1] self.atr_ma = atr_array[-self.atr_ma_length:].mean() self.rsi_value = am.rsi(self.rsi_length) if self.pos == 0: self.intra_trade_high = bar.high_price self.intra_trade_low = bar.low_price if self.atr_value > self.atr_ma: if self.rsi_value > self.rsi_buy: self.buy(bar.close_price + 5, self.fixed_size) elif self.rsi_value < self.rsi_sell: self.short(bar.close_price - 5, self.fixed_size) elif self.pos > 0: self.intra_trade_high = max(self.intra_trade_high, bar.high_price) self.intra_trade_low = bar.low_price long_stop = self.intra_trade_high * \ (1 - self.trailing_percent / 100) self.sell(long_stop, abs(self.pos), stop=True) elif self.pos < 0: self.intra_trade_low = min(self.intra_trade_low, bar.low_price) self.intra_trade_high = bar.high_price short_stop = self.intra_trade_low * \ (1 + self.trailing_percent / 100) self.cover(short_stop, abs(self.pos), stop=True) 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
class KingKeltnerStrategy(CtaTemplate): """""" author = '用Python的交易员' kk_length = 11 kk_dev = 1.6 trailing_percent = 0.8 fixed_size = 1 kk_up = 0 kk_down = 0 intra_trade_high = 0 intra_trade_low = 0 long_vt_orderids = [] short_vt_orderids = [] vt_orderids = [] parameters = ['kk_length', 'kk_dev', 'fixed_size'] variables = ['kk_up', 'kk_down'] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(KingKeltnerStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg = BarGenerator(self.on_bar, 5, self.on_5min_bar) self.am = ArrayManager() def on_init(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): """ Callback of new tick data update. """ self.bg.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg.update_bar(bar) def on_5min_bar(self, bar: BarData): """""" for orderid in self.vt_orderids: self.cancel_order(orderid) self.vt_orderids.clear() am = self.am am.update_bar(bar) if not am.inited: return self.kk_up, self.kk_down = am.keltner(self.kk_length, self.kk_dev) if self.pos == 0: self.intra_trade_high = bar.high_price self.intra_trade_low = bar.low_price self.send_oco_order(self.kk_up, self.kk_down, self.fixed_size) elif self.pos > 0: self.intra_trade_high = max(self.intra_trade_high, bar.high_price) self.intra_trade_low = bar.low_price vt_orderid = self.sell( self.intra_trade_high * (1 - self.trailing_percent / 100), abs(self.pos), True) self.vt_orderids.append(vt_orderid) elif self.pos < 0: self.intra_trade_high = bar.high_price self.intra_trade_low = min(self.intra_trade_low, bar.low_price) vt_orderid = self.cover( self.intra_trade_low * (1 + self.trailing_percent / 100), abs(self.pos), True) self.vt_orderids.append(vt_orderid) 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. """ if self.pos != 0: if self.pos > 0: for short_orderid in self.short_vt_orderids: self.cancel_order(short_orderid) elif self.pos < 0: for buy_orderid in self.long_vt_orderids: self.cancel_order(buy_orderid) for orderid in (self.long_vt_orderids + self.short_vt_orderids): if orderid in self.vt_orderids: self.vt_orderids.remove(orderid) self.put_event() def send_oco_order(self, buy_price, short_price, volume): """""" self.long_vt_orderids = self.buy(buy_price, volume, True) self.short_vt_orderids = self.short(short_price, volume, True) self.vt_orderids.extend(self.long_vt_orderids) self.vt_orderids.extend(self.short_vt_orderids) def on_stop_order(self, stop_order: StopOrder): """ Callback of stop order update. """ pass
class DualThrustStrategy(CtaTemplate): """""" author = "用Python的交易员" fixed_size = 1 k1 = 0.4 k2 = 0.6 bars = [] day_open = 0 day_high = 0 day_low = 0 range = 0 long_entry = 0 short_entry = 0 exit_time = time(hour=14, minute=55) long_entered = False short_entered = False parameters = ["k1", "k2", "fixed_size"] variables = ["range", "long_entry", "short_entry", "exit_time"] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(DualThrustStrategy, self).__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg = BarGenerator(self.on_bar) self.am = ArrayManager() self.bars = [] def on_init(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): """ Callback of new tick data update. """ self.bg.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.cancel_all() self.bars.append(bar) if len(self.bars) <= 2: return else: self.bars.pop(0) last_bar = self.bars[-2] if last_bar.datetime.date() != bar.datetime.date(): if self.day_high: self.range = self.day_high - self.day_low self.long_entry = bar.open_price + self.k1 * self.range self.short_entry = bar.open_price - self.k2 * self.range self.day_open = bar.open_price self.day_high = bar.high_price self.day_low = bar.low_price self.long_entered = False self.short_entered = False else: self.day_high = max(self.day_high, bar.high_price) self.day_low = min(self.day_low, bar.low_price) if not self.range: return if bar.datetime.time() < self.exit_time: if self.pos == 0: if bar.close_price > self.day_open: if not self.long_entered: self.buy(self.long_entry, self.fixed_size, stop=True) else: if not self.short_entered: self.short(self.short_entry, self.fixed_size, stop=True) elif self.pos > 0: self.long_entered = True self.sell(self.short_entry, self.fixed_size, stop=True) if not self.short_entered: self.short(self.short_entry, self.fixed_size, stop=True) elif self.pos < 0: self.short_entered = True self.cover(self.long_entry, self.fixed_size, stop=True) if not self.long_entered: self.buy(self.long_entry, self.fixed_size, stop=True) else: if self.pos > 0: self.sell(bar.close_price * 0.99, abs(self.pos)) elif self.pos < 0: self.cover(bar.close_price * 1.01, 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
class DoubleMaStrategy(CtaTemplate): author = "用Python的交易员" fast_window = 10 slow_window = 20 fast_ma0 = 0.0 fast_ma1 = 0.0 slow_ma0 = 0.0 slow_ma1 = 0.0 parameters = ["fast_window", "slow_window"] variables = ["fast_ma0", "fast_ma1", "slow_ma0", "slow_ma1"] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(DoubleMaStrategy, self).__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(1, callback=self.init_bar) def init_bar(self, bar: BarData): if bar.datetime.replace(second=0) >= dt.datetime.now().replace( second=0) - dt.timedelta(minutes=1): self.bg.bar = bar else: am = self.am am.update_bar(bar) 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. """ print(bar) am = self.am am.update_bar(bar) if not am.inited: return fast_ma = am.sma(self.fast_window, array=True) self.fast_ma0 = fast_ma[-1] self.fast_ma1 = fast_ma[-2] slow_ma = am.sma(self.slow_window, array=True) self.slow_ma0 = slow_ma[-1] self.slow_ma1 = slow_ma[-2] cross_over = self.fast_ma0 > self.slow_ma0 and self.fast_ma1 < self.slow_ma1 cross_below = self.fast_ma0 < self.slow_ma0 and self.fast_ma1 > self.slow_ma1 if cross_over: if self.pos == 0: self.buy(bar.close_price, 1) elif self.pos < 0: self.cover(bar.close_price, 1) self.buy(bar.close_price, 1) elif cross_below: if self.pos == 0: self.short(bar.close_price, 1) elif self.pos > 0: self.sell(bar.close_price, 1) self.short(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