File size: 3,777 Bytes
23c88d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import gradio as gr
import yfinance as yf
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
from pypfopt import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
from pypfopt import plotting
import copy
import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
from datetime import datetime
def plot_cum_returns(data, title):
# ์ผ์ผ ๋์ ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์๊ฐํ
daily_cum_returns = 1 + data.dropna().pct_change()
daily_cum_returns = daily_cum_returns.cumprod() * 100
fig = px.line(daily_cum_returns, title=title)
return fig
def plot_efficient_frontier_and_max_sharpe(mu, S):
# ์ต๋ ์คํ ๋น์จ๋ก ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๋ฐ ํจ์จ์ ํฌ์์ ๊ทธ๋ฆฌ๊ธฐ
ef = EfficientFrontier(mu, S)
fig, ax = plt.subplots(figsize=(6, 4))
ef_max_sharpe = copy.deepcopy(ef)
plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)
# ์ต๋ ์คํ ๋น์จ ํฌํธํด๋ฆฌ์ค ์ฐพ๊ธฐ
ef_max_sharpe.max_sharpe(risk_free_rate=0.02)
ret_tangent, std_tangent, _ = ef_max_sharpe.portfolio_performance()
ax.scatter(std_tangent, ret_tangent, marker="*", s=100, c="r", label="์ต๋ ์คํ")
ax.legend()
return fig
def output_results(start_date, end_date, tickers_string):
# ์
๋ ฅ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํ์ผ๋ก ์ต์ ํ ๊ฒฐ๊ณผ ์ถ๋ ฅ
tickers = tickers_string.split(',')
# ์ฃผ์ ๊ฐ๊ฒฉ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
# ๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ ์๊ฐํ
fig_indiv_prices = px.line(stocks_df, title='๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ')
# ๊ฐ๋ณ ์ฃผ์ ๋์ ์์ต๋ฅ ์๊ฐํ
fig_cum_returns = plot_cum_returns(stocks_df, '๊ฐ๋ณ ์ฃผ์์ ๋์ ์์ต๋ฅ ($100 ์์)')
# ์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ ๊ณ์ฐ ๋ฐ ์๊ฐํ
corr_df = stocks_df.corr().round(2)
fig_corr = px.imshow(corr_df, text_auto=True, title='์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ')
# ํฌํธํด๋ฆฌ์ค ์ต์ ํ๋ฅผ ์ํ ๊ธฐ๋ ์์ต๋ฅ ๊ณผ ์ํ ๊ณต๋ถ์ฐ ํ๋ ฌ ๊ณ์ฐ
mu = expected_returns.mean_historical_return(stocks_df)
S = risk_models.sample_cov(stocks_df)
# ํจ์จ์ ํฌ์์ ์๊ฐํ
fig_efficient_frontier = plot_efficient_frontier_and_max_sharpe(mu, S)
return fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
with gr.Blocks() as app:
with gr.Row():
gr.HTML("<h1>๊ธ๋ก๋ฒ ์ฃผ์ ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๋๊ตฌ</h1>")
with gr.Row():
start_date = gr.Textbox("2013-01-01", label="์์ ์ผ์")
end_date = gr.Textbox(datetime.now().date(), label="์ข
๋ฃ ์ผ์")
with gr.Row():
tickers_string = gr.Textbox("TSLA,META,AMZN,MSFT,BTC-USD", label="ํฌํธํด๋ฆฌ์ค์ ํฌํจ๋ ์ฃผ์ ํฐ์ปค๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅํ์ธ์ (์: 'TSLA,META,AMZN,MSFT,BTC-USD')")
btn = gr.Button("ํฌํธํด๋ฆฌ์ค ์ต์ ํ ๊ฒฐ๊ณผ ๋ณด๊ธฐ")
with gr.Row():
expected_annual_return = gr.Text(label="์์ ์ฐ๊ฐ ์์ต๋ฅ ")
annual_volatility = gr.Text(label="์ฐ๊ฐ ๋ณ๋์ฑ")
sharpe_ratio = gr.Text(label="์คํ ๋น์จ")
with gr.Row():
fig_cum_returns = gr.Plot(label="์ต์ ํ๋ ํฌํธํด๋ฆฌ์ค์ ๋์ ์์ต๋ฅ (์์ ๊ฐ๊ฒฉ $100)")
fig_efficient_frontier = gr.Plot(label="ํจ์จ์ ํฌ์์ ")
fig_corr = gr.Plot(label="์ฃผ์ ๊ฐ ์๊ด ๊ด๊ณ")
fig_indiv_prices = gr.Plot(label="๊ฐ๋ณ ์ฃผ์ ๊ฐ๊ฒฉ")
btn.click(fn=output_results, inputs=[start_date, end_date, tickers_string],
outputs=[fig_cum_returns, fig_efficient_frontier, fig_corr, fig_indiv_prices])
app.launch()
|