Follow us on Twitter ![]()
Question, remark ? Contact us at contact@myengineeringtools.com
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.
Before attempting to download stock data, the Python installation you are running must have the following libraries installed :
If you don't have those libraries installed, please run pip install to install them :
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
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 |
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.
Top 5 Most
Popular
1. Compressor
Power Calculation
2. Pump Power Calculation
3. Pipe Pressure
Drop Calculation
4. Fluid Velocity in pipes
5. Churchill Correlation
(friction factor)
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.
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