Spaces:
Sleeping
Sleeping
Commit
·
b859bf8
1
Parent(s):
d6536da
pie chart
Browse files- Notebooks/MAexp.py +2 -0
- main.py +34 -31
- utilities/__init__.py +0 -0
- utilities/checker.py +26 -0
Notebooks/MAexp.py
CHANGED
@@ -4,6 +4,7 @@ import yfinance as yf
|
|
4 |
import streamlit as st
|
5 |
import plotly.graph_objects as go
|
6 |
import time
|
|
|
7 |
import datetime
|
8 |
|
9 |
with open(r"../style/style.css") as css:
|
@@ -75,6 +76,7 @@ com_sel = [company_dict[i] for i in com_sel_name]
|
|
75 |
|
76 |
num_tick = len(com_sel)
|
77 |
|
|
|
78 |
if num_tick > 1:
|
79 |
com_data = pd.DataFrame()
|
80 |
for cname, cdate in zip(com_sel, com_sel_date):
|
|
|
4 |
import streamlit as st
|
5 |
import plotly.graph_objects as go
|
6 |
import time
|
7 |
+
from utilities import checker
|
8 |
import datetime
|
9 |
|
10 |
with open(r"../style/style.css") as css:
|
|
|
76 |
|
77 |
num_tick = len(com_sel)
|
78 |
|
79 |
+
|
80 |
if num_tick > 1:
|
81 |
com_data = pd.DataFrame()
|
82 |
for cname, cdate in zip(com_sel, com_sel_date):
|
main.py
CHANGED
@@ -279,45 +279,48 @@ if num_tick > 1:
|
|
279 |
|
280 |
st.markdown("<h1 style='text-align: center;'>Plotting</h1>", unsafe_allow_html=True)
|
281 |
|
|
|
|
|
282 |
fig = go.Figure(
|
283 |
-
data=go.
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
)
|
289 |
)
|
290 |
|
|
|
|
|
291 |
# Add color bar
|
292 |
fig.update_layout(coloraxis_colorbar=dict(title="Sharpe Ratio"))
|
293 |
|
294 |
# Add title and axis labels
|
295 |
fig.update_layout(
|
296 |
-
title="Portfolio
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
# Plot the Max Sharpe Ratio, using a `Red Star`.
|
302 |
-
fig.add_trace(
|
303 |
-
go.Scatter(
|
304 |
-
x=[max_sharpe_ratio[1]],
|
305 |
-
y=[max_sharpe_ratio[0]],
|
306 |
-
mode="markers",
|
307 |
-
marker=dict(color="red", symbol="star", size=20),
|
308 |
-
name="Max Sharpe Ratio",
|
309 |
-
)
|
310 |
-
)
|
311 |
-
|
312 |
-
# Plot the Min Volatility, using a `Blue Star`.
|
313 |
-
fig.add_trace(
|
314 |
-
go.Scatter(
|
315 |
-
x=[min_volatility[1]],
|
316 |
-
y=[min_volatility[0]],
|
317 |
-
mode="markers",
|
318 |
-
marker=dict(color="blue", symbol="star", size=20),
|
319 |
-
name="Min Volatility",
|
320 |
-
)
|
321 |
)
|
322 |
-
|
323 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
279 |
|
280 |
st.markdown("<h1 style='text-align: center;'>Plotting</h1>", unsafe_allow_html=True)
|
281 |
|
282 |
+
|
283 |
+
# plot a pie chart using plotly for max sharpe ratio
|
284 |
fig = go.Figure(
|
285 |
+
data=go.Pie(
|
286 |
+
labels=com_sel_name_temp,
|
287 |
+
values=max_sharpe_ratio["Portfolio Weights"],
|
288 |
+
hole=0.3,
|
289 |
+
textinfo='percent+label', # Information to display on the pie slices
|
290 |
+
hoverinfo='label+percent',# Information to display on hover
|
291 |
+
marker=dict( line=dict(color='white', width=2))
|
292 |
+
)
|
293 |
+
)
|
294 |
+
|
295 |
+
# update colors
|
296 |
+
fig.update_traces(
|
297 |
+
marker=dict(
|
298 |
+
colors=[
|
299 |
+
"lightseagreen",
|
300 |
+
"lightcoral",
|
301 |
+
"lightskyblue",
|
302 |
+
"lightgreen",
|
303 |
+
"lightpink",
|
304 |
+
"lightyellow",
|
305 |
+
"lightblue",
|
306 |
+
"lightgrey",
|
307 |
+
"lightgoldenrodyellow",
|
308 |
+
"lightcyan",
|
309 |
+
]
|
310 |
)
|
311 |
)
|
312 |
|
313 |
+
# update layout of the pie chart
|
314 |
+
|
315 |
# Add color bar
|
316 |
fig.update_layout(coloraxis_colorbar=dict(title="Sharpe Ratio"))
|
317 |
|
318 |
# Add title and axis labels
|
319 |
fig.update_layout(
|
320 |
+
title="Portfolio Composition",
|
321 |
+
showlegend=False,
|
322 |
+
height=500,
|
323 |
+
width=700,
|
324 |
+
margin=dict(l=50, r=50, t=50, b=50),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
)
|
|
|
326 |
st.plotly_chart(fig, use_container_width=True)
|
utilities/__init__.py
ADDED
File without changes
|
utilities/checker.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
|
3 |
+
# 1. Check if the company is listed on Yahoo Finance
|
4 |
+
def check_company(company_dict):
|
5 |
+
com_sel = []
|
6 |
+
for i in company_dict.keys():
|
7 |
+
if yf.Ticker(company_dict[i]).info:
|
8 |
+
com_sel.append(i)
|
9 |
+
|
10 |
+
return com_sel
|
11 |
+
|
12 |
+
#2. make a function to calculate moving averages from the dataframe com_data, store those moving averages in dictionary for respective company
|
13 |
+
def moving_average(data, window):
|
14 |
+
ma = {}
|
15 |
+
for i in data.columns:
|
16 |
+
ma[i] = data[i].rolling(window=window).mean().values[2]
|
17 |
+
return ma
|
18 |
+
|
19 |
+
|
20 |
+
# calculate percentage return till present date from the moving average price of the stock
|
21 |
+
def percentage_return(data, moving_avg):
|
22 |
+
pr = {}
|
23 |
+
for i in data.columns:
|
24 |
+
pr[i] = f'{round(((data[i].values[-1] - moving_avg[i]) / moving_avg[i]) * 100,2) }%'
|
25 |
+
return pr
|
26 |
+
|