Stock Market Data And Analysis In Python

9 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.

  1. How to get Stock Market Data in Python?
  2. How to get Stock Market Data for different geographies?
  3. S&P 500 Stock Tickers
  4. Intraday or Minute Frequency Stock Data
  5. Resample Stock Data
  6. Fundamental Data
  7. Futures and Options Data
  8. Stock Market Data Visualization and Analysis

How to get Stock Market Data in Python?

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.

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

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

adjusted close price of amazon

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

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

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.

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.

ohlcv_dict = {
 'Open': 'first',
 'High': 'max',
 'Low': 'min',
 'Close': 'last',
 'Volume': 'sum'
}

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.


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.

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

Earnings Before Interest and Taxes (EBIT)

ebit

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.


Futures and Options Data

If you are looking to get Options chain data then you can refer to the FREE course on Getting Market Data on Quantra.

NSEpy

The nsepy package is used to get the stock market data for the futures and options for Indian stocks and indices.

Futures Data

Date Symbol Expiry Open High Low Close Last Settle Price Number of Contracts Turnover Open Interest Change in OI Underlying
2019-01-15 HDFC 2019-02-28 1986.70 2011.00 1982.95 2008.25 2006.20 2008.25 4810 4.796817e+09 2537500 2299500 1992.15
2019-01-16 HDFC 2019-02-28 2002.10 2010.15 1985.20 1992.15 1991.30 1992.15 2656 2.655748e+09 3783500 1246000 1975.00
2019-01-17 HDFC 2019-02-28 2003.60 2019.05 1991.60 2017.15 2013.00 2017.15 3993 4.008667e+09 5545000 1761500 NaN
2019-01-18 HDFC 2019-02-28 2018.55 2025.75 2005.00 2018.40 2017.25 2018.40 481 4.845300e+08 5637000 92000 2006.85
2019-01-21 HDFC 2019-02-28 2011.25 2031.10 1998.00 2016.55 2016.60 2016.55 1489 1.505249e+09 6258000 621000 2004.45
futures data closing price stock market data

Options Data

Date Symbol Expiry Option Type Strike Price Open High Low Close Last Settle Price Number of Contracts Turnover Premium Turnover Open Interest Change in OI Underlying
2019-01-15 HDFC 2019-02-28 CE 2000.0 52.70 56.00 52.70 56.0 56.0 56.0 3 3081000.0 81000.0 10000 1000 1992.15
2019-01-16 HDFC 2019-02-28 CE 2000.0 55.00 55.00 49.00 49.0 49.0 49.0 14 14358000.0 358000.0 11000 1000 1975.00
2019-01-17 HDFC 2019-02-28 CE 2000.0 59.15 64.65 51.00 61.9 61.9 61.9 27 27750000.0 750000.0 18500 7500 NaN
2019-01-18 HDFC 2019-02-28 CE 2000.0 63.00 63.00 60.00 60.0 60.0 60.0 7 7212000.0 212000.0 18500 0 2006.85
2019-01-21 HDFC 2019-02-28 CE 2000.0 62.05 69.00 62.05 62.9 62.9 62.9 6 6198000.0 198000.0 20000 1500 2004.45
options data close price stock market data

Suggested reads


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


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.

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.

Learning Track: Algorithmic Trading for Everyone