Nano - Course - On - Python - Thread - by - Indian - Quant - Jan 22, 22 - From - Rattibha
Nano - Course - On - Python - Thread - by - Indian - Quant - Jan 22, 22 - From - Rattibha
@indian_quant
In this post, I will attempt to show a simple Python code to download the past 5
years' Nifty50 stocks EOD Data using the NSEpy library in less than 2 minutes.
The first few lines are simple to import statements. Pulling public library NSEpy
that we can use to extract historical and real-time data from NSE’s website. Then, we
import the most popular pandas library.
https://nsepy.xyz/
Then I define a function "fetchData" that fetches the data using the built-in
get_history function of the nsepy library. I adjust the original data frame to store
open, high, low and close prices. The original data frame sends more columns -
Volume, Turnover, %Deliverable, etc.
I then go ahead and put all Nifty50 tickers in a Python list named nifty50list. I also
set our start-date (2014-01-01), end-date(2022-01-21) in the YYYY-MM-DD format,
and the path where I want to save my files on my PC.
Finally, I loop through all the Nifty50 tickers calling the fetchData function on each
of them and saving them in the path specified above in the CSV format e.g.
ADANIPORTS.csv, which came first. This process is repeated for each and every
symbol in Nifty50.
The result is 2 minutes to download 50 stocks worth of data! Each ticker's data is
saved in a file named Ticker.csv You can visit this colab notebook and try out your
hands with the code
https://colab.research.google.com/drive/1u6gqWQ0Ozl84ULBEm_sRqWiPnXWEZp8Z?
usp=sharing
In Module 3, we will learn to use this data to create and test some trading strategies.
Until then, take care and have a great time learning.
Note: NSE website official Terms of Use section 22.f reads as below:
The Site may be used only for lawful purposes by individuals for personal, non-
commercial use only. NSE specifically prohibits any other use of the Site.