Stock Market Data: Obtaining Data, Visualization & Analysis in Python

8 min read

By Ishan Shah

Are you looking to get stock market data and analyse the historical data in Python? You have come to right place.

After reading this, you will be able to:

  • Get historical data for stocks
  • Plot the stock market data and analyse the performance
  • Get the fundamental, futures and options data

For easy navigation, this article is divided as below:


How to get Stock Market Data in Python?

Before going ahead to get stock market data in Python, here is comprehensive tutorial on creating your own Python trading bot and delving into the exciting world of algorithmic trading.


Yahoo Finance

One of the first sources from which you can get historical daily price-volume stock market data is Yahoo finance. You can use pandas_datareader or yfinance module to get the data and then can download or store in a csv file by using pandas.to_csv method.

If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.

Getting Market Data: Stocks, Crypto, News & Fundamental

yfinance installation logs
data using yfinance

To visualize the adjusted close price data, you can use the matplotlib library and plot method as shown below.

amazon data plot
Data Source: Yahoo Finance

Let us improve the plot by resizing, giving appropriate labels and adding grid lines for better readability.

adjusted close price of amazon
Data Source: Yahoo Finance

Advantages

  1. Adjusted close price stock market data is available
  2. Most recent stock market data is available
  3. Doesn't require API key to fetch the stock market data

Getting Market Data: Stocks, Crypto, News & Fundamental


Here's an interesting video by Nitesh Khandelwal (Co-Founder and CEO, QuantInsti) that answers all your questions related to getting Data for Algo Trading.


How to get Stock Market Data for different geographies?

To get stock market data for different geographies, search the ticker symbol on Yahoo finance and use that as the ticker.

Get stock market data for multiple tickers

To get the stock market data of multiple stock tickers, you can create a list of tickers and call the yfinance download method for each stock ticker.

For simplicity, I have created a dataframe data to store the adjusted close price of the stocks.

multiple data tickers
adjusted close price plot
Data Source: Yahoo Finance

S&P 500 Stock Tickers

If you want to analyse the stock market data for all the stocks which make up S&P 500 then below code will help you. It gets the list of stocks from the Wikipedia page and then fetches the stock market data from yahoo finance.

Learn to fetch data for Trading using Python

Free self-paced course

data for tickers
 stock market data for all the stocks which make up S&P 500

Intraday or Minute Frequency Stock Data

yfinance module can be used to fetch the minute level stock market data. It returns the stock market data for the last 7 days.

If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.

The yfinance module has the download method which can be used to download the stock market data.

It takes the following parameters:

  1. ticker: The name of the tickers you want the stock market data for. If you want stock market data for multiple tickers then separate them by space
  2. period: The number of days/month of stock market data required. The valid frequencies are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
  3. interval: The frequency of the stock market data. The valid intervals are 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo

The below code fetches the stock market data for MSFT for the past 5 days of 1-minute frequency.

stock market data for msft for the past 5 days of 1 minute frequency

Resample Stock Data

Convert 1-minute data to 1-hour data or Resample Stock Data

During strategy modelling, you might be required to work with a custom frequency of stock market data such as 15 minutes or 1 hour or even 1 month.

If you have minute level data, then you can easily construct the 15 minutes, 1 hour or daily candles by resampling them. Thus, you don't have to buy them separately.

In this case, you can use the pandas resample method to convert the stock market data to the frequency of your choice. The implementation of these is shown below where a 1-minute frequency data is converted to 10-minute frequency data.

The first step is to define the dictionary with the conversion logic. For example, to get the open value the first value will be used, to get the high value the maximum value will be used and so on.

The name Open, High, Low, Close and Volume should match the column names in your dataframe.

Convert the index to datetime timestamp as by default string is returned. Then call the resample method with the frequency such as:

  • 10T for 10 minutes,
  • D for 1 day and
  • M for 1 month
resample data stock market python

Yahoo finance has limited set of minute level data. if you need the stock market data for higher range then you can get the data from data vendors such as Quandl, AlgoSeek or your broker.

Learn to fetch data for Trading using Python

Free self-paced course

Using Quandl to get Stock Market Data (Optional)

Quandl has many data sources to get different types of stock market data. However, some are free and some are paid. Wiki is the free data source of Quandl to get the data of the end of the day prices of 3000+ US equities. It is curated by Quandl community and also provides information about the dividends and split.

Quandl also provides paid data source of minute and lower frequencies.

To get the stock market data, you need to first install the quandl module if it is not already installed using the pip command as shown below.

You need to get your own API Key from quandl to get the stock market data using the below code. If you are facing issue in getting the API key then you can refer to this link.

After you get your key, assign the variable QUANDL_API_KEY with that key. Then set the start date, end date and the ticker of the asset whose stock market data you want to fetch.

The quandl get method takes this stock market data as input and returns the open, high, low, close, volume, adjusted values and other information.

Date Open High Low Close Volume Ex-Dividend Split Ratio Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume
1997-05-16 22.38 23.75 20.50 20.75 1225000.0 0.0 1.0 1.865000 1.979167 1.708333 1.729167 14700000.0
1997-05-19 20.50 21.25 19.50 20.50 508900.0 0.0 1.0 1.708333 1.770833 1.625000 1.708333 6106800.0
1997-05-20 20.75 21.00 19.63 19.63 455600.0 0.0 1.0 1.729167 1.750000 1.635833 1.635833 5467200.0
1997-05-21 19.25 19.75 16.50 17.13 1571100.0 0.0 1.0 1.604167 1.645833 1.375000 1.427500 18853200.0
1997-05-22 17.25 17.38 15.75 16.75 981400.0 0.0 1.0 1.437500 1.448333 1.312500 1.395833 11776800.0

To learn more about how you can use Quandl, check out this article:


Fundamental Data

We have used yfinance to get the fundamental data.

The first step is to set the ticker and then call the appropriate properties to get the right stock market data.

Learn to fetch data for Trading using Python

Free self-paced course

If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.

Key Ratios

You can fetch the latest price to book ratio and price to earnings ratio as shown below.

Revenues

total-revenues
Data Source: Yahoo Finance

Earnings Before Interest and Taxes (EBIT)

ebit
Data Source: Yahoo Finance

Balance sheet, cash flows and other information


In this webinar recording, Deepak Shenoy (Founder and CEO, Capitalmind) explains how one can use Financial Market Data for Fundamental Analysis.


Suggested read

To learn more about Jupyter notebook, here's a tutorial on Jupyter Notebook to get you started. It does not require any pre-requisite knowledge and does not assume any familiarity with the framework.

Getting Market Data: Stocks, Crypto, News & Fundamental


Stock Market Data Visualization and Analysis

After you have the stock market data, the next step is to create trading strategies and analyse the performance. The ease of analysing the performance is the key advantage of the Python.

We will analyse the cumulative returns, drawdown plot, different ratios such as

Here's an article that describes the above ratios: Portfolio Allocation and Pair Trading Strategy using Python

I have created a simple buy and hold strategy for illustration purpose with four stocks namely:

  • Apple
  • Amazon
  • Microsoft
  • Walmart

To analyse the performance, you can use the pyfolio tear sheet as shown below.

Install pyfolio if not already installed, as follows:

analyze stock market data performance using pyfolio tear sheet

Suggested reads on Data Visualization using Python

You will find it very useful and knowledgeable to read through this curated compilation of some of our top blogs on:

Python for Trading
Machine Learning
Sentiment Trading
Algorithmic Trading
Options Trading
Technical Analysis


Conclusion

I hope this article will empower you to be able to use the Python codes to fetch the stock market data of your favourites stocks, build the strategies using this stock market data and analyse this data. I would appreciate if you could share your thoughts and your comments below.

Python is quite essential to understand data structures, data analysis, dealing with financial data, and for generating trading signals.

If you too want to learn how to fetch various data like pricing data of stocks, fundamental data, tweets by keyword and latest news headlines data - check out this completely FREE course Getting Market Data: Stocks, Crypto, News & Fundamental by Quantra.

You can also check out our course on Crypto Trading Strategies which is perfect for programmers and quants who wish to explore trading opportunities in Cryptocurrency.


Downloadable file

You can download the Jupyter notebook having the Python codes mentioned in the blog with the help of downloadable button below.

Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.

AI-Powered Trading Workshop 2024