Option Chain Extraction For NSE Stocks Using Python

By Milind Paradkar

We are back again with another post on Python. Our last post, “Basic Operations on Stock data using Python” was well received and we are glad to see the number of likes & shares for the post on various quant trading and Python forums. Keep them coming!

Financial market data is a very critical element of a trading system. Be it historical or live data, you need data for various purposes like analyzing stock behavior, backtesting strategies, for paper trading, and for executing live orders in the market.

One can procure data from paid data vendors or use the free data provided by various financial portals and exchanges. In this post, we will explore a way to scrape web data using Python; specifically, we will see how to extract Option chain data for the stocks listed on the National Stock Exchange of India Ltd. (NSE) using the exchange’s website. Before we jump to the Python code let us have an understanding of an Option chain.

What is an Option Chain?

An option chain is a listing of all the call and put option strike prices along with their premiums for a given maturity period. The Option chain data has two sections; namely the Calls section and the Puts section. For each of the section, we have different headers like Outstanding interest (OI), Change in OI, Volume, Implied Volatility (IV), Last Trade Price (LTP), Net Change, and the quotes for the price of the options (premiums).

We have different Option chain tables for different dates of expiry for the same underlying stock. For example, the table shown below refers to the expiry date of 28th September 2017 for the underlying UBL stock.

Python Code for Extracting Option Chain Table

Step 1: We first import the required libraries; requests, pandas, and BeautifulSoup. We then copy the URL from the NSE site for the option chain of the underlying stock.

We apply the requests.get function from the Python requests library (which is a simple HTTP library) on the URL which creates a response object (named as “page” in the code). One can check whether we were able to successfully execute the function by checking the status code. The contents of the “page” object can be viewed using the content method.

Step 2: In this step, we use the BeautifulSoup library for pulling data from the HTML. Some basic knowledge of HTML is required in this step. We inspect the HTML page to find the location of the option chain table. We apply the “find_all” method on table class and the respective table id.

Step 3: Note that find_all returns a list, so we’ll have to loop through or use list indexing to extract the required content. We first grab the headers of the option chain table. The code for the same is shown below.

The output i.e. the headers of the option chain table are shown below.

Step 4: In this step, we parse the body of the table to grab each row, and extract the values from its columns, and then move on to the next row. There are a few things to note in this section of the code:

  • We have excluded the chart’s columns as these are not required.
  • The values obtained using the get_text method are in the Unicode format and we need to convert them to string type.

The values are saved in the “new_table” data frame and the final table is saved as a CSV file in the Python’s current working directory.

You can download this entire Python script by clicking on the downloadable button provided below.

To use the Option chain data you need a good understanding of options and the strategies that can be formulated using the Option chain data. We will go deep on this topic in our future posts. Do try out the Python code and drop your valuable feedback or suggestions in our comments section.

Next Step

We have covered the complete steps to implement long combo trading strategy in Option trading. The post ‘Trading Options: Long Combo Trading Strategy‘ also includes a sample which will make it easier for you to understand how to practice this strategy. As a bonus we have also shared the Python code to evaluate payoff at different stock prices.

Download Data Files

  • NSE_Options_Chain_Table.py
  • NSE_Options_Chain_Table_for_Python3.5.py

Login to DOWNLOAD these files for FREE!

Existing Users Log In