[Add] init cta strategy in thread to avoid ui block

This commit is contained in:
vn.py 2019-02-17 15:00:02 +08:00
parent 9df1cf9a74
commit ae78a7f0c0
2 changed files with 48 additions and 21 deletions

View File

@ -8,6 +8,8 @@ from collections import defaultdict
from pathlib import Path from pathlib import Path
from typing import Any, Callable from typing import Any, Callable
from datetime import datetime, timedelta from datetime import datetime, timedelta
from threading import Thread
from queue import Queue, Empty
from vnpy.event import Event, EventEngine from vnpy.event import Event, EventEngine
from vnpy.trader.engine import BaseEngine, MainEngine from vnpy.trader.engine import BaseEngine, MainEngine
@ -64,6 +66,9 @@ class CtaEngine(BaseEngine):
self.stop_order_count = 0 # for generating stop_orderid self.stop_order_count = 0 # for generating stop_orderid
self.stop_orders = {} # stop_orderid: stop_order self.stop_orders = {} # stop_orderid: stop_order
self.init_thread = None
self.init_queue = Queue()
def init_engine(self): def init_engine(self):
""" """
""" """
@ -406,30 +411,52 @@ class CtaEngine(BaseEngine):
""" """
Init a strategy. Init a strategy.
""" """
strategy = self.strategies[strategy_name] self.init_queue.put(strategy_name)
# Call on_init function of strategy if not self.init_thread:
self.call_strategy_func(strategy, strategy.on_init) self.init_thread = Thread(target=self._init_strategy)
self.init_thread.start()
# Restore strategy data(variables) def _init_strategy(self):
data = self.strategy_data.get(strategy_name, None) """
if data: Init strategies in queue.
for name in strategy.variables: """
value = data.get(name, None) while not self.init_queue.empty():
if value: strategy_name = self.init_queue.get()
setattr(strategy, name, value) strategy = self.strategies[strategy_name]
# Subscribe market data if strategy.inited:
contract = self.main_engine.get_contract(strategy.vt_symbol) self.write_log(f"{strategy_name}已经完成初始化,禁止重复操作")
if contract: continue
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)
strategy.inited = True self.write_log(f"{strategy_name}开始执行初始化")
self.put_strategy_event(strategy)
# 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
def start_strategy(self, strategy_name: str): def start_strategy(self, strategy_name: str):
""" """

View File

@ -173,7 +173,7 @@ class StrategyManager(QtWidgets.QFrame):
def init_ui(self): def init_ui(self):
"""""" """"""
self.setMaximumHeight(200) self.setMaximumHeight(300)
self.setFrameShape(self.Box) self.setFrameShape(self.Box)
self.setLineWidth(1) self.setLineWidth(1)