From 880ee9670299d97d045d2fee4b97d6527f056498 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Sun, 5 Jun 2016 19:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8C=81=E4=BB=93=E6=9B=B4?= =?UTF-8?q?=E6=96=B0bug=EF=BC=8C=E5=9C=A8ctpGateway=E7=9A=84=E6=8C=81?= =?UTF-8?q?=E4=BB=93=E6=9B=B4=E6=96=B0=E4=B8=AD=E5=8C=BA=E5=88=86=E4=B8=8A?= =?UTF-8?q?=E6=9C=9F=E6=89=80=E5=92=8C=E5=85=B6=E4=BB=96=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E6=89=80=E7=9A=84=E6=95=B0=E6=8D=AE=E6=8E=A8=E9=80=81=E5=8C=BA?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctpGateway/ctpGateway.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/vn.trader/ctpGateway/ctpGateway.py b/vn.trader/ctpGateway/ctpGateway.py index 2a21dea9..3fae581b 100644 --- a/vn.trader/ctpGateway/ctpGateway.py +++ b/vn.trader/ctpGateway/ctpGateway.py @@ -439,6 +439,7 @@ class CtpTdApi(TdApi): self.sessionID = EMPTY_INT # 会话编号 self.posBufferDict = {} # 缓存持仓数据的字典 + self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系 #---------------------------------------------------------------------- def onFrontConnected(self): @@ -638,7 +639,11 @@ class CtpTdApi(TdApi): self.posBufferDict[positionName] = posBuffer # 更新持仓缓存,并获取VT系统中持仓对象的返回值 - pos = posBuffer.updateBuffer(data) + exchange = self.symbolExchangeDict.get(data['InstrumentID'], EXCHANGE_UNKNOWN) + if exchange == EXCHANGE_SHFE: + pos = posBuffer.updateShfeBuffer(data) + else: + pos = posBuffer.updateBuffer(data) self.gateway.onPosition(pos) #---------------------------------------------------------------------- @@ -730,6 +735,9 @@ class CtpTdApi(TdApi): contract.optionType = OPTION_CALL elif data['OptionsType'] == '2': contract.optionType = OPTION_PUT + + # 缓存代码和交易所的印射关系 + self.symbolExchangeDict[contract.symbol] = contract.exchange # 推送 self.gateway.onContract(contract) @@ -1313,8 +1321,8 @@ class PositionBuffer(object): self.pos = pos #---------------------------------------------------------------------- - def updateBuffer(self, data): - """更新缓存,返回更新后的持仓数据""" + def updateShfeBuffer(self, data): + """更新上期所缓存,返回更新后的持仓数据""" # 昨仓和今仓的数据更新是分在两条记录里的,因此需要判断检查该条记录对应仓位 # 因为今仓字段TodayPosition可能变为0(被全部平仓),因此分辨今昨仓需要用YdPosition字段 if data['YdPosition']: @@ -1337,6 +1345,21 @@ class PositionBuffer(object): self.pos.price = 0 return copy(self.pos) + + #---------------------------------------------------------------------- + def updateBuffer(self, data): + """更新其他交易所的缓存,返回更新后的持仓数据""" + # 其他交易所并不区分今昨,因此只关心总仓位,昨仓设为0 + self.pos.position = data['Position'] + self.pos.ydPosition = 0 + + if data['Position']: + self.pos.price = data['PositionCost'] / data['Position'] + else: + self.pos.price = 0 + + return copy(self.pos) + #---------------------------------------------------------------------- def test():