我创建了一个demo帐户,我正在尝试使用以下代码接收延迟报价,但到目前为止它已失败。
import re
import ib
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from time import sleep
class Downloader(object):
field4price = ''
def __init__(self):
self.tws = ibConnection('localhost', 7496, 9003)
self.tws.register(self.tickPriceHandler, 'TickPrice')
self.tws.connect()
self._reqId = 5 # current request id
def tickPriceHandler(self,msg):
if msg.field == 4:
self.field4price = msg.price
#print '[debug]', msg
def requestData(self,contract):
self.tws.reqMarketDataType(3)
self.tws.reqMktData(self._reqId, contract, '', 1)
self._reqId+=1
if __name__=='__main__':
dl = Downloader()
c = Contract()
c.m_symbol = 'SPY'
c.m_secType = 'STK'
c.m_exchange = 'SMART'
c.m_currency = 'USD'
dl.requestData(c)
sleep(3)
print('Price - field 4: ', dl.field4price)
在我使用模拟帐户时,我必须处理延迟数据,这就是我添加self.tws.reqMarketDataType(3)
的原因(请参阅link)。我的问题是dl.field4price返回SPY
符号的空列表,这是不可能的。考虑到之前的代码,我如何获得SPY股票价格?我犯了错误吗?
答案 0 :(得分:2)
我不知道你是否想出来了,但IB已经迫使升级,所以我现在正在使用ver 963。我刚刚添加了我建议的内容,并将延迟的请求添加到旧样本中。我使用加拿大股票,因为我没有订阅,但也许这甚至不重要。
from ibapi import wrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper #just for decorator
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import *
class TestApp(wrapper.EWrapper, EClient):
def __init__(self):
wrapper.EWrapper.__init__(self)
EClient.__init__(self, wrapper=self)
self.count = 0
@iswrapper
def nextValidId(self, orderId:int):
print("nextValidOrderId:", orderId)
self.nextValidOrderId = orderId
#here is where you start using api
contract = Contract()
contract.symbol = "RY"
contract.secType = "STK"
contract.currency = "CAD"
contract.exchange = "SMART"
self.reqMarketDataType(3)
self.reqMktData(1101, contract, "", False, None)
@iswrapper
def error(self, reqId:TickerId, errorCode:int, errorString:str):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
@iswrapper
def tickPrice(self, reqId: TickerId , tickType: TickType, price: float,
attrib:TickAttrib):
print("Tick Price. Ticker Id:", reqId,
"tickType:", TickTypeEnum.to_str(tickType),
"Price:", price)
#just disconnect after a bit
self.count += 1
if self.count > 10 : self.disconnect()
#I use jupyter but here is where you use if __name__ == __main__:
app = TestApp()
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % app.serverVersion(),app.twsConnectionTime()))
app.run()
以下是输出的一部分,请注意您可以使用ib的枚举类型来获取刻度类型的名称。
Error. Id: 1101 Code: 10167 Msg: Requested market data is not subscribed. Displaying delayed market data...
Tick Price. Ticker Id: 1101 tickType: DELAYED_BID Price: 97.02
Tick Price. Ticker Id: 1101 tickType: DELAYED_ASK Price: 97.02
Tick Price. Ticker Id: 1101 tickType: DELAYED_LAST Price: 0.0
现在是上午9:40,因为我输入了这个,所以市场是开放的但是还没有延迟15分钟。 DELAYED_LAST为0.0需要过滤掉。我不认为我曾经见过0.0的真实时间,所以要小心。
我等到9点45分才收到
Tick Price. Ticker Id: 1101 tickType: DELAYED_LAST Price: 97.63
准时。
答案 1 :(得分:0)
由于我的开发工作主要在 RTH 之外, 类型 4(最后价格)并不总是可用。 因此,我尝试了类型 9(收盘价)作为替代。 可以看到整个信息here
亚历克斯