From ec75d5ffc55115182b4e7688c6e40de5c9a6e645 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Mon, 3 Feb 2020 14:17:42 +0800 Subject: [PATCH] [bug fix] --- vnpy/app/cta_strategy_pro/engine.py | 27 +++++++++++++------ .../app/cta_strategy_pro/portfolio_testing.py | 6 ++--- vnpy/app/cta_strategy_pro/template.py | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/vnpy/app/cta_strategy_pro/engine.py b/vnpy/app/cta_strategy_pro/engine.py index f503aa1e..0281c655 100644 --- a/vnpy/app/cta_strategy_pro/engine.py +++ b/vnpy/app/cta_strategy_pro/engine.py @@ -825,7 +825,12 @@ class CtaEngine(BaseEngine): level=logging.CRITICAL) def add_strategy( - self, class_name: str, strategy_name: str, vt_symbol: str, setting: dict + self, class_name: str, + strategy_name: str, + vt_symbol: str, + setting: dict, + auto_init: bool = False, + auto_start: bool = False ): """ Add a new strategy. @@ -858,8 +863,8 @@ class CtaEngine(BaseEngine): self.put_strategy_event(strategy) # 判断设置中是否由自动初始化和自动启动项目 - if setting.get('auto_init', False): - self.init_strategy(strategy_name, auto_start=setting.get('auto_start', False)) + if auto_init: + self.init_strategy(strategy_name, auto_start=auto_start) def init_strategy(self, strategy_name: str, auto_start: bool = False): """ @@ -1315,10 +1320,12 @@ class CtaEngine(BaseEngine): for strategy_name, strategy_config in self.strategy_setting.items(): self.add_strategy( - strategy_config["class_name"], - strategy_name, - strategy_config["vt_symbol"], - strategy_config["setting"] + class_name=strategy_config["class_name"], + strategy_name=strategy_name, + vt_symbol=strategy_config["vt_symbol"], + setting=strategy_config["setting"], + auto_init=strategy_config.get('auto_init', False), + auto_start=strategy_config.get('auto_start', False) ) def update_strategy_setting(self, strategy_name: str, setting: dict): @@ -1327,10 +1334,14 @@ class CtaEngine(BaseEngine): """ strategy = self.strategies[strategy_name] + strategy_config = self.strategy_setting.get('strategy_name', {}) + self.strategy_setting[strategy_name] = { "class_name": strategy.__class__.__name__, "vt_symbol": strategy.vt_symbol, - "setting": setting, + "auto_init": strategy_config.get('auto_init', False), + "auto_start": strategy_config.get('auto_start', False), + "setting": setting } save_json(self.setting_filename, self.strategy_setting) diff --git a/vnpy/app/cta_strategy_pro/portfolio_testing.py b/vnpy/app/cta_strategy_pro/portfolio_testing.py index 6c03dbbe..65240297 100644 --- a/vnpy/app/cta_strategy_pro/portfolio_testing.py +++ b/vnpy/app/cta_strategy_pro/portfolio_testing.py @@ -854,12 +854,12 @@ class PortfolioTestingEngine(object): if 'auto_start' in strategy_setting: strategy_setting.update({'auto_start': False}) - # 强制更新回测为True - strategy_setting.update({'backtesting': True}) - # 策略参数设置 setting = strategy_setting.get('setting', {}) + # 强制更新回测为True + setting.update({'backtesting': True}) + # 创建实例 strategy = strategy_class(self, strategy_name, vt_symbol, setting) diff --git a/vnpy/app/cta_strategy_pro/template.py b/vnpy/app/cta_strategy_pro/template.py index 049866ee..299aed48 100644 --- a/vnpy/app/cta_strategy_pro/template.py +++ b/vnpy/app/cta_strategy_pro/template.py @@ -874,7 +874,7 @@ class CtaProTemplate(CtaTemplate): # 找出非主力合约的持仓网格 for g in self.gt.get_opened_grids(direction=Direction.SHORT): - none_mi_symbol = g.snapshot.get('miSymbol') + none_mi_symbol = g.snapshot.get('mi_symbol') if none_mi_symbol is None or none_mi_symbol == self.vt_symbol: continue if not g.open_status or g.order_status or g.volume - g.traded_volume <= 0: