erdewit / ib_insync

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

retrieve historical options data #658

Closed LucaPaterlini closed 8 months ago

LucaPaterlini commented 10 months ago

Hello, I have followed the example of the tutorial page, changed a few lines to get options data instead and I am getting this error instead.

Error 200, reqId 4: No security definition has been found for the request, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20231215', strike=4400, right='C', multiplier='USD', exchange='SMART')

code

from ib_insync import *
# util.startLoop()  # uncomment this line when in a notebook

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

/---- changed line
contract = Option('SPY','20231215', 4400,'C','SMART','USD')
/------
bars = ib.reqHistoricalData(
    contract, endDateTime='', durationStr='30 D',
    barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True)

# convert to pandas dataframe (pandas needs to be installed):
df = util.df(bars)
print(df)
Boblogic07 commented 10 months ago

try this: Strike 4400 doesn't exist for SPY and you used positional argument for 'USD' which is interpreted as multiplier instead of currency.

contract = Option(symbol='SPY',lastTradeDateOrContractMonth='20231215', strike=440,right='C',exchange='SMART',currency='USD')