0

I am trying to retrieve tick data from Interactive Brokers using the ib_insync library in Python. However, I'm encountering an error when making the request. Here's the code I'm using:

from ib_insync import *
import pandas as pd

def main():
    ib = IB()
    ib.connect('localhost', 4002, clientId=1)

    contract = Contract()
    contract.symbol = "FDAX"
    contract.secType = "FUT"
    contract.exchange = "EUREX"
    contract.currency = "EUR"
    contract.lastTradeDateOrContractMonth = "202306"

    duration = "1 D"
    endDateTime = ""  # Set an empty string to retrieve the most recent ticks

    tick_data = ib.reqHistoricalTicks(
        contract,
        endDateTime,
        duration,
        numberOfTicks=1000000,
        whatToShow="TRADES",
        useRth=False
    )

    df = pd.DataFrame([[t.time, t.price, t.size] for t in tick_data],
                      columns=['Time', 'Price', 'Size'])
    df.to_csv('historical_tick_data.csv', index=False)

    ib.disconnect()

if __name__ == '__main__':
    main()

Error Message:I am receiving the following error when running the code

`Error 320, reqId 4: Error reading request. String index out of range: 0, contract: Contract(secType='FUT', symbol='FDAX', lastTradeDateOrContractMonth='202306', exchange='EUREX', currency='EUR')

`

Expected Behavior:

I expect the code to retrieve the tick data for the specified contract and save it to a CSV file.

I have an active connection to the Interactive Brokers API, and the IB Gateway software is running.

I have tried specifying a valid end date and time, but the error persists.

I would appreciate any insights or suggestions on how to resolve this issue.

Many thanks in advance for your help!

deltaone
  • 1
  • 1
  • Your numberOfTicks value is too high. According to the [documentation](https://interactivebrokers.github.io/tws-api/classIBApi_1_1EClient.html#a99dbcf80c99b8f262a524de64b2f9c1e), the maximum value is 1000. – MatthewScarpino Jun 09 '23 at 19:17
  • Thanks Matthew, Ive reduced the numberOfTicks=100, so well within the limit now but i still get the following error, so if i understand it correctly it looks like there's an issue with Invalid contract details. looking at IB's website its not clear to me what values I should be using. . Error 320, reqId 4: Error reading request. String index out of range: 0, contract: Contract(secType='FUT', symbol='FDAX', lastTradeDateOrContractMonth='202306', exchange='EUREX', currency='EUR') Process finished with exit code 0 – deltaone Jun 09 '23 at 19:29
  • In that case, I'd look for the unique Contract ID on the web and use that instead of other fields of the Contract class. The search page is [here](https://pennies.interactivebrokers.com/cstools/contract_info/v3.10/index.php). – MatthewScarpino Jun 09 '23 at 20:34
  • Mathew,thanks again for your help! Using the unique Contract ID I eventually got it to work – deltaone Jun 10 '23 at 00:58

0 Answers0