How do I access historical stock data?

  • Published on October 2, 2021
  • In Developers Corner

Top Python Libraries to Get Historical Stock Data (With Code)

Stock market analysis has always been a very interesting work not only for investors but also for analytics professionals.
  • By Vijaysinh Lendave

Stock market analysis has always been a very interesting work not only for investors but also for analytics professionals. To analyze the stock market, it needs to have the historical data of the stocks. Finding historical data used to be tedious, time-consuming and costly in the past. With the advancement of financial technologies (FinTech) and the trend toward inclusive finance, there are now a variety of free-market data sources available online. In this post, we will discuss the popular python packages which can be used to retrieve the historical data of a single or multiple stocks. We will see how with only a few lines of codes, we can download the data of years within seconds. The python packages that we are going to cover in this article are listed below.

Packages to be Discussed

  1. Pandas DataReaders
  2. Yahoo Finance
  3. Twelve Data

Pandas DataReaders

The first method that we are going to see is for collecting 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 subpackage supports the user in building 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 Frenchs data repository, and 16 more such sources as mentioned in its documentation. Following the connection, we can extract the data and read it in as a data frame.

While retrieving any stock price or data in sequence certain arguments that need to be defined in most of all the packages are;

  • Period: The frequency with which the data is collected; common selections are 1d (daily), 1mo (monthly), and 1y (yearly)
  • Start: The date on which the data collection will begin. For example, 2015525
  • End: the date on which the data collection will be completed. For instance, 2021925.

When you get output values of any stock, in most of the cases the output of the query is a pandas data frame and the fields of those data frames are described below:

  • Open: The stock price at the start of that day/month/year.
  • Close: the stock price at the conclusion of that particular day/month/year
  • High: the stocks highest price that day/month/year.
  • Low: the stocks lowest price that day/month/year.
  • Volume: The number of shares traded that day/month/year.

Pandas DataRedears is not a data source in and of itself, but rather an API in the PyData stack that enables a multitude of data sources. The data will be downloaded as a pandas Dataframe, as the name implies. The complete document is available here. The sources that it currently supports are listed below. We will only go through a few of them.

Getting data from Alpha Vantage

Alpha Vantage provides enterprise-grade financial market data through a set of powerful and developer-friendly APIs. To set up this environment you will need to have an API key, it can be straightly 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()

Heres how the obtained data frame looks like:

Getting Data from FRED

The Federal Reserve Economic Data (FRED) database is managed by the Research division of the Federal Reserve Bank of St. Louis and contains over 765,000 economic time series from 96 sources. All such huge data can be accessed by the DataReader API just under the symbol category we need 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 passed above, it shows the trading categories that are imported from Japan and China.

Yahoo Finance

Yahoo! Finance is a component of Yahoos network. It is the most widely used business news website in the United States, featuring stock quotes, press announcements, financial reports, and original content, as well as financial news, data, and commentary. They provide market data, fundamental and option data, market analysis, and news for cryptocurrencies, fiat currencies, commodities futures, equities, and bonds, as well as fundamental and option data, market analysis, and news.

The above image is the web interface of Yahoo Finance which markets the status of different cryptocurrencies. To retrieve such data Yahoo finance has its dedicated tool called yfinance. It is really simple and straightforward, as you will go through the below API under which you need to change only the symbol ( left-most column in the above image)

!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()

Also, we can take multiple trades into account as given below.

data = yf.download(['BTC-USD','AMD'],'2021-01-01','2021-09-30') data["Close"].plot() plt.show()

Twelve Data

Twelve Data was created in 2009 and has recently gained traction. The following are the major elements of the services they provide:

  • API access to real-time and historical data
  • Creating dynamic graphs
  • Large technical indicators (above 100).
  • Quotes are streamed using WebSockets.

The TwelveData projects main purpose is to offer a single location where all Pythonistas may receive fast access to all financial markets and analyze them with just a few lines of code.

We must first register on their website and obtain our API KEY, the same as we did with Alpha Vantage.

Using the Twelve data we will query the stock price of Microsoft corporation and also we will 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()

Final Words

As weve seen, there are numerous ways to obtain historical stock data. Weve seen not only the many data providers but also how to extract data from them using Pythons standard API. As previously stated, having access to high-quality historical data is critical for backtesting your trading technique. These data suppliers are both free and paid. In this post, we looked at three free historical financial data sources: Pandas DataReader, Yahoo Finance, and Twelve Data covering equities, rates, foreign exchange, cryptocurrency, and commodities.

References

  • Stock Market Data Analysis in Python
  • Yahoo Finance
  • Pandas Data Readers
  • Twelve Data
  • Link for above codes

More Great AIM Stories

Council Post: Uberisation of Analytics

Explaining Metaverse To A 6-Year-Old

A Guide to Automated String Cleaning and Encoding in Python

Hands-On Guide to Building Knowledge Graph for Named Entity Recognition

Can Robots Have Social Skills

Version 3 Of StyleGAN Released: Major Updates & Features

Vijaysinh is an enthusiast in machine learning and deep learning. He is skilled in ML algorithms, data manipulation, handling and visualization, model building.

More Stories

How NFTs will shape the metaverse

Akashdeep Arul

LatentView Analytics sees 38% revenue increase in third quarter

Sharath Kumar Nair

How DeepMind uses language models to flag harmful content in language models

Sharath Kumar Nair

Will Electronic Arts be acquired?

Srishti Mukherjee

OUR UPCOMING EVENTS

The Rising 2022 | Women in AI Conference

8th April | In-person Conference | Hotel Radisson Blue, Bangalore

Organized by Analytics India Magazine

View Event >>

Data Engineering Summit 2022

30th Apr | Virtual conference

Organized by Analytics India Magazine

View Event >>

Cypher 2022

21-23rd Sep | Bangalore | 6th edition

Indias largest AI Summit.

View Event >>

MORE FROM AIM
How to perform fast and explainable clustering using CLASSIX?

Clustering is the process of putting items together so that members of the same group (cluster) be more common with their peers than members of other groups.

Top resources to learn quantum machine learning

We will take a look at a few online resources one can use to learn quantum machine learning.

IIM Bangalore launches AI for Managers, a 16-month online course for decision-makers

The course starts with the foundations of data science and moves on to advanced topics such as deep learning and artificial neural networks.

Scikit-learn wins open-source scientific software prize at OSEC2022

Scikit-learn was developed by David Cournapeau as a Google summer of code project in 2007.

All you need to know about temporal knowledge graphs

Temporal knowledge graphs are graphs with a set of facts, information, or knowledge that have temporal features. These graphs can also be considered as dynamic, evolving, or time-varying graphs.

A guide to building reinforcement learning models in PyTorch

In this article, we will discuss how we can build reinforcement learning models using PyTorch.

How to restore the blurred image using Real-ESRGAN?

Real-ESRGAN is an extension of the powerful ESRGAN that synthesizes training pairs with a more practical degradation mechanism to recover general real-world low-resolution pictures. Real-ESRGAN is able to repair most real-world photos and produce superior visual performance than prior works

What is Datasaurus Dozen and its relevancy in data science

Researchers designed 12 shapes to direct the dots towards creating the Datasaurus Dozen.

A guide to Kats: python tool by Meta for effective time-series analysis

Kats stands for Kits to Analyze Time Series, which was developed by the researchers at Facebook, now Meta. One of the most important things about Kats is that it is very easy to use. Also, it is a very light weighted library of generic time series analysis in a very generalized nature.

What are autocorrelation and partial autocorrelation in time series data?

In time series analysis and forecasting, autocorrelation and partial autocorrelation are frequently employed to analyze the data.

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Join Discord Community

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Join Telegram

Subscribe to our newsletter

Get the latest updates from AIM
Email
Subscribe

Video

Postingan terbaru

LIHAT SEMUA