|
import pandas as pd |
|
import seaborn as sns |
|
import streamlit as st |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import altair as alt |
|
import plotly.express as px |
|
|
|
|
|
def beta(stock_df, choices): |
|
symbols, weights, investment, rf, A_coef = choices.values() |
|
tickers = symbols |
|
tickers.append('sp500') |
|
|
|
quantity = weights |
|
selected_stocks = stock_df[tickers] |
|
|
|
|
|
|
|
|
|
|
|
df_stocks = selected_stocks.copy() |
|
|
|
for i in selected_stocks.columns[1:]: |
|
for j in range(1, len(selected_stocks)): |
|
df_stocks[i][j] = ((selected_stocks[i][j] - selected_stocks[i][j - 1]) / selected_stocks[i][j - 1]) * 100 |
|
df_stocks[i][0] = 0 |
|
|
|
|
|
|
|
beta_list = [] |
|
alpha_list = [] |
|
stocks_daily_return = df_stocks |
|
for i in stocks_daily_return.columns: |
|
if i != 'Date' and i != 'sp500': |
|
|
|
b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1) |
|
|
|
beta_list.append(round(b, 2)) |
|
alpha_list.append(round(a, 2)) |
|
|
|
|
|
symbols.remove('sp500') |
|
beta = {'Assets': symbols, 'Beta': beta_list} |
|
alpha = {'Assets': symbols, 'Alpha': alpha_list} |
|
|
|
st.subheader('Beta and Alpha of Assets Compared to S&P500 index') |
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.dataframe(beta) |
|
with col2: |
|
st.dataframe(alpha) |
|
|
|
|
|
def ER(stock_df, choices): |
|
symbols, weights, investment, rf, A_coef = choices.values() |
|
symbols_ =symbols.copy() |
|
tickers = symbols |
|
tickers.append('sp500') |
|
|
|
quantity = weights |
|
selected_stocks = stock_df[tickers] |
|
|
|
|
|
|
|
|
|
|
|
df_stocks = selected_stocks.copy() |
|
|
|
for i in selected_stocks.columns[1:]: |
|
for j in range(1, len(selected_stocks)): |
|
df_stocks[i][j] = ((selected_stocks[i][j] - selected_stocks[i][j - 1]) / selected_stocks[i][j - 1]) * 100 |
|
df_stocks[i][0] = 0 |
|
beta = {} |
|
alpha = {} |
|
stocks_daily_return = df_stocks |
|
|
|
|
|
for i in stocks_daily_return.columns: |
|
if i != 'Date' and i != 'sp500': |
|
|
|
b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1) |
|
|
|
beta[i] = round(b, 2) |
|
alpha[i] = round(a, 2) |
|
|
|
|
|
|
|
keys = list(beta.keys()) |
|
ER_ = [] |
|
|
|
rf = 0 |
|
|
|
rm = stocks_daily_return['sp500'].mean() * 252 |
|
for i in keys: |
|
ER_.append( round(rf + (beta[i] * (rm - rf)), 2)) |
|
|
|
|
|
|
|
|
|
symbols.remove('sp500') |
|
|
|
|
|
Expected_return = {'Assets': symbols_, 'Expected Annual Return': ER_} |
|
|
|
|
|
|
|
|
|
|
|
|
|
portfolio_weights = [] |
|
current_cash_value = 0 |
|
total_portfolio_value = 0 |
|
cash_value_stocks =[] |
|
for i in range(len(tickers) ): |
|
stocks_name = tickers[i] |
|
current_cash_value = selected_stocks[stocks_name].iloc[-1] |
|
stocks_quantity = quantity[i] |
|
cash_value = stocks_quantity * current_cash_value |
|
cash_value_stocks.append(cash_value) |
|
total_portfolio_value += cash_value |
|
portfolio_weights.append(cash_value) |
|
|
|
portfolio_weights = (portfolio_weights / total_portfolio_value)*100 |
|
ER_portfolio= [] |
|
ER_portfolio = sum(list(ER_) * portfolio_weights)/100 |
|
|
|
|
|
|
|
|
|
|
|
Bar_output = Expected_return.copy() |
|
Bar_output['Assets'].append('Portfolio') |
|
Bar_output['Expected Annual Return'].append(ER_portfolio) |
|
fig = px.bar(Bar_output, x='Assets', y="Expected Annual Return",color='Assets') |
|
|
|
st.subheader('Annual Expected Return of the Assets and Portfolio') |
|
st.plotly_chart(fig, use_container_width=True) |
|
|
|
return beta, cash_value_stocks |
|
|
|
def basic_portfolio(stock_df): |
|
"""Uses the stock dataframe to graph the normalized historical cumulative returns of each asset. |
|
""" |
|
|
|
daily_return = stock_df.dropna().pct_change() |
|
|
|
cumulative_return = (1 + daily_return).cumprod() |
|
|
|
|
|
st.subheader('Portfolio Historical Normalized Cumulative Returns') |
|
|
|
st.line_chart(cumulative_return) |
|
|
|
|
|
def display_heat_map(stock_df,choices): |
|
symbols, weights, investment, rf, A_coef = choices.values() |
|
selected_stocks = stock_df[symbols] |
|
|
|
price_correlation = selected_stocks.corr() |
|
|
|
|
|
st.subheader('Heatmap Showing Correlation Of Assets') |
|
|
|
fig, ax = plt.subplots() |
|
fig = px.imshow(price_correlation,text_auto=True, aspect="auto") |
|
|
|
st.write(fig) |
|
|
|
|
|
|
|
"""Uses the stock dataframe and the chosen weights from choices to calculate and graph the historical cumulative portfolio return. |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def buble_interactive(stock_df,choices): |
|
symbols, weights, investment, rf, A_coef = choices.values() |
|
beta,cash_value_weights = ER(stock_df,choices) |
|
my_list = [] |
|
for i in beta.values(): |
|
my_list.append(i) |
|
|
|
df_final =pd.DataFrame() |
|
df_final['ticker'] = symbols |
|
df_final['quantities'] = weights |
|
df_final['cash_value'] =cash_value_weights |
|
df_final['Beta'] = my_list |
|
|
|
fig = px.scatter( |
|
df_final, |
|
x="quantities", |
|
y="Beta", |
|
size="cash_value", |
|
|
|
hover_name="ticker", |
|
log_x=True, |
|
size_max=60, |
|
) |
|
fig.update_layout(title="Beta ----write something") |
|
|
|
st.plotly_chart(fig, use_container_width=True) |