How to do it...

Execute the following steps to download the stock prices and calculate simple/log returns.

  1. Import the libraries:
import pandas as pd 
import numpy as np
import yfinance as yf
  1. 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)
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.194.172