Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Stock Market Data: Obtaining Data, Visualization & Analysis in Python – Part II

Stock Market Data: Obtaining Data, Visualization & Analysis in Python – Part II

Posted November 16, 2023 at 10:20 am
Ishan Shah
QuantInsti

Learn how to get historical data for stocks with Part I in this tutorial.

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.

# Import packages
import yfinance as yf
import pandas as pd

# Read and print the stock tickers that make up S&P500
tickers = pd.read_html(
    'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
print(tickers.head())

# Get the data for this tickers from yahoo finance
data = yf.download(tickers.Symbol.to_list(),'2021-1-1','2021-7-12', auto_adjust=True)['Close']
print(data.head())

SP500_tickers_data.py hosted with ❤ by GitHub


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.

!pip install yfinance

install yfinance.py hosted with ❤ by GitHub

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.

import yfinance as yf
intraday_data = yf.download(tickers="MSFT",
                            period="5d",
                            interval="1m",
                            auto_adjust=True)
intraday_data.head()

minute_data.py hosted with ❤ by GitHub


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'
}

ohlcv_dict.py hosted with ❤ by GitHub

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
# Import package & get the data
import yfinance as yf
intraday_data = yf.download(tickers="MSFT",
                            period="5d",
                            interval="1m",
                            auto_adjust=True)

# Define the resampling logic
ohlcv_dict = {
     'Open': 'first',
     'High': 'max',
     'Low': 'min',
     'Close': 'last',
     'Volume': 'sum'
}

# Resample the data
intraday_data_10 = intraday_data.resample('10T').agg(ohlcv_dict)
intraday_data_10.head()

resample_data_10.py hosted with ❤ by GitHub

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.

!pip install quandl

Install quandl.py hosted with ❤ by GitHub

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.

# Import quandl package
import quandl

# To get your API key, sign up for a free Quandl account.
# Then, you can find your API key on Quandl account settings page.
QUANDL_API_KEY = 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY'

# This is to prompt you to change the Quandl Key
if QUANDL_API_KEY == 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY':
    raise Exception("Please provide a valid Quandl API key!")
 
# Set the start and end date
start_date = '1990-01-01'
end_date = '2018-03-01'

# Set the ticker name
ticker = 'AMZN'

# Feth the data
data = quandl.get('WIKI/'+ticker, 
                  start_date=start_date, 
                  end_date=end_date, 
                  api_key=QUANDL_API_KEY)

# Print the first 5 rows of the dataframe
data.head()

get_data_quandl.py hosted with ❤ by GitHub

DateOpenHighLowCloseVolumeEx-DividendSplit RatioAdj. OpenAdj. HighAdj. LowAdj. CloseAdj. Volume
1997-05-1622.3823.7520.5020.751225000.00.01.01.8650001.9791671.7083331.72916714700000.0
1997-05-1920.5021.2519.5020.50508900.00.01.01.7083331.7708331.6250001.7083336106800.0
1997-05-2020.7521.0019.6319.63455600.00.01.01.7291671.7500001.6358331.6358335467200.0
1997-05-2119.2519.7516.5017.131571100.00.01.01.6041671.6458331.3750001.42750018853200.0
1997-05-2217.2517.3815.7516.75981400.00.01.01.4375001.4483331.3125001.39583311776800.0

Stay tuned for the next installment for insights on getting Fundamental Data.

Originally posted on QuantInsti blog.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.