实盘运行

在实盘环境,用户可以基于编写好的组合策略来创建新的实例,一键初始化、启动、停止策略(不要忘记先连接对应接口,获取行情)。

创建策略实例

用户可以基于编写好的组合策略来创建新的实例,策略实例的好处在于同一个策略可以同时去运行多个品种合约,并且每个实例的参数可以是不同的。 在创建实例的时候需要填写如图的实例名称、合约品种、参数设置。注意:实例名称不能重名;合约名称是vt_symbol的格式,如y2101.DCE,p2101.DCE。

https://user-images.githubusercontent.com/11263900/93662304-b6a9ba80-fa91-11ea-895a-ae56fc7ddf60.png创建

创建策略流程如下:

  • 检查策略实例重名

  • 检查是否存在策略类

  • 添加策略配置信息(strategy_name, vt_symbols, setting)到strategies字典上

  • 添加该策略要订阅行情的所有合约信息到symbol_strategy_map字典中;

  • 把策略配置信息保存到json文件内;

  • 在图形化界面更新状态信息。

  1. def add_strategy(
  2. self, class_name: str, strategy_name: str, vt_symbols: str, setting: dict
  3. ):
  4. """
  5. Add a new strategy.
  6. """
  7. if strategy_name in self.strategies:
  8. self.write_log(f"创建策略失败,存在重名{strategy_name}")
  9. return
  10. strategy_class = self.classes.get(class_name, None)
  11. if not strategy_class:
  12. self.write_log(f"创建策略失败,找不到策略类{class_name}")
  13. return
  14. strategy = strategy_class(self, strategy_name, vt_symbols, setting)
  15. self.strategies[strategy_name] = strategy
  16. # Add vt_symbol to strategy map.
  17. for vt_symbol in vt_symbols:
  18. strategies = self.symbol_strategy_map[vt_symbol]
  19. strategies.append(strategy)
  20. self.save_strategy_setting()
  21. self.put_strategy_event(strategy)

https://user-images.githubusercontent.com/11263900/93662303-b27d9d00-fa91-11ea-824f-58645bd4e202.png初始化

初始化策略

  • 调用策略类的on_init()回调函数,并且载入历史数据;

  • 恢复上次退出之前的策略状态;

  • 从.vntrader/portfolio_strategy_data.json内读取策略参数,最新的技术指标,以及持仓数量;

  • 调用接口的subcribe()函数订阅指定行情信息;

  • 策略初始化状态变成True,并且更新到日志上。

  1. def _init_strategy(self, strategy_name: str):
  2. """
  3. Init strategies in queue.
  4. """
  5. strategy = self.strategies[strategy_name]
  6. if strategy.inited:
  7. self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作")
  8. return
  9. self.write_log(f"{strategy_name}开始执行初始化")
  10. # Call on_init function of strategy
  11. self.call_strategy_func(strategy, strategy.on_init)
  12. # Restore strategy data(variables)
  13. data = self.strategy_data.get(strategy_name, None)
  14. if data:
  15. for name in strategy.variables:
  16. value = data.get(name, None)
  17. if name == "pos":
  18. pos = getattr(strategy, name)
  19. pos.update(value)
  20. elif value:
  21. setattr(strategy, name, value)
  22. # Subscribe market data
  23. for vt_symbol in strategy.vt_symbols:
  24. contract: ContractData = self.main_engine.get_contract(vt_symbol)
  25. if contract:
  26. req = SubscribeRequest(
  27. symbol=contract.symbol, exchange=contract.exchange)
  28. self.main_engine.subscribe(req, contract.gateway_name)
  29. else:
  30. self.write_log(f"行情订阅失败,找不到合约{vt_symbol}", strategy)
  31. # Put event to update init completed status.
  32. strategy.inited = True
  33. self.put_strategy_event(strategy)
  34. self.write_log(f"{strategy_name}初始化完成")

启动策略

  • 检查策略初始化状态;

  • 检查策略启动状态,避免重复启动;

  • 调用策略类的on_start()函数启动策略;

  • 策略启动状态变成True,并且更新到图形化界面上。

  1. def start_strategy(self, strategy_name: str):
  2. """
  3. Start a strategy.
  4. """
  5. strategy = self.strategies[strategy_name]
  6. if not strategy.inited:
  7. self.write_log(f"策略{strategy.strategy_name}启动失败,请先初始化")
  8. return
  9. if strategy.trading:
  10. self.write_log(f"{strategy_name}已经启动,请勿重复操作")
  11. return
  12. self.call_strategy_func(strategy, strategy.on_start)
  13. strategy.trading = True
  14. self.put_strategy_event(strategy)

https://user-images.githubusercontent.com/11263900/93662312-bdd0c880-fa91-11ea-97b1-8a3505cfcb97.png启动

如果当天发生了交易,主界面如图:

https://user-images.githubusercontent.com/11263900/93662316-bf01f580-fa91-11ea-8ac6-807e28cbadd5.png主界面

停止策略

  • 检查策略启动状态;

  • 调用策略类的on_stop()函数停止策略;

  • 更新策略启动状态为False;

  • 对所有为成交的委托(市价单/限价单/本地停止单)进行撤单操作;

  • 把策略参数,最新的技术指标,以及持仓数量保存到.vntrader/cta_strategy_data.json内;

  • 在图形化界面更新策略状态。

  1. def stop_strategy(self, strategy_name: str):
  2. """
  3. Stop a strategy.
  4. """
  5. strategy = self.strategies[strategy_name]
  6. if not strategy.trading:
  7. return
  8. # Call on_stop function of the strategy
  9. self.call_strategy_func(strategy, strategy.on_stop)
  10. # Change trading status of strategy to False
  11. strategy.trading = False
  12. # Cancel all orders of the strategy
  13. self.cancel_all(strategy)
  14. # Sync strategy variables to data file
  15. self.sync_strategy_data(strategy)
  16. # Update GUI
  17. self.put_strategy_event(strategy)

https://user-images.githubusercontent.com/11263900/93662315-be695f00-fa91-11ea-8b1a-78a2f945db23.png停止

编辑策略

  • 重新配置策略参数字典setting;

  • 更新参数字典到策略中;

  • 保存参数字典setting;

  • 在图像化界面更新策略状态。

  1. def edit_strategy(self, strategy_name: str, setting: dict):
  2. """
  3. Edit parameters of a strategy.
  4. """
  5. strategy = self.strategies[strategy_name]
  6. strategy.update_setting(setting)
  7. self.save_strategy_setting()
  8. self.put_strategy_event(strategy)

移除策略

  • 检查策略状态,只有停止策略后从可以移除策略;

  • 从symbol_strategy_map字典中移除该策略订阅的合约信息;

  • 从strategy_orderid_map字典移除活动委托记录;

  • 从strategies字典移除该策略的相关配置信息。

  1. def remove_strategy(self, strategy_name: str):
  2. """
  3. Remove a strategy.
  4. """
  5. strategy = self.strategies[strategy_name]
  6. if strategy.trading:
  7. self.write_log(f"策略{strategy.strategy_name}移除失败,请先停止")
  8. return
  9. # Remove from symbol strategy map
  10. for vt_symbol in strategy.vt_symbols:
  11. strategies = self.symbol_strategy_map[vt_symbol]
  12. strategies.remove(strategy)
  13. # Remove from vt_orderid strategy map
  14. for vt_orderid in strategy.active_orderids:
  15. if vt_orderid in self.orderid_strategy_map:
  16. self.orderid_strategy_map.pop(vt_orderid)
  17. # Remove from strategies
  18. self.strategies.pop(strategy_name)
  19. self.save_strategy_setting()
  20. return True