From 3373038763d91e75d4cf214fe151d5aaa1a1e754 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Tue, 13 Dec 2016 22:35:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9IbGateway=E8=BF=9B=E8=A1=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=9A=201.=20=E8=82=A1=E7=A5=A8=E3=80=81=E6=9C=9F?= =?UTF-8?q?=E8=B4=A7=E7=9A=84tick=E6=8E=A8=E9=80=81=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E6=88=90=E4=BA=A4=E5=8F=98=E5=8C=96=EF=BC=88last=20price/last?= =?UTF-8?q?=20time=EF=BC=89=E7=9A=84=E6=9B=B4=E6=96=B0=202.=20=E5=A4=96?= =?UTF-8?q?=E6=B1=87=E7=9A=84tick=E6=8E=A8=E9=80=81=E5=9F=BA=E4=BA=8Ebid-a?= =?UTF-8?q?sk=E4=BB=B7=E6=A0=BC=E5=8F=98=E5=8A=A8=EF=BC=88last=20price/las?= =?UTF-8?q?t=20time=E4=BD=BF=E7=94=A8=E6=9C=AC=E5=9C=B0=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=89=203.=20=E8=BF=9E=E6=8E=A5=E6=88=90=E5=8A=9F=E5=90=8E?= =?UTF-8?q?=E4=BC=9A=E8=87=AA=E5=8A=A8=E8=AE=A2=E9=98=85=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E4=BA=A4=E6=98=93=E8=B4=A6=E6=88=B7=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9B=B4=E6=96=B0=EF=BC=88=E8=80=8C=E4=B8=8D=E5=8F=AA?= =?UTF-8?q?=E6=98=AF=E5=BD=93=E5=89=8D=E8=B4=A6=E6=88=B7=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ibGateway/ibGateway.py | 51 ++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/vn.trader/ibGateway/ibGateway.py b/vn.trader/ibGateway/ibGateway.py index 1d39e098..b0000fa8 100644 --- a/vn.trader/ibGateway/ibGateway.py +++ b/vn.trader/ibGateway/ibGateway.py @@ -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):