-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (184 loc) · 11 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
import streamlit as st
from postgres.storage import register, login, load_ticker_history, connect
from markowitz import minimize_risk, maximize_return, getMaxReturn, getMinRisk, getStocksData, getMinReturn, getMaxRisk, getPortfolioHistory, get_sigma, get_mu
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from config.reader import *
DETAILED_PIE = False
st.set_page_config(page_icon=":money_with_wings:", page_title="EMALAK Invest", layout="centered", initial_sidebar_state="collapsed")
sns.set_theme()
sns.set_context("notebook", font_scale=0.85, rc={"lines.linewidth": 2.5})
plt.rcParams['figure.facecolor'] = 'none'
cfg = read_config('config.json')
conn = connect()
def prettify_weighs(df):
return df.sort_values(axis=1, by='Доля в портфеле (%)', ascending=False)
def build_pie(df):
if df.shape[1] > 7 and not DETAILED_PIE:
tickers = list(df.columns.values)
values = list(df.values.tolist())
top_tickers = tickers[:6]
top_values = values[0][:6]
other_values = values[0][6:]
top_tickers.append("Другие акции")
top_values.append(sum(other_values))
fig, ax = plt.subplots()
ax.pie(top_values, autopct='%1.2f%%')
ax.axis('equal')
ax.legend(labels=top_tickers)
return fig
fig, ax = plt.subplots()
ax.pie(df.iloc[0], autopct='%1.2f%%')
ax.axis('equal')
ax.legend(labels=df.columns.values)
return fig
def LogInClicked(username, password):
if login(username, password):
st.session_state['loggedIn'] = True
st.session_state['username'] = username
st.session_state['password'] = password
#print('loggedIn')
else:
#st.session_state['loggedIn'] = False
st.error('Неверный пароль или имя пользователя')
def RegisterClicked(username, password):
if register(username, password):
st.session_state['loggedIn'] = True
st.session_state['username'] = username
st.session_state['password'] = password
#print('loggedIn')
else:
#st.session_state['loggedIn'] = False
st.error('Неизвестная ошибка, попробуйте другое имя пользователя')
def goToRegistration():
st.session_state.status = 'register'
def show_login_page():
if st.session_state['loggedIn'] == False and st.session_state.status == 'login':
st.title('Вход в аккаунт')
username = st.text_input(label="username", label_visibility='hidden', value="", placeholder="Имя пользователя")
password = st.text_input(label="password", label_visibility='hidden', value="", type="password", placeholder="Пароль")
col1, col2 = st.columns(2)
with col1:
st.button("Войти", on_click=lambda: LogInClicked(username, password))
with col2:
st.button("Зарегистрироваться", on_click=lambda: goToRegistration())
def LogOutClicked():
st.session_state['loggedIn'] = False
def show_main_page():
st.title(':money: EMALAK Invest')
st.title('Здравствуйте, ' + st.session_state['username'])
col1, col2 = st.columns([1,1,])
with col1:
fromDate = st.date_input('От', value=datetime.date(2018, 1, 1), min_value=datetime.date(2015, 1, 1))
with col2:
toDate = st.date_input('До', min_value=fromDate, max_value=datetime.date(2023, 3, 16))
if ('stocks' not in st.session_state or 'fromDate' not in st.session_state or 'toDate' not in st.session_state) or (st.session_state.fromDate != fromDate or st.session_state.toDate != toDate):
st.session_state.fromDate = fromDate
st.session_state.toDate = toDate
st.session_state.stocks = getStocksData(start=fromDate, end=toDate)
min_risk = float(getMinRisk(st.session_state.stocks)) * 100 + 0.005
max_risk = float(getMaxRisk(st.session_state.stocks)) * 100
st.session_state.max_risk = max_risk
st.session_state.min_risk = min_risk
st.session_state.target_risk = min_risk
max_return = float(getMaxReturn(st.session_state.stocks)) * 100 - 0.005
min_return = float(getMinReturn(st.session_state.stocks)) * 100 + 0.005
st.session_state.min_return = min_return
st.session_state.max_return = max_return
st.session_state.target_return = max_return
if 'max_risk' not in st.session_state or (st.session_state.fromDate != fromDate or st.session_state.toDate != toDate):
min_risk = float(getMinRisk(st.session_state.stocks)) * 100 + 0.005
max_risk = float(getMaxRisk(st.session_state.stocks)) * 100
st.session_state.max_risk = max_risk
st.session_state.min_risk = min_risk
if 'min_return' not in st.session_state or (st.session_state.fromDate != fromDate or st.session_state.toDate != toDate):
max_return = float(getMaxReturn(st.session_state.stocks)) * 100 - 0.005
min_return = float(getMinReturn(st.session_state.stocks)) * 100 + 0.005
st.session_state.min_return = min_return
st.session_state.max_return = max_return
st.session_state.target_return = max_return
if 'target_risk' not in st.session_state or (st.session_state.fromDate != fromDate or st.session_state.toDate != toDate):
st.session_state.target_risk = st.session_state.min_risk
if 'target_return' not in st.session_state or (st.session_state.fromDate != fromDate or st.session_state.toDate != toDate):
st.session_state.target_return = st.session_state.max_return
if 'deposit' not in st.session_state:
st.session_state.deposit = 10000
st.session_state.deposit = st.number_input('Депозит', min_value=10000, value=st.session_state.deposit)
profile = st.radio('Профиль', ['Максимизация доходности', 'Минимизация риска'], index = 0)
if profile == 'Максимизация доходности':
st.session_state.target_risk = st.slider('Максимальный риск',min_value=st.session_state.min_risk, max_value=st.session_state.max_risk, step=0.01, value=st.session_state.target_risk)
elif profile == 'Минимизация риска':
st.session_state.target_return = st.slider('Минимальная доходность',min_value=st.session_state.min_return, max_value=st.session_state.max_return, step=0.01, value=st.session_state.target_return)
clicked = st.button('Расчитать')
if clicked:
if profile == 'Максимизация доходности':
st.write('Расчет максимизации доходности при заданном риске в {:10.2f}%...'.format(st.session_state.target_risk))
port = maximize_return(stocks=st.session_state.stocks, target_risk=(st.session_state.target_risk / 100))
perf = port.portfolio_performance()
weights = port.clean_weights()
weights_cpy = weights.copy()
for key, value in weights_cpy.items():
if value <= 0:
del weights[key]
del weights_cpy
expected_annual_return = perf[0]
annual_risk = perf[1]
col1, col2 = st.columns(2)
col1.metric("Доходность", "%.2f" % (expected_annual_return * 100) + "%")
col2.metric("Риск", "%.2f" % (annual_risk * 100) + "%")
prweights = pd.DataFrame(weights, columns=weights.keys(), index=["Доля в портфеле (%)"])
prweights = prweights.select_dtypes(exclude=['object', 'datetime']) * 100
prweights = prettify_weighs(prweights)
st.table(prweights)
st.pyplot(build_pie(prweights),transparent=True)
st.title('Динамика портфеля')
history = getPortfolioHistory(st.session_state.deposit, weights, st.session_state.stocks)
st.line_chart(data=history, x='date', y='value')
elif profile == 'Минимизация риска':
st.write('Расчет минимизации риска при заданной доходности в {:10.2f}%...'.format(st.session_state.target_return))
port = minimize_risk(stocks=st.session_state.stocks, target_return=(st.session_state.target_return / 100))
perf = port.portfolio_performance()
weights = port.clean_weights()
weights_cpy = weights.copy()
for key, value in weights_cpy.items():
if value <= 0:
del weights[key]
del weights_cpy
expected_annual_return = perf[0]
annual_risk = perf[1]
col1, col2 = st.columns(2)
col1.metric("Доходность", "%.2f" % (expected_annual_return * 100) + "%")
col2.metric("Риск", "%.2f" % (annual_risk * 100) + "%")
prweights = pd.DataFrame(weights, columns=weights.keys(), index=["Доля в портфеле (%)"])
prweights = prweights.select_dtypes(exclude=['object', 'datetime']) * 100
prweights = prettify_weighs(prweights)
st.table(prweights)
st.pyplot(build_pie(prweights),transparent=True)
st.title('Динамика портфеля')
history = getPortfolioHistory(st.session_state.deposit, weights, st.session_state.stocks).rename(columns={'date': 'Дата', 'value': 'Сумма'})
st.line_chart(data=history, x='Дата', y='Сумма')
def show_register_page():
if st.session_state['loggedIn'] == False and st.session_state.status == 'register':
st.title('Регистрация')
username = st.text_input(label="username", key="regusername", label_visibility='hidden', value="", placeholder="Имя пользователя")
password = st.text_input(label="password", key="regpassword",label_visibility='hidden', value="", type="password", placeholder="Пароль")
st.button("Зарегистрироваться", key="regbutton", on_click=lambda: RegisterClicked(username, password))
if 'loggedIn' not in st.session_state:
st.session_state['loggedIn'] = False
st.session_state.status = 'login'
show_login_page()
else:
if st.session_state['loggedIn']:
show_main_page()
else:
if 'status' not in st.session_state:
st.session_state.status = 'login'
show_login_page()
else:
if st.session_state.status == 'login':
show_login_page()
elif st.session_state.status == 'register':
show_register_page()