[Fix]add more state control in cta engine
This commit is contained in:
parent
c143080341
commit
466eac66cf
@ -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:
|
||||
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.
|
||||
|
@ -230,12 +230,14 @@ class CtaTemplate(ABC):
|
||||
"""
|
||||
Put an strategy data event for ui update.
|
||||
"""
|
||||
if self.inited:
|
||||
self.cta_engine.put_strategy_event(self)
|
||||
|
||||
def send_email(self, msg):
|
||||
"""
|
||||
Send email to default receiver.
|
||||
"""
|
||||
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):
|
||||
""""""
|
||||
|
@ -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,7 +251,10 @@ class StrategyManager(QtWidgets.QFrame):
|
||||
|
||||
def remove_strategy(self):
|
||||
""""""
|
||||
self.cta_engine.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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user