def __init__(self, strategy_engine, strategy_name: str, spread: SpreadData, setting: dict): """""" super().__init__(strategy_engine, strategy_name, spread, setting) self.bg = BarGenerator(self.on_spread_bar) self.am = ArrayManager()
def new_chart(self) -> None: """""" # Filter invalid vt_symbol vt_symbol = self.symbol_line.text() if not vt_symbol: return if vt_symbol in self.charts: return contract = self.main_engine.get_contract(vt_symbol) if not contract: return # Create new chart self.bgs[vt_symbol] = BarGenerator(self.on_bar) chart = self.create_chart() self.charts[vt_symbol] = chart self.tab.addTab(chart, vt_symbol) # Query history data end = datetime.now(get_localzone()) start = end - timedelta(days=5) self.chart_engine.query_history(vt_symbol, Interval.MINUTE, start, end)
def __init__(self, strategy_engine: StrategyEngine, strategy_name: str, vt_symbols: List[str], setting: dict): """""" super().__init__(strategy_engine, strategy_name, vt_symbols, setting) self.bgs: Dict[str, BarGenerator] = {} self.ams: Dict[str, ArrayManager] = {} self.rsi_data: Dict[str, float] = {} self.atr_data: Dict[str, float] = {} self.atr_ma: Dict[str, float] = {} self.intra_trade_high: Dict[str, float] = {} self.intra_trade_low: Dict[str, float] = {} self.targets: Dict[str, int] = {} self.last_tick_time: datetime = None # Obtain contract info for vt_symbol in self.vt_symbols: def on_bar(bar: BarData): """""" pass self.bgs[vt_symbol] = BarGenerator(on_bar) self.ams[vt_symbol] = ArrayManager() self.targets[vt_symbol] = 0
def __init__(self, strategy_engine: StrategyEngine, strategy_name: str, vt_symbols: List[str], setting: dict): """""" super().__init__(strategy_engine, strategy_name, vt_symbols, setting) self.bgs: Dict[str, BarGenerator] = {} self.last_tick_time: datetime = None # Obtain contract info for vt_symbol in self.vt_symbols: symbol, exchange = extract_vt_symbol(vt_symbol) if "C" in symbol: self.call_symbol = vt_symbol _, strike_str = symbol.split("-C-") # For CFFEX/DCE options self.strike_price = int(strike_str) elif "P" in symbol: self.put_symbol = vt_symbol else: self.futures_symbol = vt_symbol def on_bar(bar: BarData): """""" pass self.bgs[vt_symbol] = BarGenerator(on_bar)
def get_bar_generator(self, vt_symbol: str): """""" bg = self.bar_generators.get(vt_symbol, None) if not bg: bg = BarGenerator(self.record_bar) self.bar_generators[vt_symbol] = bg return bg
def __init__(self, strategy_engine: StrategyEngine, strategy_name: str, vt_symbols: List[str], setting: dict): """""" super().__init__(strategy_engine, strategy_name, vt_symbols, setting) self.bgs: Dict[str, BarGenerator] = {} self.targets: Dict[str, int] = {} self.last_tick_time: datetime = None self.spread_count: int = 0 self.spread_data: np.array = np.zeros(100) # Obtain contract info self.leg1_symbol, self.leg2_symbol = vt_symbols def on_bar(bar: BarData): """""" pass for vt_symbol in self.vt_symbols: self.targets[vt_symbol] = 0 self.bgs[vt_symbol] = BarGenerator(on_bar)
class StatisticalArbitrageStrategy(SpreadStrategyTemplate): """""" author = "用Python的交易员" boll_window = 20 boll_dev = 2 max_pos = 10 payup = 10 interval = 5 spread_pos = 0.0 boll_up = 0.0 boll_down = 0.0 boll_mid = 0.0 parameters = ["boll_window", "boll_dev", "max_pos", "payup", "interval"] variables = ["spread_pos", "boll_up", "boll_down", "boll_mid"] def __init__(self, strategy_engine, strategy_name: str, spread: SpreadData, setting: dict): """""" super().__init__(strategy_engine, strategy_name, spread, setting) self.bg = BarGenerator(self.on_spread_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("策略停止") self.put_event() def on_spread_data(self): """ Callback when spread price is updated. """ tick = self.get_spread_tick() self.on_spread_tick(tick) def on_spread_tick(self, tick: TickData): """ Callback when new spread tick data is generated. """ self.bg.update_tick(tick) def on_spread_bar(self, bar: BarData): """ Callback when spread bar data is generated. """ self.stop_all_algos() self.am.update_bar(bar) if not self.am.inited: return self.boll_mid = self.am.sma(self.boll_window) self.boll_up, self.boll_down = self.am.boll(self.boll_window, self.boll_dev) if not self.spread_pos: if bar.close_price >= self.boll_up: self.start_short_algo(bar.close_price - 10, self.max_pos, payup=self.payup, interval=self.interval) elif bar.close_price <= self.boll_down: self.start_long_algo(bar.close_price + 10, self.max_pos, payup=self.payup, interval=self.interval) elif self.spread_pos < 0: if bar.close_price <= self.boll_mid: self.start_long_algo(bar.close_price + 10, abs(self.spread_pos), payup=self.payup, interval=self.interval) else: if bar.close_price >= self.boll_mid: self.start_short_algo(bar.close_price - 10, abs(self.spread_pos), payup=self.payup, interval=self.interval) self.put_event() def on_spread_pos(self): """ Callback when spread position is updated. """ self.spread_pos = self.get_spread_pos() self.put_event() def on_spread_algo(self, algo: SpreadAlgoTemplate): """ Callback when algo status is updated. """ pass def on_order(self, order: OrderData): """ Callback when order status is updated. """ pass def on_trade(self, trade: TradeData): """ Callback when new trade data is received. """ pass def stop_open_algos(self): """""" if self.buy_algoid: self.stop_algo(self.buy_algoid) if self.short_algoid: self.stop_algo(self.short_algoid) def stop_close_algos(self): """""" if self.sell_algoid: self.stop_algo(self.sell_algoid) if self.cover_algoid: self.stop_algo(self.cover_algoid)