aiqtech commited on
Commit
23c88d4
โ€ข
1 Parent(s): d8de096

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from datetime import datetime
14
+
15
+ def plot_cum_returns(data, title):
16
+ # ์ผ์ผ ๋ˆ„์  ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ ๋ฐ ์‹œ๊ฐํ™”
17
+ daily_cum_returns = 1 + data.dropna().pct_change()
18
+ daily_cum_returns = daily_cum_returns.cumprod() * 100
19
+ fig = px.line(daily_cum_returns, title=title)
20
+ return fig
21
+
22
+ def plot_efficient_frontier_and_max_sharpe(mu, S):
23
+ # ์ตœ๋Œ€ ์ƒคํ”„ ๋น„์œจ๋กœ ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™” ๋ฐ ํšจ์œจ์  ํˆฌ์ž์„  ๊ทธ๋ฆฌ๊ธฐ
24
+ ef = EfficientFrontier(mu, S)
25
+ fig, ax = plt.subplots(figsize=(6, 4))
26
+ ef_max_sharpe = copy.deepcopy(ef)
27
+ plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)
28
+ # ์ตœ๋Œ€ ์ƒคํ”„ ๋น„์œจ ํฌํŠธํด๋ฆฌ์˜ค ์ฐพ๊ธฐ
29
+ ef_max_sharpe.max_sharpe(risk_free_rate=0.02)
30
+ ret_tangent, std_tangent, _ = ef_max_sharpe.portfolio_performance()
31
+ ax.scatter(std_tangent, ret_tangent, marker="*", s=100, c="r", label="์ตœ๋Œ€ ์ƒคํ”„")
32
+ ax.legend()
33
+ return fig
34
+
35
+ def output_results(start_date, end_date, tickers_string):
36
+ # ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ตœ์ ํ™” ๊ฒฐ๊ณผ ์ถœ๋ ฅ
37
+ tickers = tickers_string.split(',')
38
+
39
+ # ์ฃผ์‹ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
40
+ stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
41
+
42
+ # ๊ฐœ๋ณ„ ์ฃผ์‹ ๊ฐ€๊ฒฉ ์‹œ๊ฐํ™”
43
+ fig_indiv_prices = px.line(stocks_df, title='๊ฐœ๋ณ„ ์ฃผ์‹ ๊ฐ€๊ฒฉ')
44
+
45
+ # ๊ฐœ๋ณ„ ์ฃผ์‹ ๋ˆ„์  ์ˆ˜์ต๋ฅ  ์‹œ๊ฐํ™”
46
+ fig_cum_returns = plot_cum_returns(stocks_df, '๊ฐœ๋ณ„ ์ฃผ์‹์˜ ๋ˆ„์  ์ˆ˜์ต๋ฅ  ($100 ์‹œ์ž‘)')
47
+
48
+ # ์ฃผ์‹ ๊ฐ„ ์ƒ๊ด€ ๊ด€๊ณ„ ๊ณ„์‚ฐ ๋ฐ ์‹œ๊ฐํ™”
49
+ corr_df = stocks_df.corr().round(2)
50
+ fig_corr = px.imshow(corr_df, text_auto=True, title='์ฃผ์‹ ๊ฐ„ ์ƒ๊ด€ ๊ด€๊ณ„')
51
+
52
+ # ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ๊ธฐ๋Œ€ ์ˆ˜์ต๋ฅ ๊ณผ ์ƒ˜ํ”Œ ๊ณต๋ถ„์‚ฐ ํ–‰๋ ฌ ๊ณ„์‚ฐ
53
+ mu = expected_returns.mean_historical_return(stocks_df)
54
+ S = risk_models.sample_cov(stocks_df)
55
+
56
+ # ํšจ์œจ์  ํˆฌ์ž์„  ์‹œ๊ฐํ™”
57
+ fig_efficient_frontier = plot_efficient_frontier_and_max_sharpe(mu, S)
58
+
59
+ return fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices
60
+
61
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
62
+ with gr.Blocks() as app:
63
+ with gr.Row():
64
+ gr.HTML("<h1>๊ธ€๋กœ๋ฒŒ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™” ๋„๊ตฌ</h1>")
65
+
66
+ with gr.Row():
67
+ start_date = gr.Textbox("2013-01-01", label="์‹œ์ž‘ ์ผ์ž")
68
+ end_date = gr.Textbox(datetime.now().date(), label="์ข…๋ฃŒ ์ผ์ž")
69
+
70
+ with gr.Row():
71
+ tickers_string = gr.Textbox("TSLA,META,AMZN,MSFT,BTC-USD", label="ํฌํŠธํด๋ฆฌ์˜ค์— ํฌํ•จ๋  ์ฃผ์‹ ํ‹ฐ์ปค๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•˜์„ธ์š” (์˜ˆ: 'TSLA,META,AMZN,MSFT,BTC-USD')")
72
+ btn = gr.Button("ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™” ๊ฒฐ๊ณผ ๋ณด๊ธฐ")
73
+
74
+ with gr.Row():
75
+ expected_annual_return = gr.Text(label="์˜ˆ์ƒ ์—ฐ๊ฐ„ ์ˆ˜์ต๋ฅ ")
76
+ annual_volatility = gr.Text(label="์—ฐ๊ฐ„ ๋ณ€๋™์„ฑ")
77
+ sharpe_ratio = gr.Text(label="์ƒคํ”„ ๋น„์œจ")
78
+
79
+ with gr.Row():
80
+ fig_cum_returns = gr.Plot(label="์ตœ์ ํ™”๋œ ํฌํŠธํด๋ฆฌ์˜ค์˜ ๋ˆ„์  ์ˆ˜์ต๋ฅ  (์‹œ์ž‘ ๊ฐ€๊ฒฉ $100)")
81
+ fig_efficient_frontier = gr.Plot(label="ํšจ์œจ์  ํˆฌ์ž์„ ")
82
+ fig_corr = gr.Plot(label="์ฃผ์‹ ๊ฐ„ ์ƒ๊ด€ ๊ด€๊ณ„")
83
+ fig_indiv_prices = gr.Plot(label="๊ฐœ๋ณ„ ์ฃผ์‹ ๊ฐ€๊ฒฉ")
84
+
85
+ btn.click(fn=output_results, inputs=[start_date, end_date, tickers_string],
86
+ outputs=[fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices])
87
+
88
+ app.launch()