I am trying to build option chain data much like the major exchanges present it on their websites IE: Sort by expiration dates, then strike prices and display the bids/asks for the puts/calls etc.
I am trying to get the data but have only been able to obtain the expiry and strike prices.
I am using their ibapi library with TWS
I have tried using the reqSecDefOptParams()
but keep receiving errors like: Error: 1 321 Error validating request.-'cB' : cause - Invalid security type -
Here is my code:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
class OptionDataApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error:", reqId, errorCode, errorString)
def contractDetails(self, reqId, contractDetails):
contract = contractDetails.contract
print("Symbol:", contract.symbol)
print("Expiry:", contract.lastTradeDateOrContractMonth)
print("Strike Price:", contract.strike)
self.request_option_chain(contract)
def request_option_chain(self, contract):
self.reqSecDefOptParams(
1, contract.exchange, "", contract.tradingClass, contract.conId
)
def contractDetailsEnd(self, reqId):
self.disconnect()
def main():
app = OptionDataApp()
app.connect("127.0.0.1", 7496, clientId=0)
contract = Contract()
contract.symbol = "AAPL" # Specify the symbol for which you want option data
contract.secType = "OPT"
contract.exchange = "SMART"
contract.currency = "USD"
app.reqContractDetails(1, contract)
app.run()
if __name__ == "__main__":
main()
This codes prints what appears to be all the Expiry/Strike prices one after the other like this:
Symbol: AAPL
Expiry: 20230811
Strike Price: 105.0
Symbol: AAPL
Expiry: 20230811
Strike Price: 115.0
And then at the end(with a few mixed in the above data), many errors like this:
Error: 1 321 Error validating request.-'cB' : cause - Invalid security type -
Error: 1 321 Error validating request.-'cB' : cause - Invalid security type -
Error: 1 321 Error validating request.-'cB' : cause - Invalid security type -
Error: 1 321 Error validating request.-'cB' : cause - Invalid security type -
Any help would be appreciated, Thanks