-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAPM_Returns.py
100 lines (78 loc) · 3.22 KB
/
CAPM_Returns.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Importing libraries
import streamlit as st
import pandas as pd
import yfinance as yf
import pandas_datareader.data as web
import datetime
import capm_functions
st.set_page_config(page_title = "CAPM",
page_icon = "chart_with_upwards_trend",
layout = 'wide')
st.title("Capital Asset Pricing Model")
# Getting input from user
col1, col2 = st.columns([1,1])
with col1:
stock_list = st.multiselect("Choose 4 stocks", ('TSLA', 'AAPL', 'NFLX', 'MSFT', 'MGM', 'AMZN', 'NVDA', 'GOOGL'), ['TSLA', 'AAPL', 'AMZN', 'GOOGL'])
with col2:
year = st.number_input("Number of years",1,10)
# Downloading data for SP500
try:
end = datetime.date.today()
start = datetime.date(datetime.date.today().year-year, datetime.date.today().month, datetime.date.today().day)
SP500 = web.DataReader(['sp500'], 'fred', start, end)
print(SP500.tail())
stocks_df = pd.DataFrame()
for stock in stock_list:
data = yf.download(stock, period = f'{year}y')
stocks_df[f'{stock}'] = data['Close']
stocks_df.reset_index(inplace = True)
SP500.reset_index(inplace = True)
SP500.columns = ['Date', 'sp500']
stocks_df['Date'] = stocks_df['Date'].astype('datetime64[ns]')
stocks_df['Date'] = stocks_df['Date'].apply(lambda x:str(x)[:10])
stocks_df['Date'] = pd.to_datetime(stocks_df['Date'])
stocks_df = pd.merge(stocks_df, SP500, on = 'Date', how = 'inner')
print(stocks_df)
col1, col2 = st.columns([1,1])
with col1:
st.markdown("### Dataframe head")
st.dataframe(stocks_df.head(), use_container_width = True)
with col2:
st.markdown("### Dataframe tail")
st.dataframe(stocks_df.tail(), use_container_width = True)
col1, col2 = st.columns([1,1])
with col1:
st.markdown("### Price of all the Stocks")
st.plotly_chart(capm_functions.interactive_plot(stocks_df))
with col2:
st.markdown("### Price of all the Stocks (After Normalizing)")
st.plotly_chart(capm_functions.interactive_plot(capm_functions.normalize(stocks_df)))
stocks_daily_return = capm_functions.daily_return(stocks_df)
print(stocks_daily_return.head())
beta = {}
alpha = {}
for i in stocks_daily_return.columns:
if i !='Date' and i != 'sp500':
b, a = capm_functions.calculate_beta(stocks_daily_return, i)
beta[i] = b
alpha[i] = a
print(beta, alpha)
beta_df = pd.DataFrame(columns = ['Stocks', 'Beta Value'])
beta_df['Stock'] = beta.keys()
beta_df['Beta Value'] = [str(round(i,2)) for i in beta.values()]
with col1:
st.markdown('### Calculated Beta Value')
st.dataframe(beta_df, use_container_width = True)
rf = 0
rm = stocks_daily_return['sp500'].mean()*252
return_df = pd.DataFrame()
return_value = []
for stock, value in beta.items():
return_value.append(str(round(rf+(value*(rf-rm)),2)))
return_df['Stock'] = stock_list
return_df['Return Value'] = return_value
with col2:
st.markdown('### Calculated Return using CAPM')
st.dataframe(return_df, use_container_width = True)
except:
st.write("Please select valid inputs")