-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcagr.py
More file actions
32 lines (26 loc) · 1.02 KB
/
cagr.py
File metadata and controls
32 lines (26 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# --------------------------------------------------------
# Author: Daniel Xu
# Date: 07/20/2023
# Description: A program that implements Compounded Average Growth Rate
# for AMZN, AAPL, and GOOG
# --------------------------------------------------------
# Import libraries
import yfinance as yf
# CAGR - takes in a DataFrame and computes the Compounded Average Growth Rate
def cagr(df):
temp = df.copy()
temp['Return'] = temp['Adj Close'].pct_change()
temp['Cumulative Returns'] = (1 + temp['Return']).cumprod()
# decimal expression of years, based on number of trading days
n = len(temp)/252
CAGR = (temp['Cumulative Returns'][-1])**(1/n) - 1
return CAGR
# Download historical data for various stocks
tickers = ['AMZN', 'AAPL', 'GOOG']
stock_data = {}
for t in tickers:
data = yf.download(t, period='7mo', interval='1d') # 1 day candlestick
data.dropna(how='any', inplace=True)
stock_data[t] = data
for s in stock_data:
print('{} CAGR calculation: {}'.format(s, cagr(stock_data[s])))