aiqtech commited on
Commit
f0ad4e9
โ€ข
1 Parent(s): 4244035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -100
app.py CHANGED
@@ -1,100 +1,2 @@
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, 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'))