From 5580336f4b500903d5df5fdcb02f760eb4a8b3b8 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Wed, 6 May 2020 16:34:04 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA=E5=8A=9F=E8=83=BD]=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=AF=B9=E5=B9=B3=E7=AD=96=E7=95=A5=E5=92=8C?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E4=BB=93=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/cta_crypto/engine.py | 49 ++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/vnpy/app/cta_crypto/engine.py b/vnpy/app/cta_crypto/engine.py index 989a2a99..ceb46b52 100644 --- a/vnpy/app/cta_crypto/engine.py +++ b/vnpy/app/cta_crypto/engine.py @@ -1230,14 +1230,15 @@ class CtaEngine(BaseEngine): self.write_log(f'{strategy_name}返回得K线切片数据为空') return - # 剩下工作:保存本地文件/数据库 - snapshot_folder = get_folder_path(f'data/snapshots/{strategy_name}') - snapshot_file = snapshot_folder.joinpath('{}.pkb2'.format(datetime.now().strftime('%Y%m%d_%H%M%S'))) - with bz2.BZ2File(str(snapshot_file), 'wb') as f: - pickle.dump(snapshot, f) - self.write_log(u'切片保存成功:{}'.format(str(snapshot_file))) + if self.engine_config.get('snapshot2file', False): + # 剩下工作:保存本地文件/数据库 + snapshot_folder = get_folder_path(f'data/snapshots/{strategy_name}') + snapshot_file = snapshot_folder.joinpath('{}.pkb2'.format(datetime.now().strftime('%Y%m%d_%H%M%S'))) + with bz2.BZ2File(str(snapshot_file), 'wb') as f: + pickle.dump(snapshot, f) + self.write_log(u'切片保存成功:{}'.format(str(snapshot_file))) - # 通过事件方式,传导到account_recorder + # 通过事件方式,传导到account_recorder snapshot.update({ 'account_id': self.engine_config.get('accountid', '-'), 'strategy_group': self.engine_config.get('strategy_group', self.engine_name), @@ -1469,7 +1470,7 @@ class CtaEngine(BaseEngine): value = getattr(strategy, parameter, None) return value - def compare_pos(self, strategy_pos_list=[]): + def compare_pos(self, strategy_pos_list=[], auto_balance=False): """ 对比账号&策略的持仓,不同的话则发出微信提醒 :return: @@ -1556,10 +1557,40 @@ class CtaEngine(BaseEngine): compare_info += msg else: pos_compare_result += '\n{}: {}'.format(vt_symbol, json.dumps(symbol_pos, indent=2, ensure_ascii=False)) - self.write_error(u'{}不一致:{}'.format(vt_symbol, json.dumps(symbol_pos, indent=2, ensure_ascii=False))) compare_info += u'{}不一致:{}\n'.format(vt_symbol, json.dumps(symbol_pos, indent=2, ensure_ascii=False)) + diff_volume = round(symbol_pos['账号净仓'], 7) - net_symbol_pos + # 账号仓位> 策略仓位, sell + if diff_volume > 0 and auto_balance: + contract = self.main_engine.get_contract(vt_symbol) + req = OrderRequest( + symbol=contract.symbol, + exchange=contract.exchange, + direction=Direction.SHORT, + offset=Offset.CLOSE, + type=OrderType.MARKET, + price=0, + volume=round(diff_volume,7) + ) + self.write_log(f'卖出{vt_symbol} {req.volume},平衡仓位') + self.main_engine.send_order(req, contract.gateway_name) + + # 账号仓位 < 策略仓位 ,buy + elif diff_volume < 0 and auto_balance: + contract = self.main_engine.get_contract(vt_symbol) + req = OrderRequest( + symbol=contract.symbol, + exchange=contract.exchange, + direction=Direction.LONG, + offset=Offset.OPEN, + type=OrderType.MARKET, + price=0, + volume=round(-diff_volume, 7) + ) + self.write_log(f'买入{vt_symbol} {req.volume},平衡仓位') + self.main_engine.send_order(req, contract.gateway_name) + # 不匹配,输入到stdErr通道 if pos_compare_result != '': msg = u'账户{}持仓不匹配: {}' \