def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super().__init__(cta_engine, strategy_name, vt_symbol, setting) self.rsi_long = 50 + self.rsi_signal self.rsi_short = 50 - self.rsi_signal self.bg5 = BarGenerator(self.on_bar, 5, self.on_5min_bar) self.am5 = ArrayManager() self.bg15 = BarGenerator(self.on_bar, 15, self.on_15min_bar) self.am15 = ArrayManager()
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() self.bars = []
def __init__(self, cta_engine: CtaEngine, strategy_name, vt_symbol, setting): """""" super().__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg_1hour = BarGenerator(self.on_bar, 1, self.on_1hour_bar, Interval.HOUR) self.am = ArrayManager(size=100) # 时间序列,类似我们用的pandas, 值保留最近的N个K线的数据.
class FixedTradeTimeStrategy(CtaTemplate): """ 基于价格的定投 """ author = "51bitquant" fixed_trade_money = 1000 parameters = ["fixed_trade_money"] def __init__(self, cta_engine: CtaEngine, strategy_name, vt_symbol, setting): """""" super().__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg_1hour = BarGenerator(self.on_bar, 1, self.on_1hour_bar, Interval.HOUR) self.am = ArrayManager(size=100) # 时间序列,类似我们用的pandas, 值保留最近的N个K线的数据. def on_init(self): """ Callback when strategy is inited. """ self.write_log("策略初始化") self.load_bar(1) # 具体加载多少天的数据, 1表示1天的数据,如果是2表示过去2天的数据 def on_start(self): """ Callback when strategy is started. """ self.write_log(f"我的策略启动") self.put_event() def on_stop(self): """ Callback when strategy is stopped. """ self.write_log("策略停止") self.put_event() def on_tick(self, tick: TickData): pass def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg_1hour.update_bar(bar) # 合成1小时的数据. self.put_event() def on_1hour_bar(self, bar: BarData): """ 1小时的K线数据. """ self.cancel_all() # 取消订单. self.am.update_bar(bar) # 把最新的K线放进时间序列里面. if not self.am.inited: # True return """ 定投逻辑: 周四下午三点定投, 周五下午四点定投 """ # 2000 * 54 # 10万美金, if bar.datetime.isoweekday() == 5 and bar.datetime.hour == 16: price = bar.close_price * 1.001 self.buy(price, self.fixed_trade_money/price) if bar.datetime.isoweekday() == 4 and bar.datetime.hour == 15: price = bar.close_price * 1.001 self.buy(price, self.fixed_trade_money / price) # 下面可以计算基数指标等等.... # 以及下单的事情. self.put_event() def on_order(self, order: OrderData): """ 订单的回调方法: 订单状态更新的时候,会调用这个方法。 """ self.put_event() def on_trade(self, trade: TradeData): """ """ self.put_event() # 更新UI界面方法。 def on_stop_order(self, stop_order: StopOrder): """ 这个是一个停止单的方法,用来监听你止损单的方法。 """ pass
class MultiTimeframeStrategy(CtaTemplate): """""" author = "用Python的交易员" rsi_signal = 20 rsi_window = 14 fast_window = 5 slow_window = 20 fixed_size = 1 rsi_value = 0 rsi_long = 0 rsi_short = 0 fast_ma = 0 slow_ma = 0 ma_trend = 0 parameters = [ "rsi_signal", "rsi_window", "fast_window", "slow_window", "fixed_size" ] variables = [ "rsi_value", "rsi_long", "rsi_short", "fast_ma", "slow_ma", "ma_trend" ] def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super().__init__(cta_engine, strategy_name, vt_symbol, setting) self.rsi_long = 50 + self.rsi_signal self.rsi_short = 50 - self.rsi_signal self.bg5 = BarGenerator(self.on_bar, 5, self.on_5min_bar) self.am5 = ArrayManager() self.bg15 = BarGenerator(self.on_bar, 15, self.on_15min_bar) self.am15 = 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.bg5.update_tick(tick) def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg5.update_bar(bar) self.bg15.update_bar(bar) def on_5min_bar(self, bar: BarData): """""" self.cancel_all() self.am5.update_bar(bar) if not self.am5.inited: return if not self.ma_trend: return close_5 = pd.Series(self.am5.close_array) self.rsi_value = ta.rsi(close_5, self.rsi_window).iloc[-1] if self.pos == 0: if self.ma_trend > 0 and self.rsi_value >= self.rsi_long: self.buy(bar.close_price + 5, self.fixed_size) elif self.ma_trend < 0 and self.rsi_value <= self.rsi_short: self.short(bar.close_price - 5, self.fixed_size) elif self.pos > 0: if self.ma_trend < 0 or self.rsi_value < 50: self.sell(bar.close_price - 5, abs(self.pos)) elif self.pos < 0: if self.ma_trend > 0 or self.rsi_value > 50: self.cover(bar.close_price + 5, abs(self.pos)) self.put_event() def on_15min_bar(self, bar: BarData): """""" self.am15.update_bar(bar) if not self.am15.inited: return close_15 = pd.Series(self.am15.close_array) self.fast_ma = ta.sma(close_15, self.fast_window).iloc[-1] self.slow_ma = ta.sma(close_15, self.slow_window).iloc[-1] if self.fast_ma > self.slow_ma: self.ma_trend = 1 else: self.ma_trend = -1 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
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, 15, self.on_15min_bar) self.am = ArrayManager()
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().__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 high = pd.Series(self.am.high_array) low = pd.Series(self.am.low_array) close = pd.Series(self.am.close_array) if not self.pos: self.entry_up, self.entry_down = high.rolling(self.entry_window).max().iloc[-1], low.rolling( self.entry_window).min().iloc[-1] self.exit_up, self.exit_down = high.rolling(self.exit_window).max().iloc[-1], \ low.rolling(self.entry_window).min().iloc[-1] if not self.pos: self.atr_value = ta.atr(high, low, close, self.atr_window).iloc[-1] # 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.entry_up) 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.entry_down) 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 FixedTradPriceStrategy(CtaTemplate): """ 基于价格的定投 """ author = "51bitquant" fixed_trade_money = 1000 # 每次定投的资金比例. price_change_pct = 0.05 # 价格变动多少的时候定投 parameters = ['fixed_trade_money', 'price_change_pct'] def __init__(self, cta_engine: CtaEngine, strategy_name, vt_symbol, setting): """""" super().__init__(cta_engine, strategy_name, vt_symbol, setting) self.bg_4hour = BarGenerator(self.on_bar, 4, self.on_4hour_bar, Interval.HOUR) self.am = ArrayManager(size=100) # 时间序列,类似我们用的pandas, 值保留最近的N个K线的数据. def on_init(self): """ Callback when strategy is inited. """ self.write_log("策略初始化") self.load_bar(1) # 具体加载多少天的数据, 1表示1天的数据,如果是2表示过去2天的数据 def on_start(self): """ Callback when strategy is started. """ self.write_log(f"我的策略启动") self.put_event() def on_stop(self): """ Callback when strategy is stopped. """ self.write_log("策略停止") self.put_event() def on_tick(self, tick: TickData): pass def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.bg_4hour.update_bar(bar) # 合成四小时的数据. self.put_event() def on_4hour_bar(self, bar: BarData): """ 四小时的K线数据. """ self.cancel_all() # 撤销所有订单. self.am.update_bar(bar) # 把最新的K线放进时间序列里面. # 下面可以计算基数指标等等.... # 以及下单的事情. if not self.am.inited: return # [0,1,2,3,4,5,6] last_close_price = self.am.close_array[-2] # 上一根K线 current_close_price = bar.close_price # self.am.close_array[-1] # 当前的收盘价 # 如果四小时价格下跌5%就买入. if (last_close_price - current_close_price)/last_close_price >= self.price_change_pct: price = bar.close_price * 1.001 self.buy(price, self.fixed_trade_money/price) self.put_event() def on_order(self, order: OrderData): """ 订单的回调方法: 订单状态更新的时候,会调用这个方法。 """ self.put_event() def on_trade(self, trade: TradeData): """ """ self.put_event() # 更新UI界面方法。 def on_stop_order(self, stop_order: StopOrder): """ 这个是一个停止单的方法,用来监听你止损单的方法。 """ pass