Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Option Chain Extraction For NSE Stocks Using Python

By Milind Paradkar

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

If you want to learn various aspects of Algorithmic trading then check out our Executive Programme in Algorithmic Trading (EPAT™). The course covers training modules like Statistics & Econometrics, Financial Computing & Technology, and Algorithmic & Quantitative Trading. EPAT™ is designed to equip you with the right skill sets to be a successful trader. Enroll now!

Download Data Files

  • NSE_Options_Chain_Table.py

Download

The post Option Chain Extraction For NSE Stocks Using Python appeared first on .



This post first appeared on Best Algo Trading Platforms Used In Indian Market, please read the originial post: here

Share the post

Option Chain Extraction For NSE Stocks Using Python

×

Subscribe to Best Algo Trading Platforms Used In Indian Market

Get updates delivered right to your inbox!

Thank you for your subscription

×