Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,2 @@
|
|
1 |
-
import
|
2 |
-
|
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 |
-
from datetime import datetime
|
14 |
-
|
15 |
-
def plot_cum_returns(data, title, initial_capital=1000):
|
16 |
-
# ์ผ์ผ ๋์ ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์๊ฐํ
|
17 |
-
daily_cum_returns = (1 + data.dropna().pct_change()).cumprod() * initial_capital
|
18 |
-
fig = px.line(daily_cum_returns, title=title)
|
19 |
-
return fig
|
20 |
-
|
21 |
-
def plot_efficient_frontier_and_max_sharpe(mu, S):
|
22 |
-
# ์ต๋ ์คํ ๋น์จ๋ก ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๋ฐ ํจ์จ์ ํฌ์์ ๊ทธ๋ฆฌ๊ธฐ
|
23 |
-
ef = EfficientFrontier(mu, S)
|
24 |
-
fig, ax = plt.subplots(figsize=(6, 4))
|
25 |
-
ef_max_sharpe = copy.deepcopy(ef)
|
26 |
-
plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)
|
27 |
-
ef_max_sharpe.max_sharpe(risk_free_rate=0.02)
|
28 |
-
ret_tangent, std_tangent, _ = ef_max_sharpe.portfolio_performance()
|
29 |
-
ax.scatter(std_tangent, ret_tangent, marker="*", s=100, c="r", label="MAX Sharpe")
|
30 |
-
ax.legend()
|
31 |
-
return fig
|
32 |
-
|
33 |
-
def plot_weights(weights):
|
34 |
-
# ํฌํธํด๋ฆฌ์ค ์ต์ ํฌ์ ๋น์จ ๊ทธ๋ํ ์ถ๋ ฅ
|
35 |
-
labels = weights.keys()
|
36 |
-
sizes = weights.values()
|
37 |
-
fig, ax = plt.subplots()
|
38 |
-
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
|
39 |
-
ax.axis('equal')
|
40 |
-
return fig
|
41 |
-
|
42 |
-
def output_results(start_date, end_date, tickers_string):
|
43 |
-
tickers = tickers_string.split(',')
|
44 |
-
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
45 |
-
fig_indiv_prices = px.line(stocks_df, title='๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ')
|
46 |
-
fig_cum_returns = plot_cum_returns(stocks_df, '๊ฐ๋ณ ์ฃผ์์ ๋์ ์์ต๋ฅ ($1,000 ์์)')
|
47 |
-
corr_df = stocks_df.corr().round(2)
|
48 |
-
fig_corr = px.imshow(corr_df, text_auto=True, title='์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ')
|
49 |
-
mu = expected_returns.mean_historical_return(stocks_df)
|
50 |
-
S = risk_models.sample_cov(stocks_df)
|
51 |
-
ef = EfficientFrontier(mu, S)
|
52 |
-
weights = ef.max_sharpe(risk_free_rate=0.02)
|
53 |
-
cleaned_weights = ef.clean_weights()
|
54 |
-
fig_weights = plot_weights(cleaned_weights)
|
55 |
-
expected_annual_return, annual_volatility, sharpe_ratio = ef.portfolio_performance()
|
56 |
-
fig_efficient_frontier = plot_efficient_frontier_and_max_sharpe(mu, S)
|
57 |
-
|
58 |
-
return fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices, fig_weights, \
|
59 |
-
f"{expected_annual_return*100:.2f}%", f"{annual_volatility*100:.2f}%", f"{sharpe_ratio:.2f}"
|
60 |
-
|
61 |
-
css = """
|
62 |
-
footer {
|
63 |
-
visibility: hidden;
|
64 |
-
}
|
65 |
-
"""
|
66 |
-
|
67 |
-
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
68 |
-
with gr.Blocks(css=css) as app:
|
69 |
-
gr.Markdown("""
|
70 |
-
<style>
|
71 |
-
.markdown-text h2 {
|
72 |
-
font-size: 18px; # ํฐํธ ํฌ๊ธฐ๋ฅผ 18px๋ก ์ค์
|
73 |
-
}
|
74 |
-
</style>
|
75 |
-
<h2>AIQ Asset Portpolio: ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์,์ง์,BTC,์ํ ๋ฑ) AI ํฌํธํด๋ฆฌ์ค ์ต์ ํ ์๋น์ค</h2>
|
76 |
-
<h2>์ ์ธ๊ณ ๋ชจ๋ ํฐ์ปค ๋ณด๊ธฐ(์ผํ ํ์ด๋ธ์ค): <a href="https://finance.yahoo.com/most-active" target="_blank">https://finance.yahoo.com/most-active</a></h2>
|
77 |
-
""")
|
78 |
-
with gr.Row():
|
79 |
-
start_date = gr.Textbox("2013-01-01", label="์์ ์ผ์")
|
80 |
-
end_date = gr.Textbox(datetime.now().date(), label="์ข
๋ฃ ์ผ์")
|
81 |
-
tickers_string = gr.Textbox("NVDA,^GSPC,GC=F,MSFT,BTC-USD", label="์ฃผ์ ํฐ์ปค๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅํ์ธ์")
|
82 |
-
btn = gr.Button("ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๊ฒฐ๊ณผ ๋ณด๊ธฐ")
|
83 |
-
|
84 |
-
with gr.Row():
|
85 |
-
expected_annual_return = gr.Text(label="์์ ์ฐ๊ฐ ์์ต๋ฅ ")
|
86 |
-
annual_volatility = gr.Text(label="์ฐ๊ฐ ๋ณ๋์ฑ")
|
87 |
-
sharpe_ratio = gr.Text(label="์คํ ๋น์จ")
|
88 |
-
|
89 |
-
with gr.Column():
|
90 |
-
fig_cum_returns = gr.Plot(label="์ต์ ํ๋ ํฌํธํด๋ฆฌ์ค์ ๋์ ์์ต๋ฅ (์์ ๊ฐ๊ฒฉ $1,000)")
|
91 |
-
fig_efficient_frontier = gr.Plot(label="ํจ์จ์ ํฌ์์ ")
|
92 |
-
fig_corr = gr.Plot(label="์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ")
|
93 |
-
fig_indiv_prices = gr.Plot(label="๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ")
|
94 |
-
fig_weights = gr.Plot(label="ํฌํธํด๋ฆฌ์ค ์ต์ ํฌ์ ๋น์จ")
|
95 |
-
|
96 |
-
btn.click(fn=output_results, inputs=[start_date, end_date, tickers_string],
|
97 |
-
outputs=[fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices, fig_weights, expected_annual_return, annual_volatility, sharpe_ratio])
|
98 |
-
|
99 |
-
app.launch()
|
100 |
-
|
|
|
1 |
+
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|