'first'
Browse files- app.py +130 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yfinance as yf
|
3 |
+
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
|
4 |
+
from pypfopt import EfficientFrontier
|
5 |
+
from pypfopt import risk_models
|
6 |
+
from pypfopt import expected_returns
|
7 |
+
from pypfopt import plotting
|
8 |
+
import copy
|
9 |
+
import numpy as np
|
10 |
+
import pandas as pd
|
11 |
+
import plotly.express as px
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
import seaborn as sns
|
14 |
+
from datetime import datetime
|
15 |
+
from io import BytesIO
|
16 |
+
import datetime
|
17 |
+
|
18 |
+
def plot_cum_returns(data, title):
|
19 |
+
daily_cum_returns = 1 + data.dropna().pct_change()
|
20 |
+
daily_cum_returns = daily_cum_returns.cumprod()*100
|
21 |
+
fig = px.line(daily_cum_returns, title=title)
|
22 |
+
return fig
|
23 |
+
|
24 |
+
def plot_efficient_frontier_and_max_sharpe(mu, S):
|
25 |
+
# Optimize portfolio for max Sharpe ratio and plot it out with efficient frontier curve
|
26 |
+
ef = EfficientFrontier(mu, S)
|
27 |
+
fig, ax = plt.subplots(figsize=(6,4))
|
28 |
+
ef_max_sharpe = copy.deepcopy(ef)
|
29 |
+
plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)
|
30 |
+
# Find the max sharpe portfolio
|
31 |
+
ef_max_sharpe.max_sharpe(risk_free_rate=0.02)
|
32 |
+
ret_tangent, std_tangent, _ = ef_max_sharpe.portfolio_performance()
|
33 |
+
ax.scatter(std_tangent, ret_tangent, marker="*", s=100, c="r", label="Max Sharpe")
|
34 |
+
# Generate random portfolios
|
35 |
+
n_samples = 1000
|
36 |
+
w = np.random.dirichlet(np.ones(ef.n_assets), n_samples)
|
37 |
+
rets = w.dot(ef.expected_returns)
|
38 |
+
stds = np.sqrt(np.diag(w @ ef.cov_matrix @ w.T))
|
39 |
+
sharpes = rets / stds
|
40 |
+
ax.scatter(stds, rets, marker=".", c=sharpes, cmap="viridis_r")
|
41 |
+
# Output
|
42 |
+
ax.legend()
|
43 |
+
return fig
|
44 |
+
|
45 |
+
def output_results(start_date, end_date, tickers_string):
|
46 |
+
tickers = tickers_string.split(',')
|
47 |
+
|
48 |
+
# Get Stock Prices
|
49 |
+
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
50 |
+
|
51 |
+
# Plot Individual Stock Prices
|
52 |
+
fig_indiv_prices = px.line(stocks_df, title='Price of Individual Stocks')
|
53 |
+
|
54 |
+
# Plot Individual Cumulative Returns
|
55 |
+
fig_cum_returns = plot_cum_returns(stocks_df, 'Cumulative Returns of Individual Stocks Starting with $100')
|
56 |
+
|
57 |
+
# Calculatge and Plot Correlation Matrix between Stocks
|
58 |
+
corr_df = stocks_df.corr().round(2)
|
59 |
+
fig_corr = px.imshow(corr_df, text_auto=True, title = 'Correlation between Stocks')
|
60 |
+
|
61 |
+
# Calculate expected returns and sample covariance matrix for portfolio optimization later
|
62 |
+
mu = expected_returns.mean_historical_return(stocks_df)
|
63 |
+
S = risk_models.sample_cov(stocks_df)
|
64 |
+
|
65 |
+
# Plot efficient frontier curve
|
66 |
+
fig_efficient_frontier = plot_efficient_frontier_and_max_sharpe(mu, S)
|
67 |
+
|
68 |
+
# Get optimized weights
|
69 |
+
ef = EfficientFrontier(mu, S)
|
70 |
+
ef.max_sharpe(risk_free_rate=0.02)
|
71 |
+
weights = ef.clean_weights()
|
72 |
+
expected_annual_return, annual_volatility, sharpe_ratio = ef.portfolio_performance()
|
73 |
+
|
74 |
+
expected_annual_return, annual_volatility, sharpe_ratio = '{}%'.format((expected_annual_return*100).round(2)), \
|
75 |
+
'{}%'.format((annual_volatility*100).round(2)), \
|
76 |
+
'{}%'.format((sharpe_ratio*100).round(2))
|
77 |
+
|
78 |
+
weights_df = pd.DataFrame.from_dict(weights, orient = 'index')
|
79 |
+
weights_df = weights_df.reset_index()
|
80 |
+
weights_df.columns = ['Tickers', 'Weights']
|
81 |
+
|
82 |
+
# Calculate returns of portfolio with optimized weights
|
83 |
+
stocks_df['Optimized Portfolio'] = 0
|
84 |
+
for ticker, weight in weights.items():
|
85 |
+
stocks_df['Optimized Portfolio'] += stocks_df[ticker]*weight
|
86 |
+
|
87 |
+
# Plot Cumulative Returns of Optimized Portfolio
|
88 |
+
fig_cum_returns_optimized = plot_cum_returns(stocks_df['Optimized Portfolio'], 'Cumulative Returns of Optimized Portfolio Starting with $100')
|
89 |
+
|
90 |
+
return fig_cum_returns_optimized, weights_df, fig_efficient_frontier, fig_corr, \
|
91 |
+
expected_annual_return, annual_volatility, sharpe_ratio, fig_indiv_prices, fig_cum_returns
|
92 |
+
|
93 |
+
|
94 |
+
with gr.Blocks() as app:
|
95 |
+
with gr.Row():
|
96 |
+
start_date = gr.Textbox("2013-01-01", label="Start Date")
|
97 |
+
end_date = gr.Textbox(datetime.datetime.now().date(), label="End Date")
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
tickers_string = gr.Textbox("MA,META,V,AMZN,JPM,BA",
|
101 |
+
label='Enter all stock tickers to be included in portfolio separated \
|
102 |
+
by commas WITHOUT spaces, e.g. "MA,META,V,AMZN,JPM,BA"')
|
103 |
+
btn = gr.Button("View Optimized Portfolio")
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
gr.Markdown("Optimzied Portfolio Metrics")
|
107 |
+
with gr.Row():
|
108 |
+
expected_annual_return = gr.Text(label="Expected Annual Return")
|
109 |
+
annual_volatility = gr.Text(label="Annual Volatility")
|
110 |
+
sharpe_ratio = gr.Text(label="Sharpe Ratio")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
fig_cum_returns_optimized = gr.Plot(label="Cumulative Returns of Optimized Portfolio (Starting Price of $100)")
|
114 |
+
weights_df = gr.DataFrame(label="Optimized Weights of Each Ticker")
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
fig_efficient_frontier = gr.Plot(label="Efficient Frontier")
|
118 |
+
fig_corr = gr.Plot(label="Correlation between Stocks")
|
119 |
+
|
120 |
+
|
121 |
+
with gr.Row():
|
122 |
+
fig_indiv_prices = gr.Plot(label="Price of Individual Stocks")
|
123 |
+
fig_cum_returns = gr.Plot(label="Cumulative Returns of Individual Stocks Starting with $100")
|
124 |
+
|
125 |
+
#demo.load(plot_indiv_stock_prices, inputs=[start_date, end_date, tickers_string], outputs=fig_indiv_price, examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])
|
126 |
+
btn.click(fn=output_results, inputs=[start_date, end_date, tickers_string],
|
127 |
+
outputs=[fig_cum_returns_optimized, weights_df, fig_efficient_frontier, fig_corr, \
|
128 |
+
expected_annual_return, annual_volatility, sharpe_ratio, fig_indiv_prices, fig_cum_returns])
|
129 |
+
|
130 |
+
app.launch(server_port=7888)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
PyPortfolioOpt
|
3 |
+
plotly
|
4 |
+
matplotlib
|
5 |
+
seaborn
|
6 |
+
yfinance
|