Options Data for Indian Markets

NSEpy is a library to extract historical data for Indian Equity, Index, Derivative, and Volatility Index (INDIAVIX). In this notebook, you will learn to fetch options data for the Indian equities and indices.


The notebook is divided into the following parts:

  1. [Install NSEpy Package](#install)

  2. [Options Data for a Particular Strike Price](#same_strike)


You need first to pip install nsepy package to fetch the data. If nsepy module is not installed in your machine, then change the below cell from Markdown to Code and run.


You can use the below command to install the nsepy package in a command prompt or Spyder IDE.

pip install nsepy

Options Data for a Particular Strike Price


You need to use the `get_history` function from the `nsepy` to get the options data.


# For data manipulation
import pandas as pd
import numpy as np

# Import get_history function from nsepy module
from nsepy import get_history

# For manipulating date 
from datetime import date

# For plotting
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('seaborn-darkgrid')

# Get options data for NIFTY index
nifty_options = get_history(symbol='NIFTY',
                            start=date(2021, 2, 7),
                            end=date(2021, 12, 2),
                            index=True,
                            expiry_date=date(2021, 12, 2),
                            option_type='CE',
                            strike_price=17000,
                            )

nifty_options.head()

Output:

From the above output, you can see that NIFTY call options, 17000 strike price data, is fetched for the recent past three months. Each day, you get the 17000 strike price: `Open`, `High`, `Low`, `Close`, `Last` and `Settle Price`. You also get data for `Open Interest` and `Change in OI`, which are important parameters in analyzing options.

fig = plt.figure(figsize=(15, 10))

# Plot close prices
ax = fig.add_subplot(211)
ax.plot(nifty_options['Close'])
ax.set_title('17000 Strike NIFTY Call Close Price', fontsize=14)
ax.set_ylabel('Close Price', fontsize=12)
ax.set_xlabel('Date', fontsize=12)


# Plot the cumulative returns
ax = fig.add_subplot(212)
ax = nifty_options['Open Interest'].plot(kind='bar', color='c')
ax.set_title('17000 Strike NIFTY Call Open Interest', fontsize=14)
ax.set_ylabel('Open Interest', fontsize=12)
ax.set_xlabel('Date', fontsize=12)

plt.show()

Output:

Conclusion


Indian equity and index options data for different strike prices can be fetched using the NSEpy package. To get the list of stocks and indexes available in the Indian market, you can refer to the NSE official website.