• 实盘运行
    • 创建策略实例
    • 初始化策略
    • 启动策略
    • 停止策略
    • 编辑策略
    • 移除策略

    实盘运行

    在实盘环境,用户可以基于编写好的CTA策略来创建新的实例,一键初始化、启动、停止策略。

    创建策略实例

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

    https://vnpy-community.oss-cn-shanghai.aliyuncs.com/forum_experience/yazhang/cta_strategy/add_strategy.png

    创建策略流程如下:

    • 检查策略实例重名
    • 添加策略配置信息(strategy_name, vt_symbol, setting)到strategies字典上
    • 添加该策略要订阅行情的合约信息到symbol_strategy_map字典中;
    • 把策略配置信息保存到json文件内;
    • 在图形化界面更新状态信息。
    1. def add_strategy(
    2. self, class_name: str, strategy_name: str, vt_symbol: 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.  
    11. strategy_class = self.classes[class_name]
    12.  
    13. strategy = strategy_class(self, strategy_name, vt_symbol, setting)
    14. self.strategies[strategy_name] = strategy
    15.  
    16. # Add vt_symbol to strategy map.
    17. strategies = self.symbol_strategy_map[vt_symbol]
    18. strategies.append(strategy)
    19.  
    20. # Update to setting file.
    21. self.update_strategy_setting(strategy_name, setting)
    22.  
    23. self.put_strategy_event(strategy)

    初始化策略

    • 调用策略类的on_init()回调函数,并且载入历史数据;
    • 恢复上次退出之前的策略状态;
    • 调用接口的subcribe()函数订阅指定行情信息;
    • 策略初始化状态变成True,并且更新到日志上。
    1. def _init_strategy(self):
    2. """
    3. Init strategies in queue.
    4. """
    5. while not self.init_queue.empty():
    6. strategy_name = self.init_queue.get()
    7. strategy = self.strategies[strategy_name]
    8.  
    9. if strategy.inited:
    10. self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作")
    11. continue
    12.  
    13. self.write_log(f"{strategy_name}开始执行初始化")
    14.  
    15. # Call on_init function of strategy
    16. self.call_strategy_func(strategy, strategy.on_init)
    17.  
    18. # Restore strategy data(variables)
    19. data = self.strategy_data.get(strategy_name, None)
    20. if data:
    21. for name in strategy.variables:
    22. value = data.get(name, None)
    23. if value:
    24. setattr(strategy, name, value)
    25.  
    26. # Subscribe market data
    27. contract = self.main_engine.get_contract(strategy.vt_symbol)
    28. if contract:
    29. req = SubscribeRequest(
    30. symbol=contract.symbol, exchange=contract.exchange)
    31. self.main_engine.subscribe(req, contract.gateway_name)
    32. else:
    33. self.write_log(f"行情订阅失败,找不到合约{strategy.vt_symbol}", strategy)
    34.  
    35. # Put event to update init completed status.
    36. strategy.inited = True
    37. self.put_strategy_event(strategy)
    38. self.write_log(f"{strategy_name}初始化完成")
    39.  
    40. self.init_thread = None

    启动策略

    • 检查策略初始化状态;
    • 检查策略启动状态,避免重复启动;
    • 调用策略类的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.  
    10. if strategy.trading:
    11. self.write_log(f"{strategy_name}已经启动,请勿重复操作")
    12. return
    13.  
    14. self.call_strategy_func(strategy, strategy.on_start)
    15. strategy.trading = True
    16.  
    17. self.put_strategy_event(strategy)

    停止策略

    • 检查策略启动状态;
    • 调用策略类的on_stop()函数停止策略;
    • 更新策略启动状态为False;
    • 对所有为成交的委托(市价单/限价单/本地停止单)进行撤单操作;
    • 在图形化界面更新策略状态。
    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.  
    9. # Call on_stop function of the strategy
    10. self.call_strategy_func(strategy, strategy.on_stop)
    11.  
    12. # Change trading status of strategy to False
    13. strategy.trading = False
    14.  
    15. # Cancel all orders of the strategy
    16. self.cancel_all(strategy)
    17.  
    18. # Update GUI
    19. self.put_strategy_event(strategy)

    编辑策略

    • 重新配置策略参数字典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.  
    8. self.update_strategy_setting(strategy_name, setting)
    9. self.put_strategy_event(strategy)

    移除策略

    • 检查策略状态,只有停止策略后从可以移除策略;
    • 从json文件移除策略配置信息(strategy_name, vt_symbol, setting);
    • 从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.  
    10. # Remove setting
    11. self.remove_strategy_setting(strategy_name)
    12.  
    13. # Remove from symbol strategy map
    14. strategies = self.symbol_strategy_map[strategy.vt_symbol]
    15. strategies.remove(strategy)
    16.  
    17. # Remove from active orderid map
    18. if strategy_name in self.strategy_orderid_map:
    19. vt_orderids = self.strategy_orderid_map.pop(strategy_name)
    20.  
    21. # Remove vt_orderid strategy map
    22. for vt_orderid in vt_orderids:
    23. if vt_orderid in self.orderid_strategy_map:
    24. self.orderid_strategy_map.pop(vt_orderid)
    25.  
    26. # Remove from strategies
    27. self.strategies.pop(strategy_name)
    28.  
    29. return True