How to Fetch Live Option Chain data from NSE India using Python

Do you want to learn how to fetch Live Option Chain Data from NSE (nseindia.com) for automated trading bot, or custom analysis for Option Trading using Python. Then you are at the right place. Just keep reading till the end and you will be able to get your desired Option Chain Live data from NSE India yourself.

What is Option Chain?

An options chain is also known as an options matrix. It is a listing of all available options contracts for a given security or stock or index. It contains all listed puts, calls, their expiration, strike prices, volume and pricing information for a single underlying asset within a given maturity period. The chain can commonly be categorized by expiration date and have 2 segments calls vs puts. In simple words we can say, it provides detailed quote and price information with strike price, expiration date and volume for a underlying security.

Why We Need Option Chain Data?

The option chain matrix allows the trader to evaluate the liquidity and depth of the specific strike. It does not only capture the executed price but also captures real time bid price, ask price, bid quantity and ask quantity. When we combine these, we get a clear view of the depth and the liquidity in each strike and also overall for comparison.

Python Source Code

Code Explanation

  • [Line #1-2] First we need to import few packages. For making http requests we need “requests” package. And to convert JSON data to tabular data frame “pandas” package is used.
  • [Line #4] We are creating “OptionChain” class for our this module.
  • [Line #5-10] In our constructor (__init__) function we are creating some class members (variables). We will use these in our module. This function will accepts 2 parameters. One is symbol and another one is timeout for http request. We are also creating an instance of session, which will store cookies for subsequent requests.
  • [Line #12] Now we are creating a function named fetch_data. This function will accepts 3 parameters. First one is expiry_data for our option chain data. Second is for starting_strike_price, it is the strike price from where we want our option chain data to be returned. And finally the third parameter is number_of_rows, it is the number of rows the data frame would contain.
  • [Line #14-24] In fetch_data function, we are making get request to the NSE India API URL to fetch option chain data using session object created in __init__ function. When we have data from nseindia.com, we try to convert it into pandas data frame. Afterward, we are trying to implement filter on this data frame for expiry_date and strike_price and finally we return this data frame which have our data we need. [Line #13, #25-26] And all this process is surrounded by try, except to handle any exception.
  • [Line #27] If we get any exception, we will generate a new session for next request.
  • [Line #31-33] Now to test our module, we are creating an instance of OptionChain and executing fetch_data function.

Conclusion

Finally if you have reached the end of this post (how to fetch Live Option Chain data from NSE India using Python), as a result you should now be able to fetch the option chain data from NSE India website. Now you can use this script as an API in your program and import it where you want to use this data. If you want to learn more about trading, then stay tune to tutlogic.com or follow my channel on youtube.com/@tutlogic.

Scroll to Top