erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.8k stars 743 forks source link

reqHistoricalTicks - Can't find EId with tickerId:0 #15

Closed josefstr closed 7 years ago

josefstr commented 7 years ago

I'm trying to request historical tick data:

from ib_insync import *
import datetime

ib = IB()
ib.connect('127.0.0.1', 4001, clientId=76)

fdaxContract = Future(localSymbol='FDAX SEP 17', multiplier='25', currency='EUR', exchange='DTB')

start = datetime.datetime(2017, 9, 1, 1, 0, 0)
end = ''
ticks = ib.reqHistoricalTicks(fdaxContract, start, end, 1, 'MIDPOINT', True, False, [])
print(ticks)
print(util.df(ticks))
ERROR:ib_insync.wrapper:Error 300, reqId 0: Can't find EId with tickerId:0
ERROR:ib_insync.client:Peer closed connection

I could request data with:

bars = ib.reqHistoricalData(fdaxContract, endDateTime='', durationStr='30 D', barSizeSetting='1 hour', whatToShow='TRADES', useRTH=True)

any idea what I'm doing wrong?

erdewit commented 7 years ago

For reqHistoricalTicks the latest TWS or gateway is required. If you already have that then try to qualify the contract before doing the request, like

ib.qualifyContracts(fdaxContract)
josefstr commented 7 years ago

from your docs:

(version 967 or higher)

and I was running the latest stable 963. With the update to 967 I can request tick data. Thanks. Do you have plans to introduce some export possibilities for historical data, e.g. to csv?

erdewit commented 7 years ago

Josef, saving to csv is a simple matter of converting to a DataFrame (for example with util.df) and then df.to_csv(...)

josefstr commented 7 years ago

yep I've already found it. another question: looking at my results it seems that the most precise time is seconds: HistoricalTickBidAsk(time=datetime.datetime(2017, 9, 1, 8, 1, 14) but for tick data there should be 3 digits of micros available. something like: M/d/yyyy HH:mm:ss.SSS

erdewit commented 7 years ago

Unfortunately the data is sent with second resolution (unix time in seconds, as integer). This corresponds to the Time & Sales window in TWS.

shevkoplyas commented 7 years ago

Ewald,

Just a side note - when you reqHistoricalData for day bars (and longer) then the time field of bars in the callbacks is no longer epoch seconds time! It still a number, which represents YYYYMMDD. The new java api I use return "bar" object and behind the scene IP converts those YYYYMMDD back to epoch seconds (they become UTC midnight for daybars), so double-check what is returned in ib_insync case for day bars.

Cheers, Dmitry

erdewit commented 7 years ago

Dmitry, the datetime parsing is handled here: https://github.com/erdewit/ib_insync/blob/9cf7bb4fd0dacca0ae2834ac60857deacbde0138/ib_insync/util.py#L307

It also handles the case for intraday bars that have been requested with formatDate=1.