Error reading request when retrieving tick data from Interactive Brokers API (Python)

158 Views Asked by At

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!

0

There are 0 best solutions below