Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,22 @@ 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 |
# ์ผ์ผ ๋์ ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์๊ฐํ
|
@@ -38,13 +54,6 @@ def plot_weights(weights):
|
|
38 |
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
|
39 |
ax.axis('equal')
|
40 |
return fig
|
41 |
-
|
42 |
-
def plot_individual_volatility(data):
|
43 |
-
# ๊ฐ๋ณ ์ฃผ์์ ๋ณ๋์ฑ ๊ทธ๋ํ ์ถ๋ ฅ
|
44 |
-
rolling_std = data.pct_change().rolling(window=30).std() * np.sqrt(252)
|
45 |
-
fig = px.line(rolling_std, title='๊ฐ๋ณ ์ฃผ์์ ๋ณ๋์ฑ (30์ผ ๋กค๋ง ์ฐ๊ฐํ)')
|
46 |
-
return fig
|
47 |
-
|
48 |
def output_results(start_date, end_date, tickers_string):
|
49 |
tickers = tickers_string.split(',')
|
50 |
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
@@ -84,10 +93,8 @@ def output_results(start_date, end_date, tickers_string):
|
|
84 |
f"{expected_annual_return*100:.2f}%", f"{annual_volatility*100:.2f}%", f"{sharpe_ratio:.2f}", ticker_info_output
|
85 |
|
86 |
|
87 |
-
|
88 |
css = """footer { visibility: hidden; }"""
|
89 |
|
90 |
-
|
91 |
with gr.Blocks(css=css) as app:
|
92 |
gr.Markdown("""
|
93 |
<style>
|
@@ -98,23 +105,32 @@ with gr.Blocks(css=css) as app:
|
|
98 |
<h2>AIQ ์์ฐ ํฌํธํด๋ฆฌ์ค: ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์, ์ง์, BTC, ์ํ ๋ฑ) AI ํฌํธํด๋ฆฌ์ค ์ต์ ํ ์๋น์ค</h2>
|
99 |
<h2>์ ์ธ๊ณ ๋ชจ๋ ํฐ์ปค ๋ณด๊ธฐ(์ผํ ํ์ด๋ธ์ค): <a href="https://finance.yahoo.com/most-active" target="_blank">์ฌ๊ธฐ๋ฅผ ํด๋ฆญ</a></h2>
|
100 |
""")
|
101 |
-
with gr.
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
import plotly.express as px
|
12 |
import matplotlib.pyplot as plt
|
13 |
from datetime import datetime
|
14 |
+
from prophet import Prophet
|
15 |
+
|
16 |
+
def predict_future_prices(ticker, periods=1825): # 5๋
๊ฐ์ ๋ฐ์ดํฐ ์์ธก
|
17 |
+
data = yf.download(ticker, start="2010-01-01")['Adj Close'].reset_index()
|
18 |
+
data.rename(columns={'Date': 'ds', 'Adj Close': 'y'}, inplace=True)
|
19 |
+
|
20 |
+
model = Prophet(daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=True)
|
21 |
+
model.fit(data)
|
22 |
+
|
23 |
+
future = model.make_future_dataframe(periods=periods, freq='D')
|
24 |
+
forecast = model.predict(future)
|
25 |
+
|
26 |
+
fig = px.line(forecast, x='ds', y='yhat', title=f'5-Year Future Price Forecast for {ticker}')
|
27 |
+
return fig, forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]
|
28 |
+
|
29 |
+
|
30 |
|
31 |
def plot_cum_returns(data, title, initial_capital=1000):
|
32 |
# ์ผ์ผ ๋์ ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์๊ฐํ
|
|
|
54 |
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
|
55 |
ax.axis('equal')
|
56 |
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def output_results(start_date, end_date, tickers_string):
|
58 |
tickers = tickers_string.split(',')
|
59 |
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
|
|
93 |
f"{expected_annual_return*100:.2f}%", f"{annual_volatility*100:.2f}%", f"{sharpe_ratio:.2f}", ticker_info_output
|
94 |
|
95 |
|
|
|
96 |
css = """footer { visibility: hidden; }"""
|
97 |
|
|
|
98 |
with gr.Blocks(css=css) as app:
|
99 |
gr.Markdown("""
|
100 |
<style>
|
|
|
105 |
<h2>AIQ ์์ฐ ํฌํธํด๋ฆฌ์ค: ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์, ์ง์, BTC, ์ํ ๋ฑ) AI ํฌํธํด๋ฆฌ์ค ์ต์ ํ ์๋น์ค</h2>
|
106 |
<h2>์ ์ธ๊ณ ๋ชจ๋ ํฐ์ปค ๋ณด๊ธฐ(์ผํ ํ์ด๋ธ์ค): <a href="https://finance.yahoo.com/most-active" target="_blank">์ฌ๊ธฐ๋ฅผ ํด๋ฆญ</a></h2>
|
107 |
""")
|
108 |
+
with gr.Tabs():
|
109 |
+
with gr.TabItem("Portfolio Analysis"):
|
110 |
+
with gr.Row():
|
111 |
+
start_date = gr.Textbox("2013-01-01", label="์์ ์ผ์")
|
112 |
+
end_date = gr.Textbox(datetime.now().date(), label="์ข
๋ฃ ์ผ์")
|
113 |
+
tickers_string = gr.Textbox("NVDA,^GSPC,GC=F,MSFT,BTC-USD", label="์ฃผ์ ํฐ์ปค๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅํ์ธ์")
|
114 |
+
btn = gr.Button("ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๊ฒฐ๊ณผ ๋ณด๊ธฐ")
|
115 |
+
with gr.Row():
|
116 |
+
expected_annual_return = gr.Text(label="์์ ์ฐ๊ฐ ์์ต๋ฅ ")
|
117 |
+
annual_volatility = gr.Text(label="์ฐ๊ฐ ๋ณ๋์ฑ")
|
118 |
+
sharpe_ratio = gr.Text(label="์คํ ๋น์จ")
|
119 |
+
with gr.Column():
|
120 |
+
fig_cum_returns = gr.Plot(label="์ต์ ํ๋ ํฌํธํด๋ฆฌ์ค์ ๋์ ์์ต๋ฅ (์์ ๊ฐ๊ฒฉ $1,000)")
|
121 |
+
fig_efficient_frontier = gr.Plot(label="ํจ์จ์ ํฌ์์ ")
|
122 |
+
fig_corr = gr.Plot(label="์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ")
|
123 |
+
fig_indiv_prices = gr.Plot(label="๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ")
|
124 |
+
fig_weights = gr.Plot(label="ํฌํธํด๋ฆฌ์ค ์ต์ ํฌ์ ๋น์จ")
|
125 |
+
ticker_info_output = gr.Textbox(label="ํฐ์ปค ์ ๋ณด ๋ฐ ๋ด์ค")
|
126 |
+
btn.click(fn=output_results, inputs=[start_date, end_date, tickers_string], outputs=[fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices, fig_weights, expected_annual_return, annual_volatility, sharpe_ratio, ticker_info_output])
|
127 |
+
|
128 |
+
with gr.TabItem("Future Price Forecast"):
|
129 |
+
with gr.Column():
|
130 |
+
ticker_input = gr.Textbox(value="AAPL", label="Enter Stock Ticker for Forecast")
|
131 |
+
forecast_button = gr.Button("Generate Forecast")
|
132 |
+
forecast_chart = gr.Plot(label="Forecast Chart")
|
133 |
+
forecast_data = gr.Dataframe()
|
134 |
+
forecast_button.click(fn=predict_future_prices, inputs=[ticker_input], outputs=[forecast_chart, forecast_data])
|
135 |
+
|
136 |
+
app.launch()
|