Thursday, April 23, 2020

Python - How to get the NSE daily data feed?

These are days when trading decisions are taken more by a software, and people write algorithms to get their trades executed. For all this to happen, you need to have the stock data feed with you. Data is nothing but the daily open,close,high, low and volume for the stocks. Once you have the feed, the kind of things you can do with that data is endless.  In this article, we will see how to download the data feed through a Python program.

1. Install pandas if not present already:
pip install pandas
2. Alpha Vantage has exposed free API's for downloading data feed. Install the alpha vantage package:
pip install alpha-vantage
3. You need to register yourself in Alpha vantage and get an API key.

Below is the Python program,, to get the feed:

Changes you should do in the script:
1. In Line 1, replace the she-bang line with the appropriate one as present in your system.
2. In Line 7, replace the path in stock_dir to that location in which you want to save your feed files.
3. In Line 8, replace the file nifty50list.dat along with path with a file which contains the list of stock names for which you want to get the data feed. One stock per line. Sample below:
$ head -3 /home/guru/history/config/nifty50list.dat
4. In Line 10, replace the value of key with the value you got during registration with alpha-vantage.

Execute the Python script:
Lets run the program, to begin with you can just have one or two stocks in the config file.
$ python
Fetching for the stock NSE:ADANIPORTS
Completed for the stock NSE:ADANIPORTS in /home/guru/history/stock/adaniports.csv
Fetching for the stock NSE:ASIANPAINT
Completed for the stock NSE:ASIANPAINT in /home/guru/history/stock/asianpaint.csv
Fetching for the stock NSE:AXISBANK
Completed for the stock NSE:AXISBANK in /home/guru/history/stock/axisbank.csv
Checking the content of one of the feed files:
$ head -3 /home/guru/history/stock/axisbank.csv
$ tail -3 /home/guru/history/stock/axisbank.csv
Start playing with your data!!!

 Note:  A sleep of 20 seconds is given because alpha vantage allows only 3 API calls per minute.

1 comment:

  1. Great work man you have post a great post it will help people very much keep it do more for people like that pre funded trading account.