Execute the following steps to download the stock prices and calculate simple/log returns.
- Import the libraries:
import pandas as pd
import numpy as np
import yfinance as yf
- Download the data and keep the adjusted close prices only:
df = yf.download('AAPL',
start='2000-01-01',
end='2010-12-31',
progress=False)
df = df.loc[:, ['Adj Close']]
df.rename(columns={'Adj Close':'adj_close'}, inplace=True)
- Calculate the simple and log returns using the adjusted close prices:
df['simple_rtn'] = df.adj_close.pct_change()
df['log_rtn'] = np.log(df.adj_close/df.adj_close.shift(1))
The resulting DataFrame looks as follows:
The first row will always contain a not a number (NaN) value, as there is no previous price to use for calculating the returns.