|
import pandas as pd |
|
import numpy as np |
|
from datetime import datetime |
|
import streamlit as st |
|
import matplotlib.pyplot as plt |
|
import plotly.express as px |
|
|
|
|
|
def preprocess(stocks,choices): |
|
symbols, weights, investing_style, benchmark, rf, A_coef = choices.values() |
|
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close") |
|
print('stocks',stocks) |
|
logRet = np.log(stocks/stocks.shift()) |
|
log_returns = np.log(stocks/stocks.shift()) |
|
tickers_list = symbols.copy() |
|
weights_list = weights.copy() |
|
return logRet,tickers_list,weights_list |
|
|
|
def cumulative_return(stocks,choices): |
|
symbols, weights, investing_style, benchmark, rf, A_coef = choices.values() |
|
logRet,tickers_list,weights_list = preprocess(stocks,choices) |
|
tkers = sorted(set(stocks['Ticker'].unique())) |
|
|
|
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close") |
|
|
|
stock_port = {} |
|
for e in tickers_list: stock_port[e] = 0 |
|
|
|
weights = [float(x) for x in weights_list] |
|
s = sum(weights) |
|
|
|
new_weights = [] |
|
for i in weights: new_weights.append(i/s) |
|
|
|
i = 0 |
|
for e in stock_port: |
|
stock_port[e] = new_weights[i] |
|
i += 1 |
|
|
|
port = dict.fromkeys(tkers, 0) |
|
port.update(stock_port) |
|
|
|
portfolio_dict = port |
|
|
|
for e in portfolio_dict: |
|
tmp = 0 |
|
if portfolio_dict[e] > tmp: |
|
tmp = portfolio_dict[e] |
|
tick = e |
|
list_ =[] |
|
for e in tickers_list: |
|
if e not in list_: |
|
list_.append(e) |
|
|
|
df = stocks[list_] |
|
df = df/df.iloc[0] |
|
df.reset_index(inplace=True) |
|
df=pd.DataFrame(df) |
|
print(df) |
|
fig = px.line(df, x='Date' ,y=df.columns[1:,]) |
|
|
|
|
|
|
|
fig.update_layout( |
|
xaxis=dict( |
|
rangeselector=dict( |
|
buttons=list([ |
|
dict(count=1, |
|
label="1m", |
|
step="month", |
|
stepmode="backward"), |
|
dict(count=6, |
|
label="6m", |
|
step="month", |
|
stepmode="backward"), |
|
dict(count=1, |
|
label="YTD", |
|
step="year", |
|
stepmode="todate"), |
|
dict(count=1, |
|
label="1y", |
|
step="year", |
|
stepmode="backward"), |
|
dict(step="all") |
|
]) |
|
), |
|
rangeslider=dict( |
|
visible=True |
|
), |
|
type="date" |
|
) |
|
) |
|
fig.update_layout(xaxis=dict(rangeselector = dict(font = dict( color = "black")))) |
|
st.subheader('Portfolio Historical Normalized Cumulative Returns') |
|
|
|
st.plotly_chart(fig, use_container_width=True) |
|
|
|
def sharp_ratio_func(stocks,choices): |
|
symbols, weights, investing_style, benchmark, rf, A_coef = choices.values() |
|
logRet,tickers_list,weights_list = preprocess(stocks,choices) |
|
tkers = sorted(set(stocks['Ticker'].unique())) |
|
|
|
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close") |
|
|
|
stock_port = {} |
|
for e in tickers_list: stock_port[e] = 0 |
|
|
|
weights = [float(x) for x in weights_list] |
|
s = sum(weights) |
|
|
|
new_weights = [] |
|
for i in weights: new_weights.append(i/s) |
|
|
|
i = 0 |
|
for e in stock_port: |
|
stock_port[e] = new_weights[i] |
|
i += 1 |
|
|
|
port = dict.fromkeys(tkers, 0) |
|
port.update(stock_port) |
|
|
|
portfolio_dict = port |
|
|
|
sharp_ratio_list = [] |
|
for ticker in symbols: |
|
logRet = np.log(stocks/stocks.shift()) |
|
stk = dict.fromkeys(tkers, 0) |
|
stkTicker = {ticker:1} |
|
stk.update(stkTicker) |
|
ttlStk = np.sum(logRet*stk, axis=1) |
|
stock_sharpe_ratio = ttlStk.mean() / ttlStk.std() |
|
sharp_ratio_list.append(stock_sharpe_ratio) |
|
|
|
sharp_ratio = {'Assets': symbols, 'Sharpe Ratio': sharp_ratio_list} |
|
|
|
|
|
logRet = np.log(stocks/stocks.shift()) |
|
portfolio = dict.fromkeys(tkers, 0) |
|
portfolio.update(portfolio_dict) |
|
totalPortfolio = np.sum(logRet*portfolio, axis=1) |
|
portfolio_sharpe_ratio = totalPortfolio.mean() / totalPortfolio.std() |
|
|
|
sharp_ratio['Assets'].append('Portfolio') |
|
sharp_ratio['Sharpe Ratio'].append(portfolio_sharpe_ratio) |
|
|
|
fig = px.bar(sharp_ratio, x='Assets', y="Sharpe Ratio",color='Assets') |
|
fig.update_layout(title_text = 'Sharpe Ratio of the Assets and Portfolio', |
|
title_x=0.458) |
|
st.plotly_chart(fig, use_container_width=True) |
|
|
|
|