From cf97252314212492af25a2b0309cf9575fbe14ad Mon Sep 17 00:00:00 2001 From: msincenselee Date: Mon, 10 Aug 2020 15:24:15 +0800 Subject: [PATCH] =?UTF-8?q?[bug=20fix]=EF=BC=9B=E8=87=AA=E5=8A=A8=E6=8D=A2?= =?UTF-8?q?=E5=90=88=E7=BA=A6=E7=9A=84bug=E3=80=90=E5=8E=9F=E5=90=88?= =?UTF-8?q?=E7=BA=A6=E4=B8=8D=E8=83=BD=E5=B9=B3=E4=BB=93=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=A4=9A=E6=AC=A1=E5=BC=80=E4=BB=93=E6=96=B0=E5=90=88?= =?UTF-8?q?=E7=BA=A6=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/cta_strategy_pro/engine.py | 4 ++-- vnpy/app/cta_strategy_pro/template.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vnpy/app/cta_strategy_pro/engine.py b/vnpy/app/cta_strategy_pro/engine.py index 5908bd62..ab42ae35 100644 --- a/vnpy/app/cta_strategy_pro/engine.py +++ b/vnpy/app/cta_strategy_pro/engine.py @@ -1519,13 +1519,13 @@ class CtaEngine(BaseEngine): # 新增处理SPD结尾得特殊自定义套利合约 try: - if strategy.vt_symbol.endswith('SPD') and len(pos_list) > 0: + if strategy.vt_symbol.endswith('.SPD') and len(pos_list) > 0: old_pos_list = copy(pos_list) pos_list = [] for pos in old_pos_list: # SPD合约 spd_vt_symbol = pos.get('vt_symbol', None) - if spd_vt_symbol is not None and spd_vt_symbol.endswith('SPD'): + if spd_vt_symbol is not None and spd_vt_symbol.endswith('.SPD'): spd_symbol, spd_exchange = extract_vt_symbol(spd_vt_symbol) spd_setting = self.main_engine.get_all_custom_contracts(rtn_setting=True).get(spd_symbol, None) diff --git a/vnpy/app/cta_strategy_pro/template.py b/vnpy/app/cta_strategy_pro/template.py index 03d3e604..c3237f9e 100644 --- a/vnpy/app/cta_strategy_pro/template.py +++ b/vnpy/app/cta_strategy_pro/template.py @@ -908,6 +908,7 @@ class CtaProTemplate(CtaTemplate): continue if not g.open_status or g.order_status or g.volume - g.traded_volume <= 0: continue + none_mi_grid = g if g.traded_volume > 0 and g.volume - g.traded_volume > 0: g.volume -= g.traded_volume @@ -939,6 +940,12 @@ class CtaProTemplate(CtaTemplate): if len(vt_orderids) > 0: self.write_log(f'切换合约,委托卖出非主力合约{none_mi_symbol}持仓:{none_mi_grid.volume}') + # 已经发生过换月的,不执行买入新合约 + if none_mi_grid.snapshot.get("switched", False): + self.write_log(f'已经执行过换月,不再创建新的买入操作') + return + + none_mi_grid.snapshot.update({'switched': True}) # 添加买入主力合约 grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price}) self.gt.dn_grids.append(grid) @@ -951,9 +958,9 @@ class CtaProTemplate(CtaTemplate): if len(vt_orderids) > 0: self.write_log(u'切换合约,委托买入主力合约:{},价格:{},数量:{}' .format(self.vt_symbol, self.cur_mi_price, grid.volume)) - self.gt.save() else: self.write_error(f'委托买入主力合约:{self.vt_symbol}失败') + self.gt.save() else: self.write_error(f'委托卖出非主力合约:{none_mi_symbol}失败') @@ -1006,7 +1013,12 @@ class CtaProTemplate(CtaTemplate): grid=none_mi_grid) if len(vt_orderids) > 0: self.write_log(f'委托平空非主力合约{none_mi_symbol}持仓:{none_mi_grid.volume}') + # 已经发生过换月的,不执行开空新合约 + if none_mi_grid.snapshot.get("switched", False): + self.write_log(f'已经执行过换月,不再创建新的空操作') + return + none_mi_grid.snapshot.update({'switched': True}) # 添加卖出主力合约 grid.id = str(uuid.uuid1()) grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price}) @@ -1018,9 +1030,9 @@ class CtaProTemplate(CtaTemplate): grid=grid) if len(vt_orderids) > 0: self.write_log(f'委托做空主力合约:{self.vt_symbol},价格:{self.cur_mi_price},数量:{grid.volume}') - self.gt.save() else: self.write_error(f'委托做空主力合约:{self.vt_symbol}失败') + self.gt.save() else: self.write_error(f'委托平空非主力合约:{none_mi_symbol}失败')