-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
220 lines (157 loc) · 7.56 KB
/
main.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Imports
import streamlit as st
import pandas as pd
import numpy as np
import time
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
from streamlit.components.v1 import html
from functions import portfolio_optimization as po
# ------------------------------------------------------------------------------------------------------------------------------------
#Set Page Configurations
# ------------------------------------------------------------------------------------------------------------------------------------
st.set_page_config(
page_title = "Portfolio Optimization",
page_icon = "📊",
layout = "wide"
)
# ------------------------------------------------------------------------------------------------------------------------------------
# Upload a file
# ------------------------------------------------------------------------------------------------------------------------------------
c1_,c2_,c3_= st.columns([1,1,1])
c_1,c_2,c_3= st.columns([1,6,1])
c2_.title("**Portfolio :blue[Optimization]**")
with st.sidebar:
st.title("**Portfolio :blue[Optimization]**")
uploaded_file = c_2.file_uploader(":blue[Upload your .csv file with assets prices]")
c_2.write("---")
c_2.info('''**Ensure your .csv file matches the provided example to avoid errors.**\n
⚠️FORMAT: [time column: MM-DD-YYYY, Asset name columns: 0.000]''', icon="📝")
c_2.write("***Example .csv***")
c_2.image("images/image.png")
if uploaded_file is not None:
progress_text = "Operation in progress. Please wait."
my_bar = c_2.progress(0, text=progress_text)
for percent_complete in range(100):
time.sleep(0.01)
my_bar.progress(percent_complete + 1, text=progress_text)
my_bar.empty()
up_done = c_2.success('Uploaded', icon="✅")
time.sleep(1.0)
up_done.empty()
df = pd.DataFrame()
df = pd.read_csv(uploaded_file)
# Setting Date as index
df['time'] = pd.to_datetime(df['time'], format='%m-%d-%Y')
df = df.set_index(df['time'].values)
# Removing the Date Column
df.drop(columns=['time'], axis=1, inplace=True)
if uploaded_file is None:
c_2.warning('Please upload a file.')
st.stop()
# PLOT
c_2.subheader("The Modern Portfolio Theory (MPT)",
help = '''The Modern Portfolio Theory (MPT) refers to an investment theory
that allows investors to assemble an asset portfolio that maximizes expected return for a given level of risk.
The theory assumes that investors are risk-averse; for a given level of expected return, investors will always prefer the less risky portfolio.''')
c_2.pyplot(fig)
# ------------------------------------------------------------------------------------------------------------------------------------
# INPUTS
# ------------------------------------------------------------------------------------------------------------------------------------
c_2.write("###")
c_2.write("---")
c_2.write("###")
if uploaded_file is not None:
c_2.subheader("Method of Optimization")
method = c_2.radio(
"Set a method and Run optimization ⚙️",
key = "visibility",
options = ["Sharpe", "Min Volatility"],
horizontal = True
)
values = c_2.slider(
"Select a range of weights %",
0.0, 1.0, (0.05, 0.65)
)
# Calculate expected returns and sample covariance
mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df)
# Optimize for maximal Sharpe ratio
ef = EfficientFrontier(mu, S, weight_bounds=values)
button = c_2.button("Run optimization")
if not button:
st.stop()
if method == "Sharpe" and button:
c_2.write("---")
c_2.subheader(f"Optimal :blue[{method}] Weights")
c_2.write("###")
weights = ef.max_sharpe()
cleaned_weights = ef.clean_weights()
c_2.bar_chart(cleaned_weights, use_container_width=True, height=500)
c_2.dataframe(cleaned_weights, use_container_width = True)
c_2.write("---")
if method == "Min Volatility" and button:
c_2.write("---")
c_2.subheader(f"Optimal :blue[{method}] Weights")
c_2.write("###")
weights = ef.min_volatility()
cleaned_weights = ef.clean_weights()
c_2.bar_chart(cleaned_weights, use_container_width=True, height=500)
c_2.dataframe(cleaned_weights, use_container_width = True)
c_2.write("---")
with c_2:
po.plot_ef(5000, ef, method)
weights = pd.DataFrame({'Weights': weights})
# ------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------
# Portfolio Performance
# ------------------------------------------------------------------------------------------------------------------------------------
c_2.write("---")
c_2.subheader("Portfolio :blue[Performance]")
asset_names = df.columns
for asset in df.columns:
df[asset] = df[asset] * weights.loc[asset, 'Weights']
df['Portfolio'] = df.sum(axis=1)
final_df = df[['Portfolio']]
maxdd_ = np.round(po.max_drawdown(final_df['Portfolio'])*100, 2)
maxdd_rol = po.rol_max_drawdown(final_df['Portfolio']*100)
cum_ret = np.round((np.exp(np.log1p(final_df.pct_change()['Portfolio']).cumsum()).iloc[-1]) * 100, 2)
dail_vol = np.round(po.daily_vol(final_df).iloc[0]*100, 2)
monthly_vol = np.round(po.daily_vol(final_df).iloc[0]*100 * np.sqrt(30), 2)
anuall_vol = np.round(po.daily_vol(final_df).iloc[0]*100 * np.sqrt(365), 2)
sharpe = np.round(po.sharpe(final_df).iloc[0], 2)
sortino = np.round(po.sortino(final_df).iloc[0], 2)
omega = np.round(po.omega(final_df).iloc[0], 2)
# Portfolio Metrics Plot
with c_2:
st.area_chart(final_df/10)
st.write("---")
c1,c2,c3= st.columns([2,3,1])
c1.write(f"Cumulative returns:"), c2.write(f"{cum_ret} %")
c1.write("---"), c2.write("---")
c1.write(f"Sharpe Ratio:"), c2.write(f"{sharpe}")
c1.write(f"Sortino Ratio:"), c2.write(f"{sortino}")
c1.write(f"Omega Ratio:"), c2.write(f"{omega}")
c1.write("---"), c2.write("---")
c1.write(f"AVG Daily Volatility:"), c2.write(f"{dail_vol} %")
c1.write(f"AVG Monthly Volatility:"), c2.write(f"{monthly_vol} %")
c1.write(f"AVG Anuall Volatility:"), c2.write(f"{anuall_vol} %")
c1.write("---"), c2.write("---")
c1.write(f"Max Draw Down:"), c2.write(f"{maxdd_} %")
st.area_chart(maxdd_rol*100, color="#d14747")
else:
st.stop()
c1_,c2_,c3_= st.columns([0.5,6,0.5])
c2_.write("---")
c2_.write("**About**")
c2_.write(
'''
Created by @VanHe1sing
TradingView: https://www.tradingview.com/u/VanHe1sing/#published-scripts
Telegram: https://t.me/IvanKocherzhat
X: https://x.com/sxJEoRg7wwLR6ug
*This site is for informational purposes only.*\n
*· The information on our website is not financial advice, and you should not consider it to be financial advice.*
'''
)