[Fix]add more state control in cta engine

This commit is contained in:
vn.py 2019-04-01 14:54:06 +08:00
parent c143080341
commit 466eac66cf
3 changed files with 40 additions and 11 deletions

View File

@ -667,6 +667,14 @@ class CtaEngine(BaseEngine):
Start a strategy. Start a strategy.
""" """
strategy = self.strategies[strategy_name] 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) self.call_strategy_func(strategy, strategy.on_start)
strategy.trading = True strategy.trading = True
@ -677,9 +685,19 @@ class CtaEngine(BaseEngine):
Stop a strategy. Stop a strategy.
""" """
strategy = self.strategies[strategy_name] 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) self.call_strategy_func(strategy, strategy.on_stop)
# Change trading status of strategy to False
strategy.trading = False strategy.trading = False
# Cancel all orders of the strategy
self.cancel_all(strategy)
# Update GUI
self.put_strategy_event(strategy) self.put_strategy_event(strategy)
def edit_strategy(self, strategy_name: str, setting: dict): def edit_strategy(self, strategy_name: str, setting: dict):
@ -696,11 +714,15 @@ class CtaEngine(BaseEngine):
""" """
Remove a strategy. Remove a strategy.
""" """
strategy = self.strategies[strategy_name]
if strategy.trading:
self.write_log(f"策略{strategy.strategy_name}移除失败,请先停止")
return
# Remove setting # Remove setting
self.remove_strategy_setting(strategy_name) self.remove_strategy_setting(strategy_name)
# Remove from symbol strategy map # Remove from symbol strategy map
strategy = self.strategies[strategy_name]
strategies = self.symbol_strategy_map[strategy.vt_symbol] strategies = self.symbol_strategy_map[strategy.vt_symbol]
strategies.remove(strategy) strategies.remove(strategy)
@ -710,11 +732,14 @@ class CtaEngine(BaseEngine):
# Remove vt_orderid strategy map # Remove vt_orderid strategy map
for vt_orderid in vt_orderids: for vt_orderid in vt_orderids:
self.orderid_strategy_map.pop(vt_orderid) if vt_orderid in self.orderid_strategy_map:
self.orderid_strategy_map.pop(vt_orderid)
# Remove from strategies # Remove from strategies
self.strategies.pop(strategy_name) self.strategies.pop(strategy_name)
return True
def load_strategy_class(self): def load_strategy_class(self):
""" """
Load strategy class from source code. Load strategy class from source code.

View File

@ -230,13 +230,15 @@ class CtaTemplate(ABC):
""" """
Put an strategy data event for ui update. Put an strategy data event for ui update.
""" """
self.cta_engine.put_strategy_event(self) if self.inited:
self.cta_engine.put_strategy_event(self)
def send_email(self, msg): def send_email(self, msg):
""" """
Send email to default receiver. Send email to default receiver.
""" """
self.cta_engine.send_email(msg, self) if self.inited:
self.cta_engine.send_email(msg, self)
def sync_data(self): def sync_data(self):
""" """
@ -309,9 +311,10 @@ class TargetPosTemplate(CtaTemplate):
""" """
Callback of new order data update. Callback of new order data update.
""" """
if order.status == Status.ALLTRADED or order.status == Status.CANCELLED: vt_orderid = order.vt_orderid
if order.vt_orderid in self.vt_orderids:
self.vt_orderids.remove(order.vt_orderid) if not order.is_active() and vt_orderid in self.vt_orderids:
self.vt_orderids.remove(vt_orderid)
def set_target_pos(self, target_pos): def set_target_pos(self, target_pos):
"""""" """"""

View File

@ -239,8 +239,6 @@ class StrategyManager(QtWidgets.QFrame):
def edit_strategy(self): def edit_strategy(self):
"""""" """"""
# vt_symbol = self._data["vt_symbol"]
# class_name = self._data["class_name"]
strategy_name = self._data["strategy_name"] strategy_name = self._data["strategy_name"]
parameters = self.cta_engine.get_strategy_parameters(strategy_name) parameters = self.cta_engine.get_strategy_parameters(strategy_name)
@ -253,8 +251,11 @@ class StrategyManager(QtWidgets.QFrame):
def remove_strategy(self): def remove_strategy(self):
"""""" """"""
self.cta_engine.remove_strategy(self.strategy_name) result = self.cta_engine.remove_strategy(self.strategy_name)
self.cta_manager.remove_strategy(self.strategy_name)
# Only remove strategy gui manager if it has been removed from engine
if result:
self.cta_manager.remove_strategy(self.strategy_name)
class DataMonitor(QtWidgets.QTableWidget): class DataMonitor(QtWidgets.QTableWidget):