对IbGateway进行修改:

1. 股票、期货的tick推送基于成交变化(last price/last time)的更新
2. 外汇的tick推送基于bid-ask价格变动(last price/last time使用本地时间)
3. 连接成功后会自动订阅所有可交易账户的数据更新(而不只是当前账户)
This commit is contained in:
chenxy123 2016-12-13 22:35:28 +08:00
parent d1f196a4aa
commit 3373038763

View File

@ -170,9 +170,6 @@ class IbGateway(VtGateway):
# 查询服务器时间
self.api.reqCurrentTime()
# 请求账户数据主推更新
self.api.reqAccountUpdates(True, self.accountCode)
#----------------------------------------------------------------------
def subscribe(self, subscribeReq):
@ -352,20 +349,23 @@ class IbWrapper(IbApi):
def tickPrice(self, tickerId, field, price, canAutoExecute):
"""行情价格相关推送"""
if field in tickFieldMap:
# 对于股票、期货等行情有新价格推送时仅更新tick缓存
# 只有当发生成交后tickString更新最新成交价时才推送新的tick
# 即bid/ask的价格变动并不会触发新的tick推送
tick = self.tickDict[tickerId]
key = tickFieldMap[field]
tick.__setattr__(key, price)
# IB的外汇行情没有成交价和时间通过本地计算生成同时立即推送
if self.tickProductDict[tickerId] == PRODUCT_FOREX:
tick.lastPrice = (tick.bidPrice1 + tick.askPrice1) / 2
dt = datetime.now()
tick.time = dt.strftime('%H:%M:%S.%f')
tick.date = dt.strftime('%Y%m%d')
dt = datetime.now()
tick.time = dt.strftime('%H:%M:%S.%f')
tick.date = dt.strftime('%Y%m%d')
# 行情数据更新
newtick = copy(tick)
self.gateway.onTick(newtick)
# 行情数据更新
newtick = copy(tick)
self.gateway.onTick(newtick)
else:
print field
@ -375,15 +375,7 @@ class IbWrapper(IbApi):
if field in tickFieldMap:
tick = self.tickDict[tickerId]
key = tickFieldMap[field]
tick.__setattr__(key, size)
dt = datetime.now()
tick.time = dt.strftime('%H:%M:%S.%f')
tick.date = dt.strftime('%Y%m%d')
# 行情数据更新
newtick = copy(tick)
self.gateway.onTick(newtick)
tick.__setattr__(key, size)
else:
print field
@ -399,8 +391,17 @@ class IbWrapper(IbApi):
#----------------------------------------------------------------------
def tickString(self, tickerId, tickType, value):
""""""
pass
"""行情补充信息相关推送"""
# 如果是最新成交时间戳更新
if tickType == '45':
tick = self.tickDict[tickerId]
dt = datetime.fromtimestamp(value)
tick.time = dt.strftime('%H:%M:%S.%f')
tick.date = dt.strftime('%Y%m%d')
newtick = copy(tick)
self.gateway.onTick(newtick)
#----------------------------------------------------------------------
def tickEFP(self, tickerId, tickType, basisPoints, formattedBasisPoints, totalDividends, holdDays, futureLastTradeDate, dividendImpact, dividendsToLastTradeDate):
@ -591,8 +592,12 @@ class IbWrapper(IbApi):
#----------------------------------------------------------------------
def managedAccounts(self, accountsList):
""""""
pass
"""推送管理账户的信息"""
l = accountsList.split(',')
# 请求账户数据主推更新
for account in l:
self.reqAccountUpdates(True, account)
#----------------------------------------------------------------------
def receiveFA(self, pFaDataType, cxml):