As traders, we always want to monitor the stock price movement on-screen to quickly take a buy or sell position based on our strategy. It can be a task to open any financial market website like- Yahoo Finance or NSE India and monitor that one stock on which we want to make our decision. But, if we want to monitor multiple stocks, then the time required to open each stock and under a separate window will be a bit cumbersome.
How about extracting intraday data of all the stocks available in our watchlist?
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 later to test or backtest our strategies or identify the entry or exit points/mistakes to refine our strategy. For now, we will just focus on how to extract the intraday stock data.
Let us understand how to extract intraday stock data through API-
Suppose, we have a list of stocks for which we want to extract the intraday data from Yahoo Finance. We decide that we want the data of the date 31-12-2020.
Firstly, we have to import the libraries required to extract the data such as pandas, nsepy, datetime as shown below-
(learn more about Libraries)
We also need to provide the list of ticker symbols, period, and interval as shown below-
(learn more about Lists)
("nifty50_ticker_list" contains the list of 50 constituents present in NIFTY 50 Index)
Now that we have imported the requisite libraries and specified the criteria as above, it is very easy to extract the intraday 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 "nifty50_ticker_spot_intra_df" 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 intraday stock data from Yahoo Finance using Python API-
(For each item in the ticker list "nifty50_ticker_list", it will extract the data using "yf.download" function of Yahoo Finance library referring to the ticker symbol, period, and interval. Then it will store all such data into a dictionary classified with the ticker item name.)
To call the stock data from that 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 Datetime, Open, High, Low, Close, Adjusted Close, and Volume is available and can be used for the 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 want. One can extract live stock price movement updated every minute by running that one-liner code in a loop until the "Datetime" market closes. "matplotlib" can be used to chart the data points and provide ' .clear()' and '.show()' command to update the chart including the latest data points as shown below-
(The first plot represents intraday Stock Price movement and the second plot represents intraday volume.)