Reqhistoricaldata In Ibpy Doesn't Return Anything [python]
I am trying to obtain historical data from Interactive Brokers (IB) through Ibpy. I have tried several scripts for this task, which I have adapted from others who indicate that it
Solution 1:
Always implement an error handler and the API will tell you what's wrong. In this case it says use "1 day" for a bar size.
There's no need to sleep. Use nextValidId
to know when the connection is ready. Use the different end methods to know when you're done. historicalDataEnd
doesn't seem to be implemented yet in IBpy so just look for 'finished'
Don't shut off api logging, it would have shown the error as well as all the different messages sent to and from TWS. You can shut off market data in the log file as it's quite a lot. Look for a file 'api.222.Wed.log' in your jts dir.
fromtime import sleep, strftime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
import pandas as pd
import numpy as np
def nextValidId_handler(msg):
print(msg)
inner()
hist = []
def my_hist_data_handler(msg):
print(msg)
if "finished" in msg.date:
print('disconnecting', con.disconnect())
df = df = pd.DataFrame(index=np.arange(0, len(hist)), columns=('date', 'close', 'volume'))
for index, msg in enumerate(hist):
df.loc[index,'date':'volume'] = msg.date, msg.close, msg.volume
print(df )
else:
hist.append(msg)
def error_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection(port=7497,clientId=222)
con.register(error_handler, message.Error)
con.register(nextValidId_handler, message.nextValidId)
con.register(my_hist_data_handler, message.historicalData)
con.connect()
print(con.isConnected())
def inner():
qqqq = Contract()
qqqq.m_secType = "STK"
qqqq.m_symbol = "AAPL"
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART"
endtime = strftime('%Y%m%d %H:%M:%S')
print(endtime)
con.reqHistoricalData(1,qqqq,endtime,"1 W","1 day","MIDPOINT",1,2)
print(con.isConnected())
Post a Comment for "Reqhistoricaldata In Ibpy Doesn't Return Anything [python]"