diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index db6c5add..311981f1 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -667,6 +667,14 @@ class CtaEngine(BaseEngine): 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 @@ -677,9 +685,19 @@ class CtaEngine(BaseEngine): 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) def edit_strategy(self, strategy_name: str, setting: dict): @@ -696,11 +714,15 @@ class CtaEngine(BaseEngine): """ 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 - strategy = self.strategies[strategy_name] strategies = self.symbol_strategy_map[strategy.vt_symbol] strategies.remove(strategy) @@ -710,11 +732,14 @@ class CtaEngine(BaseEngine): # Remove vt_orderid strategy map 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 self.strategies.pop(strategy_name) + return True + def load_strategy_class(self): """ Load strategy class from source code. diff --git a/vnpy/app/cta_strategy/template.py b/vnpy/app/cta_strategy/template.py index 35c9818a..e4f309ff 100644 --- a/vnpy/app/cta_strategy/template.py +++ b/vnpy/app/cta_strategy/template.py @@ -230,13 +230,15 @@ class CtaTemplate(ABC): """ 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): """ 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): """ @@ -309,9 +311,10 @@ class TargetPosTemplate(CtaTemplate): """ Callback of new order data update. """ - if order.status == Status.ALLTRADED or order.status == Status.CANCELLED: - if order.vt_orderid in self.vt_orderids: - self.vt_orderids.remove(order.vt_orderid) + vt_orderid = 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): """""" diff --git a/vnpy/app/cta_strategy/ui/widget.py b/vnpy/app/cta_strategy/ui/widget.py index e4f2df1e..f7ba697d 100644 --- a/vnpy/app/cta_strategy/ui/widget.py +++ b/vnpy/app/cta_strategy/ui/widget.py @@ -239,8 +239,6 @@ class StrategyManager(QtWidgets.QFrame): def edit_strategy(self): """""" - # vt_symbol = self._data["vt_symbol"] - # class_name = self._data["class_name"] strategy_name = self._data["strategy_name"] parameters = self.cta_engine.get_strategy_parameters(strategy_name) @@ -253,8 +251,11 @@ class StrategyManager(QtWidgets.QFrame): def remove_strategy(self): """""" - self.cta_engine.remove_strategy(self.strategy_name) - self.cta_manager.remove_strategy(self.strategy_name) + result = self.cta_engine.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):