I am trying to use IBpy to return historical data from some instruments, but when I try the code in the documentation I get an empty result.
I managed to make it work using R Ibroker but I would really prefer to have it working using the Python API.
Here is the code I am testing.
from time import sleep, strftime
from time import sleep
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
def my_account_handler(msg):
print(msg)
def my_tick_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection()
con.register(my_account_handler, 'UpdateAccountValue')
con.register(my_tick_handler, message.tickSize, message.tickPrice)
con.connect()
def inner():
qqqq = Contract()
qqqq.m_secType = "CASH"
qqqq.m_symbol = "MSFT"
qqqq.m_currency = "USD"
qqqq.m_exchange = "IDEALPRO"
endtime = strftime('%Y%m%d %H:%M:%S')
con.reqHistoricalData(1,qqqq,endtime,"5 D","1 hour","MIDPOINT",1,1)
sleep(10)
inner()
sleep(5)
print('disconnected', con.disconnect())
Any idea what might be going wrong?
You need to register for the historical data message.
con.register(my_hist_data_handler, message.historicalData)
Then define whatever you want to do with it
def my_hist_data_handler(msg):
print(msg)
Also note MSFT (or QQQ) is a stock
qqqq.m_secType = "STK" #cash is for forex
qqqq.m_symbol = "MSFT" #use less confusing var name
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART" #for stocks usually