luisotorres
commited on
Added Legend to Cumulative Returns Plot
Browse files- functions.py +11 -5
functions.py
CHANGED
@@ -17,6 +17,7 @@ init_notebook_mode(connected=True)
|
|
17 |
# Hiding Warnings
|
18 |
import warnings
|
19 |
warnings.filterwarnings('ignore')
|
|
|
20 |
def perform_portfolio_analysis(df, tickers_weights):
|
21 |
"""
|
22 |
This function takes historical stock data and the weights of the securities in the portfolio,
|
@@ -58,7 +59,7 @@ def perform_portfolio_analysis(df, tickers_weights):
|
|
58 |
individual_sharpe[ticker] = sharpe # Adding Sharpe Ratio for each ticker
|
59 |
|
60 |
# Creating subplots for comparison across securities
|
61 |
-
fig1 = make_subplots(rows = 1, cols = 2, horizontal_spacing=0.
|
62 |
column_titles=['Historical Performance Assets', 'Risk-Reward'],
|
63 |
column_widths=[.55, .45],
|
64 |
shared_xaxes=False, shared_yaxes=False)
|
@@ -70,7 +71,7 @@ def perform_portfolio_analysis(df, tickers_weights):
|
|
70 |
mode = 'lines',
|
71 |
name = ticker,
|
72 |
hovertemplate = '%{y:.2f}%',
|
73 |
-
showlegend=
|
74 |
row=1, col=1)
|
75 |
|
76 |
# Defining colors for markers on the second subplot
|
@@ -99,7 +100,9 @@ def perform_portfolio_analysis(df, tickers_weights):
|
|
99 |
},
|
100 |
template = 'plotly_white',
|
101 |
height = 650, width = 1250,
|
102 |
-
hovermode = 'x unified'
|
|
|
|
|
103 |
|
104 |
fig1.update_yaxes(title_text='Returns (%)', col=1)
|
105 |
fig1.update_yaxes(title_text='Returns (%)', col = 2)
|
@@ -152,7 +155,7 @@ def portfolio_vs_benchmark(port_returns, benchmark_returns):
|
|
152 |
benchmark_sharpe = (exces_benchmark_returns.mean() / benchmark_returns.std() * np.sqrt(252)).round(2)
|
153 |
|
154 |
# Creating a subplot to compare portfolio performance with the benchmark
|
155 |
-
fig2 = make_subplots(rows = 1, cols = 2, horizontal_spacing=0.
|
156 |
column_titles=['Cumulative Returns', 'Portfolio Risk-Reward'],
|
157 |
column_widths=[.55, .45],
|
158 |
shared_xaxes=False, shared_yaxes=False)
|
@@ -195,7 +198,10 @@ def portfolio_vs_benchmark(port_returns, benchmark_returns):
|
|
195 |
},
|
196 |
template = 'plotly_white',
|
197 |
height = 650, width = 1250,
|
198 |
-
hovermode = 'x unified'
|
|
|
|
|
|
|
199 |
|
200 |
fig2.update_yaxes(title_text='Cumulative Returns (%)', col=1)
|
201 |
fig2.update_yaxes(title_text='Cumulative Returns (%)', col = 2)
|
|
|
17 |
# Hiding Warnings
|
18 |
import warnings
|
19 |
warnings.filterwarnings('ignore')
|
20 |
+
|
21 |
def perform_portfolio_analysis(df, tickers_weights):
|
22 |
"""
|
23 |
This function takes historical stock data and the weights of the securities in the portfolio,
|
|
|
59 |
individual_sharpe[ticker] = sharpe # Adding Sharpe Ratio for each ticker
|
60 |
|
61 |
# Creating subplots for comparison across securities
|
62 |
+
fig1 = make_subplots(rows = 1, cols = 2, horizontal_spacing=0.25,
|
63 |
column_titles=['Historical Performance Assets', 'Risk-Reward'],
|
64 |
column_widths=[.55, .45],
|
65 |
shared_xaxes=False, shared_yaxes=False)
|
|
|
71 |
mode = 'lines',
|
72 |
name = ticker,
|
73 |
hovertemplate = '%{y:.2f}%',
|
74 |
+
showlegend=True),
|
75 |
row=1, col=1)
|
76 |
|
77 |
# Defining colors for markers on the second subplot
|
|
|
100 |
},
|
101 |
template = 'plotly_white',
|
102 |
height = 650, width = 1250,
|
103 |
+
hovermode = 'x unified',
|
104 |
+
legend_x=.45,
|
105 |
+
legend_y=.5)
|
106 |
|
107 |
fig1.update_yaxes(title_text='Returns (%)', col=1)
|
108 |
fig1.update_yaxes(title_text='Returns (%)', col = 2)
|
|
|
155 |
benchmark_sharpe = (exces_benchmark_returns.mean() / benchmark_returns.std() * np.sqrt(252)).round(2)
|
156 |
|
157 |
# Creating a subplot to compare portfolio performance with the benchmark
|
158 |
+
fig2 = make_subplots(rows = 1, cols = 2, horizontal_spacing=0.25,
|
159 |
column_titles=['Cumulative Returns', 'Portfolio Risk-Reward'],
|
160 |
column_widths=[.55, .45],
|
161 |
shared_xaxes=False, shared_yaxes=False)
|
|
|
198 |
},
|
199 |
template = 'plotly_white',
|
200 |
height = 650, width = 1250,
|
201 |
+
hovermode = 'x unified',
|
202 |
+
#legend_x=.45,
|
203 |
+
#legend_y=.5
|
204 |
+
)
|
205 |
|
206 |
fig2.update_yaxes(title_text='Cumulative Returns (%)', col=1)
|
207 |
fig2.update_yaxes(title_text='Cumulative Returns (%)', col = 2)
|