实盘运行
在实盘环境,用户可以基于编写好的CTA策略来创建新的实例,一键初始化、启动、停止策略。
创建策略实例
用户可以基于编写好的CTA策略来创建新的实例,策略实例的好处在于同一个策略可以同时去运行多个品种合约,并且每个实例的参数可以是不同的。在创建实例的时候需要填写如图的实例名称、合约品种、参数设置。注意:实例名称不能重名;合约名称是vt_symbol的格式,如IF1905.CFFEX。
创建策略流程如下:
- 检查策略实例重名
- 添加策略配置信息(strategy_name, vt_symbol, setting)到strategies字典上
- 添加该策略要订阅行情的合约信息到symbol_strategy_map字典中;
- 把策略配置信息保存到json文件内;
- 在图形化界面更新状态信息。
- def add_strategy(
- self, class_name: str, strategy_name: str, vt_symbol: str, setting: dict
- ):
- """
- Add a new strategy.
- """
- if strategy_name in self.strategies:
- self.write_log(f"创建策略失败,存在重名{strategy_name}")
- return
- strategy_class = self.classes[class_name]
- strategy = strategy_class(self, strategy_name, vt_symbol, setting)
- self.strategies[strategy_name] = strategy
- # Add vt_symbol to strategy map.
- strategies = self.symbol_strategy_map[vt_symbol]
- strategies.append(strategy)
- # Update to setting file.
- self.update_strategy_setting(strategy_name, setting)
- self.put_strategy_event(strategy)
初始化策略
- 调用策略类的on_init()回调函数,并且载入历史数据;
- 恢复上次退出之前的策略状态;
- 调用接口的subcribe()函数订阅指定行情信息;
- 策略初始化状态变成True,并且更新到日志上。
- def _init_strategy(self):
- """
- Init strategies in queue.
- """
- while not self.init_queue.empty():
- strategy_name = self.init_queue.get()
- strategy = self.strategies[strategy_name]
- if strategy.inited:
- self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作")
- continue
- 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 value:
- setattr(strategy, name, value)
- # Subscribe market data
- contract = self.main_engine.get_contract(strategy.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"行情订阅失败,找不到合约{strategy.vt_symbol}", strategy)
- # Put event to update init completed status.
- strategy.inited = True
- self.put_strategy_event(strategy)
- self.write_log(f"{strategy_name}初始化完成")
- self.init_thread = None
启动策略
- 检查策略初始化状态;
- 检查策略启动状态,避免重复启动;
- 调用策略类的on_start()函数启动策略;
- 策略启动状态变成True,并且更新到图形化界面上。
- def start_strategy(self, strategy_name: str):
- """
- Start a strategy.
- """
- strategy = self.strategies[strategy_name]
- if not strategy.inited:
- self.write_log(f"策略{strategy.strategy_name}启动失败,请先初始化")
- return
- if strategy.trading:
- self.write_log(f"{strategy_name}已经启动,请勿重复操作")
- return
- self.call_strategy_func(strategy, strategy.on_start)
- strategy.trading = True
- self.put_strategy_event(strategy)
停止策略
- 检查策略启动状态;
- 调用策略类的on_stop()函数停止策略;
- 更新策略启动状态为False;
- 对所有为成交的委托(市价单/限价单/本地停止单)进行撤单操作;
- 在图形化界面更新策略状态。
- def stop_strategy(self, strategy_name: str):
- """
- Stop a strategy.
- """
- strategy = self.strategies[strategy_name]
- if not strategy.trading:
- return
- # Call on_stop function of the strategy
- self.call_strategy_func(strategy, strategy.on_stop)
- # Change trading status of strategy to False
- strategy.trading = False
- # Cancel all orders of the strategy
- self.cancel_all(strategy)
- # Update GUI
- self.put_strategy_event(strategy)
编辑策略
- 重新配置策略参数字典setting;
- 更新参数字典到策略中;
- 在图像化界面更新策略状态。
- def edit_strategy(self, strategy_name: str, setting: dict):
- """
- Edit parameters of a strategy.
- """
- strategy = self.strategies[strategy_name]
- strategy.update_setting(setting)
- self.update_strategy_setting(strategy_name, setting)
- self.put_strategy_event(strategy)
移除策略
- 检查策略状态,只有停止策略后从可以移除策略;
- 从json文件移除策略配置信息(strategy_name, vt_symbol, setting);
- 从symbol_strategy_map字典中移除该策略订阅的合约信息;
- 从strategy_orderid_map字典移除活动委托记录;
- 从strategies字典移除该策略的相关配置信息。
- def remove_strategy(self, strategy_name: str):
- """
- Remove a strategy.
- """
- strategy = self.strategies[strategy_name]
- if strategy.trading:
- self.write_log(f"策略{strategy.strategy_name}移除失败,请先停止")
- return
- # Remove setting
- self.remove_strategy_setting(strategy_name)
- # Remove from symbol strategy map
- strategies = self.symbol_strategy_map[strategy.vt_symbol]
- strategies.remove(strategy)
- # Remove from active orderid map
- if strategy_name in self.strategy_orderid_map:
- vt_orderids = self.strategy_orderid_map.pop(strategy_name)
- # Remove vt_orderid strategy map
- for vt_orderid in vt_orderids:
- if vt_orderid in self.orderid_strategy_map:
- self.orderid_strategy_map.pop(vt_orderid)
- # Remove from strategies
- self.strategies.pop(strategy_name)
- return True