Spaces:
Sleeping
Sleeping
File size: 5,011 Bytes
5935af9 |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import yfinance as yf
import numpy as np
import pandas as pd
import streamlit as st
from utilities.py.styling import streamlit_style
from utilities.py import plots
from utilities.py import summary_tables
from pypfopt import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
import plotly.express as px
import plotly.graph_objects as go
streamlit_style()
company_list_df = pd.read_csv("utilities/data/Company List.csv")
company_name = company_list_df["Name"].to_list()
company_symbol = (company_list_df["Ticker"] + ".NS").to_list()
name_to_symbol_dict = dict()
symbol_to_name_dict = dict()
for CSymbol, CName in zip(company_symbol, company_name):
name_to_symbol_dict[CName] = CSymbol
for CSymbol, CName in zip(company_symbol, company_name):
symbol_to_name_dict[CSymbol] = CName
streamlit_company_list_input = st.multiselect(
"Select Multiple Companies", company_name, default=None
)
optimization_methods = st.selectbox(
"Select an Optimsation Technique",
(
"Maximum Sharpe Ratio",
"Efficient Risk",
"Minimum Volatility",
"Efficient Return",
),
)
company_name_to_symbol = [name_to_symbol_dict[i] for i in streamlit_company_list_input]
number_of_symbols = len(company_name_to_symbol)
start_date = st.date_input(
"Start Date",
format="YYYY-MM-DD",
value=pd.Timestamp("1947-08-15"),
max_value=pd.Timestamp.now(),
)
initial_investment = st.number_input("How much would you want to invest?", value=45000)
if number_of_symbols > 1:
company_data = pd.DataFrame()
for cname in company_name_to_symbol:
stock_data_temp = yf.download(
cname, start=start_date, end=pd.Timestamp.now().strftime("%Y-%m-%d")
)["Adj Close"]
stock_data_temp.name = cname
company_data = pd.merge(
company_data,
stock_data_temp,
how="outer",
right_index=True,
left_index=True,
)
for i in company_data.columns:
company_data.dropna(axis=1, how="all", inplace=True)
company_data.dropna(inplace=True)
st.write(
f"Note: Due to unavailability of full data, this Analysis uses data from the date: {company_data.index[0]}"
)
number_of_symbols = len(company_data.columns)
st.dataframe(company_data, use_container_width=True)
if number_of_symbols > 1:
company_stock_returns_data = company_data.pct_change().dropna()
mu = expected_returns.mean_historical_return(company_data)
S = risk_models.sample_cov(company_data)
ef = EfficientFrontier(mu, S)
if optimization_methods == "Maximum Sharpe Raio":
ef.max_sharpe()
elif optimization_methods == "Minimum Volatility":
ef.min_volatility()
elif optimization_methods == "Efficient Risk":
ef.efficient_risk(0.5)
else:
ef.efficient_return(0.05)
company_asset_weights = pd.DataFrame.from_dict(
ef.clean_weights(), orient="index"
).reset_index()
company_asset_weights.columns = ["Ticker", "Allocation"]
company_asset_weights_copy = company_asset_weights
company_asset_weights["Name"] = [
symbol_to_name_dict[i] for i in company_asset_weights["Ticker"]
]
company_asset_weights = company_asset_weights[["Name", "Ticker", "Allocation"]]
st.dataframe(company_asset_weights, use_container_width=True)
ef.portfolio_performance()
(
expected_annual_return,
annual_volatility,
sharpe_ratio,
) = ef.portfolio_performance()
st_portfolio_performance = pd.DataFrame.from_dict(
{
"Expected annual return": (expected_annual_return * 100).round(2),
"Annual volatility": (annual_volatility * 100).round(2),
"Sharpe ratio": sharpe_ratio.round(2),
},
orient="index",
).reset_index()
st_portfolio_performance.columns = ["Metrics", "Summary"]
st.write("Optimization Method - ", optimization_methods)
st.dataframe(st_portfolio_performance, use_container_width=True)
plots.pie_chart_company_asset_weights(company_asset_weights)
portfolio_returns = (
company_stock_returns_data * list(ef.clean_weights().values())
).sum(axis=1)
annual_portfolio_returns = portfolio_returns.resample("Y").apply(
lambda x: (x + 1).prod() - 1
)
cumulative_returns = (portfolio_returns + 1).cumprod() * initial_investment
tab1, tab2 = st.tabs(["Plots", "Tables"])
with tab1:
plots.plot_annual_returns(annual_portfolio_returns)
plots.plot_cummulative_returns(cumulative_returns)
with tab2:
summary_tables.annual_returns_dataframe(annual_portfolio_returns)
summary_tables.cumulative_returns_dataframe(cumulative_returns)
|