As a trader or an investor, we always want to have stock data handy so that we can quickly look for historical stock price movement or end-of-day reports. It can be a task to open any financial market website like- Yahoo Finance or NSE India website and extract the historical stock data manually by entering the criteria such as ticker symbol, start date & end date of the lookback period, and period interval. But, if the number of stocks for which you want to extract the data is huge then the time required to extract those will be immense.
How about extracting the historical data of all the stocks available on that website?
I mean why not! -- after all, it's just a matter of seconds.
Once we extract, we can store all the data in some local dictionary and use that data for our exploratory analysis, build strategies and backtest them on such historical data. For now, we will just focus on how to extract the data.
Extract Historical Stock Data using API | NSEpy
Suppose, we have a list of stocks for which we want to extract the historical data from NSE India. We decide that we want the data for the period of 250 days.
Firstly, we have to import the requisite libraries to extract the data such as pandas, numpy, yfinance, nsepy, and datetime as shown below-
We also need to provide the list of ticker symbols, start date & end date of the lookback period as shown below-
(learn more about Lists)
("s_ticker_list" contains the list of all ticker symbols)
(start_date & end_date should be mentioned in the format YYYY-M-D)
Now that we have imported the requisite libraries and specified the criteria above, it is very easy to extract the historical data of all the stocks specified in the ticker list in one go just by running a one-liner code in a loop.
Before extracting the data, we also need to define an empty dictionary "s_ticker_spot_hist_dc" that will store the extracted data ticker-wise which can be later used to call the stock data as and when required.
To extract the stock data from NSE India using Python API-
For each item in the ticker list "s_ticker_list", it will extract the data using the "get_history" function of the NSEpy library referring to the parameters- ticker symbol, start date, and end date. Then it will store all such data into a dictionary classified (index) with the ticker item name.
To call out the stock data from the dictionary, we simply specify the dictionary name followed by the ticker symbol inside the brackets with double quotes, and the output of the same is shown below-
This whole bunch of information such as Date, Symbol, Series, Previous Close, Open, High, Low, Last, Close, VWAP, Volume, Turnover, Number of Trades, Deliverable Volume, and % Deliverable are available and can be used for our further analysis.
Extract Historical Stock Data using API | yFinance
Now that we have imported the requisite libraries and specified the criteria as above, it is very easy to extract the historical data of all the stocks specified in the ticker list in one go just by running a one-liner code. We will then store all the extracted stock information into a data frame, so it can be recalled as and when required.
To extract the stock data from Yahoo Finance using Python API-
For each item in the ticker list "s_ticker_list", it will extract the data using "yf.download" function of the yFinance library referring to the parameters- ticker list, start date & end date. Then it will store all such data into a data frame.
To call the stock data from that data frame, we simply specify the data frame name, and the output of the same is shown below-
This whole bunch of information such as Date, Open, High, Low, Close, Adj Close, and Volume is available and can be used for our further analysis.
This was easy, right?
Using one-liner code, we are able to extract a lot of stock information for as many stocks as we require.
Comments