Stock market analysis has always been very interesting work not only for investors but also for analysts. To analyze the stock market, it needs to have the historical data of the stocks. In the past, searching for historical data was tedious, time consuming and expensive. With the advancement of financial technologies (FinTech) and the trend towards inclusive finance, there are now a variety of open market data sources available online. In this article, we will discuss popular python packages that can be used to retrieve historical data from one or more stocks. We will see how with just a few lines of code we can download the data for years in seconds. The python packages that we are going to cover in this article are listed below.
Packages to be discussed
- Pandas Data Readers
- Yahoo finance
- Twelve data
Pandas Data Readers
The first method we’ll see is to collect data with Pandas-DataReader. Pandas is a Python library for data analysis and manipulation that is a free source. As a result, the Pandas-DataReader sub-package helps the user to create data frames from various internet sources. It allows users to connect to a range of sources, such as Naver Finance, Bank of Canada, Google Analytics, Kenneth French Data Repository, and 16 other sources as mentioned in his documentation. After connection, we can extract the data and read it as a data frame.
When retrieving the stock price or data in order, some arguments that should be set in most packages are:
- Period: The frequency with which the data is collected; common selections are ‘1d’ (daily), ‘1mo’ (monthly) and ‘1y’ (annually)
- Start: The date on which data collection will begin. For example, ‘2015-5-25 ′
- To finish: the date on which data collection will be completed. For example, ‘2021-9-25.’
When you get the output values of any inventory, in most cases the output of the query is a pandas dataframe and the fields of these data frames are described below:
- Open: The share price at the start of that day / month / year.
- Close: the share price at the end of that particular day / month / year
- High: highest share price on that day / month / year.
- Mooing: lowest share price on this day / month / year.
- Volume: The number of shares traded on that day / month / year.
Pandas DataRedears is not a data source per se, but rather an API in the PyData stack that enables a multitude of data sources. The data will be downloaded in the form of Dataframe pandas, as the name suggests. The full document is available here. The sources it currently supports are listed below. We will only cover a few of them.
Get data from Alpha Vantage
Alpha Vantage delivers enterprise-grade financial market data through a set of powerful, developer-friendly APIs. To configure this environment, you will need an API key, it can be directly taken from the documentation here.
## Alpha vintage import pandas as pd import pandas_datareader as pdr ts = pdr.av.time_series.AVTimeSeriesReader('IBM', api_key=PUT_YOUR_API_KEY_HERE) df = ts.read() df.index = pd.to_datetime(df.index, format="%Y-%m-%d") # plotting the opening and closing value df[['open','close']].plot()
This is what the resulting dataframe looks like:
Obtain data from FRED
Federal Reserve economic data The database (FRED) is maintained by the Research Division of the Federal Reserve Bank of St. Louis and contains over 765,000 economic time series from 96 sources. All of this huge data can be accessed by the DataReader API right under the category of symbols that we have to mention for which indicator we want the data. Indicators can be found here.
### Fred import pandas_datareader as pdr start = datetime(2021, 1, 1) end = datetime(2021, 9, 30) syms = ['IMPCH', 'IMPJP'] df = pd.DataFrame() for sym in syms: ts = pdr.fred.FredReader(sym, start=start, end=end) df1 = ts.read() df = pd.concat([df, df1], axis=1) df
As adopted above, it shows the trade categories which are imported from Japan and China.
Yahoo! Finance is a component of Yahoo’s network. It is the most widely used business news website in the United States, featuring stock quotes, newspaper advertisements, financial reports, and original content, as well as financial news, data, and commentary. They provide market data, fundamental and options data, market analysis and news on cryptocurrencies, fiat currencies, commodity futures, stocks and bonds, as well as data. fundamentals and options, market analysis and news.
The image above is the Yahoo Finance web interface that markets the status of different cryptocurrencies. To recover this data, Yahoo finance has its dedicated tool called finance. It’s really simple and straightforward, as you will go through the API below under which you need to change only the symbol (leftmost column in the image above)
!pip install yfinance import yfinance as yf import matplotlib.pyplot as plt data = yf.download('BTC-USD','2021-01-01','2021-09-30') data.head()
In addition, we can take multiple transactions into account as shown below.
data = yf.download(['BTC-USD','AMD'],'2021-01-01','2021-09-30') data["Close"].plot() plt.show()
Twelve Data was established in 2009 and has recently gained traction. Here are the main elements of the services they provide:
- API access to real-time and historical data
- Creating dynamic graphics
- Large technical indicators (greater than 100).
- Quotes are distributed using WebSockets.
The main goal of the TwelveData project is to provide a single location where all Pythonistas can receive quick access to all financial markets and analyze them with just a few lines of code.
You must first register on their website and get our KEY API, just like we did with Alpha Vantage.
Using the Twelve data, we will query the stock price of the Microsoft company and also plot an interactive Plotly dashboard.
!pip install twelvedata[pandas,matplotlib,plotly] !pip install websocket_client from twelvedata import TDClient # Initialize client td = TDClient(apikey="PUT_YOUR_API_KEY_HERE") # Construct the necessary time serie ts = td.time_series( symbol="MSFT", interval="1min", outputsize=500,) # returns Plotly dash ts.as_plotly_figure().show()
As we have seen, there are many ways to get historical data on stocks. We saw not only the many data providers, but also how to extract data from them using the standard Python API. As stated earlier, having access to high quality historical data is essential for testing your trading technique. These data providers are both free and paid. In this article, we looked at three sources of free historical financial data: Pandas DataReader, Yahoo Finance, and Twelve Data covering stocks, rates, currencies, cryptocurrency, and commodities.
Join our Discord server. Be part of an engaging online community. Join here.
Subscribe to our newsletter
Receive the latest updates and relevant offers by sharing your email.