Menu

Python for finance : download stock data (stock price, volumes)

Follow us on Twitter 
Question, remark ? Contact us at contact@myengineeringtools.com


1. Prerequisites
2. How to get stock price data in Python : step by step guide
3. Python FOR LOOP : break instruction

One of the basics in using Python for finance is to download stock data, the following tutorial explains step by step how to do that with one libary : pandas-datareader.

1. Prerequisites

Before attempting to download stock data, the Python installation you are running must have the following libraries installed :

  • Numpy
  • Pandas
  • Pandas-Datareader

If you don't have those libraries installed, please run pip install to install them :

  • pip install numpy
  • pip install pandas
  • pip install pandas-datareader

2. How to get stock price data in Python : step by step guide

2.1 STEP 1 : Import the libraries

1st of all, start by importing the libraries that you will need :

import numpy as np
import pandas as pd
import pandas_datareader as web

2.2 STEP 2 : Test the simple download of a stock price data

The simplest synthax is the following :

web.DataReader(ticker, 'yahoo', start, end)

With :

ticker : the ticker symbol of the stock you want to download, as a string
'yahoo' : it is the datasource, Yahoo finance is very common but there are other datasources managed by Pandas-Datareader
start : it is the start date of the period to download, format MM-DD-YYYY (but the library can also parse other date formats)
end : it is the end date of the period to download, format MM-DD-YYYY (but the library can also parse other date formats)

Example : we download chevron stock data from 15/02/2015 to 01/11/2021 and we assign to a dataframe named df

df = web.DataReader('CVX', 'yahoo', '02-15-2015', '11-01-2021')

You can print the dataframe with print(df) afterwards, the listing is the following :

The data downloaded are, for each date, High, Low, Open, Close, Volume, Adj Close in this typical format :

Date (index) High Low Open Close Volume Adj Close








2.3 STEP 3 : Create a function to download stock data and record in a csv file

Running the function and entering manually the parameters is of course not very practical, the function below helps to automatize the download of the data and also records the data downloaded in csv files for later use.

def save_stock_data_csv(portfolio, start_date, end_date, path):
    '''Download the stock price data of all tickers in a portfolio
    portfolio is a list of strings'''
    
    for ticker in portfolio:
        df = web.DataReader(ticker, 'yahoo', start_date, end_date)
        df.to_csv(path + ticker + '.csv')

The function can be tested, for example using the code below :

path = 'Data/'
portfolio = ['AMZN', 'AAPL']
start_date = '2016-01-01'
end_date = '2020-01-01'

save_stock_data_csv(portfolio, start_date, end_date, path)

This allows to download all the stock data in csv files in the Data/ folder.

2.4 STEP 4 : read the data in the csv file

It is of course possible to download from Yahoo at each execution but this might not be efficient for a high number of stocks. In this case, downloading one time the data with the function described at step 3 allows to have csv file that can be reused later. The following code allows to red the data from the csv file :

portfolio = ['AMZN', 'AAPL']
df = pd.read_csv(path + portfolio[0] + '.csv')
print(df)

This allows to affect the data downloaded for Amazon to the dataframe df, which gives the following result :


You can download the file with the code here : get_stock_data.py