I have created a demo account, and I am trying to receive delayed quotes with the following code, but it failed so far.
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)
As I am working with a demo account, I have to work with delayed data, so that's why I added self.tws.reqMarketDataType(3)
(see that link). My problem is that dl.field4price return an empty list for the SPY
symbol which is impossible. How could I get the SPY stock price in considering the previous code? Did I make an error?