def get_leg(self, vt_symbol: str) -> LegData: """""" leg = self.legs.get(vt_symbol, None) if not leg: leg = LegData(vt_symbol) self.legs[vt_symbol] = leg # Subscribe market data contract = self.main_engine.get_contract(vt_symbol) if contract: leg.update_contract(contract) req = SubscribeRequest(contract.symbol, contract.exchange) self.main_engine.subscribe(req, contract.gateway_name) # Initialize leg position for direction in Direction: vt_positionid = f"{vt_symbol}.{direction.value}" position = self.main_engine.get_position(vt_positionid) if position: leg.update_position(position) return leg
def process_contract_event(self, event: Event) -> None: """""" contract = event.data leg = self.legs.get(contract.vt_symbol, None) if leg: # Update contract data leg.update_contract(contract) req = SubscribeRequest(contract.symbol, contract.exchange) self.main_engine.subscribe(req, contract.gateway_name)
def subscribe_data(self, vt_symbol: str): """""" contract = self.main_engine.get_contract(vt_symbol) if not contract: return req = SubscribeRequest( symbol=contract.symbol, exchange=contract.exchange ) self.main_engine.subscribe(req, contract.gateway_name)
def subscribe(self, algo: AlgoTemplate, vt_symbol: str): """""" contract = self.main_engine.get_contract(vt_symbol) if not contract: self.write_log(f'订阅行情失败,找不到合约:{vt_symbol}', algo) return algos = self.symbol_algo_map.setdefault(vt_symbol, set()) if not algos: req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange) self.main_engine.subscribe(req, contract.gateway_name) algos.add(algo)
def process_history_event(self, event: Event) -> None: """""" history: List[BarData] = event.data if not history: return bar = history[0] chart = self.charts[bar.vt_symbol] chart.update_history(history) # Subscribe following data update contract = self.main_engine.get_contract(bar.vt_symbol) req = SubscribeRequest( contract.symbol, contract.exchange ) self.main_engine.subscribe(req, contract.gateway_name)
def _init_strategy(self, strategy_name: str): """ Init strategies in queue. """ strategy = self.strategies[strategy_name] if strategy.inited: self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作") return self.write_log(f"{strategy_name}开始执行初始化") # Call on_init function of strategy self.call_strategy_func(strategy, strategy.on_init) # Restore strategy data(variables) data = self.strategy_data.get(strategy_name, None) if data: for name in strategy.variables: value = data.get(name, None) if name == "pos": pos = getattr(strategy, name) pos.update(value) elif value: setattr(strategy, name, value) # Subscribe market data for vt_symbol in strategy.vt_symbols: contract: ContractData = self.main_engine.get_contract(vt_symbol) if contract: req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange) self.main_engine.subscribe(req, contract.gateway_name) else: self.write_log(f"行情订阅失败,找不到合约{vt_symbol}", strategy) # Put event to update init completed status. strategy.inited = True self.put_strategy_event(strategy) self.write_log(f"{strategy_name}初始化完成")
def subscribe(self, contract: ContractData): """""" req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange) self.main_engine.subscribe(req, contract.gateway_name)