¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Option Chain (Strike/Bid/Ask) into Panda


 

Good afternoon,

You'll have to excuse my complete ignorance when it comes to matters of the TWS API; I am a rank beginner looking to learn. I have gone through the series of short videos posted by IB on the TWS Python API and have been able to replicate all those results in PyCharm.

Now I am trying to pull options data for a particular underlying/expiration/type(Put or Call) and store the Strike, Bid, and Ask in a Panda. But I am having trouble figuring out how to accomplish this on my own.?

Any guidance is greatly appreciated. Thank you.


Mischa Bertrand-Chetty
 

It would help if you show what you've tried, what you have so far. Have you managed to get the data you want yet?
What part of storing the data are you having trouble with? etc.


 

All perfectly reasonable questions. And questions that I am quite frankly embarassed to have to address, due to my complete lack of knowledge on the subject. But below is my honest best-efforts attempt:

I am using contractDetails and reqContractDetails to see what strikes are listed for a particular Underlying/Expiration/Right (Call or Put)/Multiplier. From what I can tell, contractDetails does not respond with a specific field/callback for "strike" but instead the strike information is contained in the callback contractDetails.contract. So for starters, I am having trouble parsing out just the strike portion of that and storing that information.

Second question - I have gathered that reqContractDetails does not provide any information related to market price (please correct me if I am wrong). In this case, I imagine one would have to follow up the reqContractDetails request with a series of reqMktData requests to get Bid/Ask prices for all the available options that you identified in step one. Is there any way to request all this market data at once? Or would one have to write a loop and do a separate reqMktData request for each contract separately.

def contractDetails(self, reqId, contractDetails):
print("ContractDetails: ", reqId, " ", contractDetails.contract, "\n")
self.data.append([contractDetails.contract])

def start(self):

contract = Contract()
contract.symbol = "AVGO"
contract.secType = "OPT"
contract.exchange = "SMART"
contract.currency = "USD"
contract.lastTradeDateOrContractMonth = "202101"
#contract.strike = 480
contract.right = "C"
contract.multiplier = "100"
#contract.primaryExchange = "NASDAQ"

self.reqContractDetails(1, contract)


Mischa Bertrand-Chetty
 

"contractDetails does not respond with a specific field/callback for "strike" but instead the strike information is contained in the callback contractDetails"
-correct

"I am having trouble parsing out just the strike portion of that and storing that information"
Ya took me some time too.?
So you can get details from "contractDetails()" by using the "contract" that you defined in start():

```
def contractDetails(self, reqId, desc):
? ? print(desc.contract.strike)
```

"reqContractDetails does not provide any information related to market price"
-correct

"one would have to follow up the reqContractDetails request with a series of reqMktData requests to get Bid/Ask prices"
-correct

"Or would one have to write a loop and do a separate reqMktData request for each contract separately"
-Yes

fyi Mathew Scarpino has written this code, and has answered my questions on how to do this, here or on StackOverflow.
I basically took his code (from his book) and have changed it a bit.

?


 

Excellent. Thank you for the detailed response. Mathew Scarpino's book is on my Christmas list, so hopefully I'll have a copy in my hands by month-end.