IB 1. 修复时间格式, 增加毫秒 2. IB外汇没有时间的问题

This commit is contained in:
Bian Jiang 2016-05-05 10:13:59 +08:00
parent 163fc39f15
commit 8d73c80919

View File

@ -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)
@ -294,6 +302,16 @@ class IbWrapper(EWrapper):
tick = self.tickDict[tickerId]
key = tickFieldMap[field]
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
@ -311,11 +329,11 @@ class IbWrapper(EWrapper):
def tickString(self, tickerId, tickType, value):
"""行情推送,特殊字段相关"""
if tickType == 45:
lt = localtime(int(value))
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,7 +457,7 @@ 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':
@ -545,7 +563,8 @@ 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