How to extract India Stock Market Data in Python in the Easiest way
In a world where data is the new oil, it is very important to obtain clean data. The national stock exchange (NSE) provides clean data of the Indian stock market and we can easily extract historical data using “nsepy” library. There are other libraries for extracting the data but “nsepy” is the easy one for extracting the data.
You can obtain below Historical data from nsepy (“nsepy provides only historical data not live data”)
- Stock (Equity data)
- Index data
- Stock option data
- Stock future data
- Index option data
- Index future data
- India Vix (Volatility data)
- Expiry dates in a year and month
- PE Ratio
How to extract data:
Step1:- The first step is to install the nsepy library into your system
pip install nsepy
You can use command-line interface or Conda installer to install nsepy
Step 2:- After installing nsepy you can import the library in jupyter notebook
from datetime import datetime
from nsepy import get_history
Now we can start
- Stock (Equity data): To extract historical data of any equity we need three parameters:-
a. Equity Symbol
b. Start date for extraction
c. End date for extraction
Let's extract the data for Reliance from 1st Jan 2021 to 15th Jan 2021
Rel = get_history(symbol='RELIANCE',
start=date(2021,1,1),
end=date(2021,1,15))
You will get an output like this
As the output is in pandas data frame we can further analyze the data using pandas and NumPy
2. Index data: To extract historical data of any equity we need four parameters:-
a. Index Symbol
b. Start date for extraction
c. End date for extraction
d. index = True (we are just telling the extractor to get the data for index)
Let’s extract the data for NIFTY from 1st Jan 2021 to 15th Jan 2021
nifty_50 = get_history(symbol="NIFTY 50",
start=date(2021,1,1),
end=date(2021,1,15),
index=True)
You will get output like below
3. Stock options data: To extract historical data of any equity we need six parameters:-
a. Equity Symbol
b. Start date for extraction
c. End date for extraction
d. Option type:- CE for the call option, PE for the put option
e. Strike Price:- The price at which option can exercised
f. Expiry date:- The date on which option can be exercised
Let’s extract the data for Reliance call option data for the strike price of 2000 from 1st Jan 2021 to 15th Jan 2021 for expiry on 28th Jan 2021
rel_opt = get_history(symbol="RELIANCE",
start=date(2021,1,1),
end=date(2021,1,15),
option_type="CE",
strike_price=2000,
expiry_date=date(2021,1,28))
you will get the output like below
4. Stock future data: To extract historical data of any equity we need five parameters:-
a. Equity Symbol
b. Start date for extraction
c. End date for extraction
d. Futures =True: Telling the extractor to extract future data
f. Expiry date:- The date on which future is going to expire
Let’s extract the data for Reliance future data from 1st Jan 2021 to 15th Jan 2021 for expiry on 28th Jan 2021
rel_fut = get_history(symbol="RELIANCE",
start=date(2021,1,1),
end=date(2021,1,15),
futures=True,
expiry_date=date(2021,1,28))
5. Index options data: To extract historical data of any equity we need six parameters:-
a. Index Symbol
b. Start date for extraction
c. End date for extraction
d. Index=True (To ensure that we pulling data for index)
e. Option type:- CE for the call option, PE for the put option
f. Strike Price:- The price at which option can exercised
g. Expiry date:- The date on which option can be exercised
Let’s extract the data for Index option data for the call options strike price of 14000 from 1st Jan 2021 to 15th Jan 2021 for expiry on 28th Jan 2021
nifty_opt = get_history(symbol="NIFTY",
start=date(2021,1,1),
end=date(2021,1,15),
index=True,
option_type='CE',
strike_price=14000,
expiry_date=date(2021,1,28))
6. Index future data: To extract historical data of any equity we need five parameters:-
a. Equity Symbol
b. Start date for extraction
c. End date for extraction
d. Futures =True: Telling the extractor to extract future data
e. Index=True (To ensure that we pulling data for index)
f. Expiry date:- The date on which future is going to expire
Let’s extract the data for Index future data from 1st Jan 2021 to 15th Jan 2021 for expiry on 28th Jan 2021
7. India Vix (Volatility data) Volatility data be very useful to make strategies. We can extract volatility data using four parameters:-
a. Equity Symbol :- INDIAVIX
b. Start date for extraction
c. End date for extraction
d. Index=True (To ensure that we pulling data for index)
Please comment if you want to know to extract real-time data from nifty.