Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,24 +15,6 @@ def download_data(tickers, start_date, end_date):
|
|
15 |
attempts += 1
|
16 |
raise Exception("Failed to download data after multiple attempts")
|
17 |
|
18 |
-
def output_results(start_date, end_date, tickers_string):
|
19 |
-
tickers = [ticker.strip() for ticker in tickers_string.split(',')]
|
20 |
-
stocks_df = download_data(tickers, start_date, end_date)['Adj Close']
|
21 |
-
|
22 |
-
mu = expected_returns.mean_historical_return(stocks_df)
|
23 |
-
S = risk_models.sample_cov(stocks_df)
|
24 |
-
|
25 |
-
# Ensure the covariance matrix is symmetric
|
26 |
-
if (S.T != S).any():
|
27 |
-
raise ValueError("Covariance matrix is not symmetric")
|
28 |
-
|
29 |
-
ef = EfficientFrontier(mu, S)
|
30 |
-
weights = ef.max_sharpe()
|
31 |
-
cleaned_weights = ef.clean_weights()
|
32 |
-
performance = ef.portfolio_performance(verbose=True)
|
33 |
-
|
34 |
-
return weights, performance
|
35 |
-
|
36 |
def plot_efficient_frontier_custom(mu, S):
|
37 |
ef = EfficientFrontier(mu, S)
|
38 |
fig = go.Figure()
|
@@ -47,6 +29,27 @@ def plot_efficient_frontier_custom(mu, S):
|
|
47 |
fig.update_layout(title='Efficient Frontier', xaxis_title='Volatility (Std. Deviation)', yaxis_title='Expected Returns', legend_title='Portfolio')
|
48 |
return fig
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Gradio ์ธํฐํ์ด์ค ์ด๊ธฐํ ๋ฐ ์คํ
|
51 |
# (์ฌ๊ธฐ์ Gradio ์ธํฐํ์ด์ค ์ค์ ์ฝ๋ ์ถ๊ฐ)
|
52 |
|
|
|
15 |
attempts += 1
|
16 |
raise Exception("Failed to download data after multiple attempts")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def plot_efficient_frontier_custom(mu, S):
|
19 |
ef = EfficientFrontier(mu, S)
|
20 |
fig = go.Figure()
|
|
|
29 |
fig.update_layout(title='Efficient Frontier', xaxis_title='Volatility (Std. Deviation)', yaxis_title='Expected Returns', legend_title='Portfolio')
|
30 |
return fig
|
31 |
|
32 |
+
|
33 |
+
|
34 |
+
def output_results(start_date, end_date, tickers_string):
|
35 |
+
tickers = [ticker.strip() for ticker in tickers_string.split(',')]
|
36 |
+
stocks_df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
37 |
+
|
38 |
+
mu = expected_returns.mean_historical_return(stocks_df)
|
39 |
+
S = risk_models.sample_cov(stocks_df)
|
40 |
+
|
41 |
+
# ๊ณต๋ถ์ฐ ํ๋ ฌ์ด ๋์นญ์ธ์ง ํ์ธ
|
42 |
+
if not (S.T == S).all().all(): # ๋ชจ๋ ๊ฐ์ด ๋์นญ์ธ์ง ํ์ธ
|
43 |
+
raise ValueError("๊ณต๋ถ์ฐ ํ๋ ฌ์ด ๋์นญ์ด ์๋๋๋ค.")
|
44 |
+
|
45 |
+
ef = EfficientFrontier(mu, S)
|
46 |
+
weights = ef.max_sharpe()
|
47 |
+
cleaned_weights = ef.clean_weights()
|
48 |
+
fig_weights = plot_weights(cleaned_weights)
|
49 |
+
expected_annual_return, annual_volatility, sharpe_ratio = ef.portfolio_performance()
|
50 |
+
|
51 |
+
return fig_weights, expected_annual_return, annual_volatility, sharpe_ratio
|
52 |
+
|
53 |
# Gradio ์ธํฐํ์ด์ค ์ด๊ธฐํ ๋ฐ ์คํ
|
54 |
# (์ฌ๊ธฐ์ Gradio ์ธํฐํ์ด์ค ์ค์ ์ฝ๋ ์ถ๊ฐ)
|
55 |
|