aiqtech commited on
Commit
ee6af49
ยท
verified ยท
1 Parent(s): a5272ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
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.Row():
102
- start_date = gr.Textbox("2013-01-01", label="์‹œ์ž‘ ์ผ์ž")
103
- end_date = gr.Textbox(datetime.now().date(), label="์ข…๋ฃŒ ์ผ์ž")
104
- tickers_string = gr.Textbox("NVDA,^GSPC,GC=F,MSFT,BTC-USD", label="์ฃผ์‹ ํ‹ฐ์ปค๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•˜์„ธ์š”")
105
- btn = gr.Button("ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™” ๊ฒฐ๊ณผ ๋ณด๊ธฐ")
106
-
107
- with gr.Row():
108
- expected_annual_return = gr.Text(label="์˜ˆ์ƒ ์—ฐ๊ฐ„ ์ˆ˜์ต๋ฅ ")
109
- annual_volatility = gr.Text(label="์—ฐ๊ฐ„ ๋ณ€๋™์„ฑ")
110
- sharpe_ratio = gr.Text(label="์ƒคํ”„ ๋น„์œจ")
111
- with gr.Column():
112
- fig_cum_returns = gr.Plot(label="์ตœ์ ํ™”๋œ ํฌํŠธํด๋ฆฌ์˜ค์˜ ๋ˆ„์  ์ˆ˜์ต๋ฅ  (์‹œ์ž‘ ๊ฐ€๊ฒฉ $1,000)")
113
- fig_efficient_frontier = gr.Plot(label="ํšจ์œจ์  ํˆฌ์ž์„ ")
114
- fig_corr = gr.Plot(label="์ฃผ์‹ ๊ฐ„ ์ƒ๊ด€ ๊ด€๊ณ„")
115
- fig_indiv_prices = gr.Plot(label="๊ฐœ๋ณ„ ์ฃผ์‹ ๊ฐ€๊ฒฉ")
116
- fig_weights = gr.Plot(label="ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์  ํˆฌ์ž ๋น„์œจ")
117
- ticker_info_output = gr.Textbox(label="ํ‹ฐ์ปค ์ •๋ณด ๋ฐ ๋‰ด์Šค")
118
- 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])
119
-
120
- app.launch()
 
 
 
 
 
 
 
 
 
 
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()