修复持仓更新bug,在ctpGateway的持仓更新中区分上期所和其他交易所的数据推送区别

This commit is contained in:
chenxy123 2016-06-05 19:31:51 +08:00
parent 5871e7619d
commit 880ee96702

View File

@ -439,6 +439,7 @@ class CtpTdApi(TdApi):
self.sessionID = EMPTY_INT # 会话编号
self.posBufferDict = {} # 缓存持仓数据的字典
self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系
#----------------------------------------------------------------------
def onFrontConnected(self):
@ -638,6 +639,10 @@ class CtpTdApi(TdApi):
self.posBufferDict[positionName] = posBuffer
# 更新持仓缓存并获取VT系统中持仓对象的返回值
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)
@ -731,6 +736,9 @@ class CtpTdApi(TdApi):
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']:
@ -1338,6 +1346,21 @@ class PositionBuffer(object):
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():
"""测试"""