From 8d73c809196041c2fe78178d6197dee50e1ca1ba Mon Sep 17 00:00:00 2001 From: Bian Jiang Date: Thu, 5 May 2016 10:13:59 +0800 Subject: [PATCH] =?UTF-8?q?IB=201.=20=E4=BF=AE=E5=A4=8D=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F,=20=E5=A2=9E=E5=8A=A0=E6=AF=AB=E7=A7=92=202.?= =?UTF-8?q?=20IB=E5=A4=96=E6=B1=87=E6=B2=A1=E6=9C=89=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ibGateway/ibGateway.py | 41 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/vn.trader/ibGateway/ibGateway.py b/vn.trader/ibGateway/ibGateway.py index f0711c78..c7cba073 100644 --- a/vn.trader/ibGateway/ibGateway.py +++ b/vn.trader/ibGateway/ibGateway.py @@ -13,7 +13,7 @@ ibpy的gateway接入 import os import json -from time import sleep, strftime, localtime +from datetime import datetime from copy import copy from PyQt4 import QtGui, QtCore @@ -193,6 +193,7 @@ class IbGateway(VtGateway): tick.exchange = subscribeReq.exchange tick.vtSymbol = '.'.join([tick.symbol, tick.exchange]) tick.gatewayName = self.gatewayName + tick.__setattr__('m_secType', productClassMap.get(subscribeReq.productClass, '')) self.tickDict[self.tickerId] = tick #---------------------------------------------------------------------- @@ -281,6 +282,13 @@ class IbWrapper(EWrapper): tick = self.tickDict[tickerId] key = tickFieldMap[field] tick.__setattr__(key, price) + + # 外汇单独设置时间, tickString 没有返回外汇时间 + if tick.m_secType == 'CASH': + dt_obj = datetime.now() + tick.time = dt_obj.strftime('%H:%M:%S.%f') + tick.date = dt_obj.strftime('%Y%m%d') + # 行情数据更新 newtick = copy(tick) self.gateway.onTick(newtick) @@ -293,7 +301,17 @@ class IbWrapper(EWrapper): if field in tickFieldMap: tick = self.tickDict[tickerId] key = tickFieldMap[field] - tick.__setattr__(key, size) + tick.__setattr__(key, size) + + # 外汇单独设置时间, tickString 没有返回外汇时间 + if tick.m_secType == 'CASH': + dt_obj = datetime.now() + tick.time = dt_obj.strftime('%H:%M:%S.%f') + tick.date = dt_obj.strftime('%Y%m%d') + + # 行情数据更新 + newtick = copy(tick) + self.gateway.onTick(newtick) else: print field @@ -310,12 +328,12 @@ class IbWrapper(EWrapper): #---------------------------------------------------------------------- def tickString(self, tickerId, tickType, value): """行情推送,特殊字段相关""" - if tickType == 45: - lt = localtime(int(value)) + if tickType == 45: + dt_obj = datetime.utcfromtimestamp(int(value)) tick = self.tickDict[tickerId] - tick.time = strftime('%H:%M:%S', lt) - tick.date = strftime('%Y%m%d') + tick.time = dt_obj.strftime('%H:%M:%S.%f') + tick.date = dt_obj.strftime('%Y%m%d') # 这里使用copy的目的是为了保证推送到事件系统中的对象 # 不会被当前的API线程修改,否则可能出现多线程数据同步错误 @@ -439,8 +457,8 @@ class IbWrapper(EWrapper): contract.exchange = contractDetails.m_summary.m_exchange contract.vtSymbol = '.'.join([contract.symbol, contract.exchange]) - contract.name = contractDetails.m_marketName.decode('UTF-8') - + contract.name = contractDetails.m_summary.m_localSymbol.decode('UTF-8') + # 合约类型 if contractDetails.m_summary.m_secType == 'STK': contract.productClass = PRODUCT_EQUITY @@ -545,14 +563,15 @@ class IbWrapper(EWrapper): #---------------------------------------------------------------------- def currentTime(self, time): """ generated source for method currentTime """ - t = strftime('%H:%M:%S', localtime(time)) - + dt_obj = datetime.utcfromtimestamp(time) + t = dt_obj.strftime("%Y-%m-%d %H:%M:%S.%f") + self.connectionStatus = True self.gateway.connected = True log = VtLogData() log.gatewayName = self.gatewayName - log.logContent = (u'IB接口连接成功,当前服务器时间%s' %t) + log.logContent = (u'IB接口连接成功,当前服务器时间 %s' %t) self.gateway.onLog(log) #----------------------------------------------------------------------