Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import matplotlib.pyplot as plt
|
|
| 6 |
import scipy.optimize as sco
|
| 7 |
|
| 8 |
def get_stock_data(tickers, start, end):
|
| 9 |
-
"""Mengambil data harga saham dari Yahoo Finance."""
|
| 10 |
data = yf.download(tickers, start=start, end=end)
|
| 11 |
|
| 12 |
if data.empty:
|
|
@@ -23,12 +22,10 @@ def get_stock_data(tickers, start, end):
|
|
| 23 |
return None
|
| 24 |
|
| 25 |
def calculate_returns(data):
|
| 26 |
-
"""Menghitung return logaritmik dan matriks kovarians saham."""
|
| 27 |
log_returns = np.log(data / data.shift(1))
|
| 28 |
return log_returns.mean() * 252, log_returns.cov() * 252
|
| 29 |
|
| 30 |
def optimize_portfolio(returns, cov_matrix):
|
| 31 |
-
"""Mengoptimalkan portofolio dengan memaksimalkan rasio Sharpe."""
|
| 32 |
num_assets = len(returns)
|
| 33 |
|
| 34 |
def sharpe_ratio(weights):
|
|
@@ -44,7 +41,6 @@ def optimize_portfolio(returns, cov_matrix):
|
|
| 44 |
return result.x if result.success else None
|
| 45 |
|
| 46 |
def generate_efficient_frontier(returns, cov_matrix, num_portfolios=5000):
|
| 47 |
-
"""Membuat simulasi Efficient Frontier untuk berbagai kombinasi portofolio."""
|
| 48 |
num_assets = len(returns)
|
| 49 |
results = np.zeros((3, num_portfolios))
|
| 50 |
|
|
@@ -62,9 +58,11 @@ def generate_efficient_frontier(returns, cov_matrix, num_portfolios=5000):
|
|
| 62 |
|
| 63 |
st.title("Analisis Portofolio Saham Optimal (Model Markowitz)")
|
| 64 |
|
| 65 |
-
st.
|
| 66 |
-
|
| 67 |
-
Model Markowitz digunakan untuk
|
|
|
|
|
|
|
| 68 |
""")
|
| 69 |
|
| 70 |
def get_recommended_stocks():
|
|
@@ -77,7 +75,7 @@ def validate_tickers(tickers):
|
|
| 77 |
return False
|
| 78 |
return True
|
| 79 |
|
| 80 |
-
st.
|
| 81 |
st.write(get_recommended_stocks())
|
| 82 |
|
| 83 |
tickers_list = st.text_input("Masukkan ticker saham", "KLBF.JK, SIDO.JK, KAEF.JK").split(", ")
|
|
@@ -86,45 +84,4 @@ end_date = st.date_input("Pilih tanggal akhir", pd.to_datetime("2023-12-31"))
|
|
| 86 |
|
| 87 |
if st.button("Analisis Portofolio"):
|
| 88 |
if validate_tickers(tickers_list):
|
| 89 |
-
stock_data =
|
| 90 |
-
if stock_data is not None:
|
| 91 |
-
mean_returns, cov_matrix = calculate_returns(stock_data)
|
| 92 |
-
optimal_weights = optimize_portfolio(mean_returns, cov_matrix)
|
| 93 |
-
|
| 94 |
-
st.subheader("Statistik Saham")
|
| 95 |
-
st.write(stock_data.describe())
|
| 96 |
-
|
| 97 |
-
if optimal_weights is not None:
|
| 98 |
-
st.subheader("Bobot Portofolio Optimal")
|
| 99 |
-
portfolio_weights = {stock: weight for stock, weight in zip(stock_data.columns, optimal_weights)}
|
| 100 |
-
st.write(portfolio_weights)
|
| 101 |
-
|
| 102 |
-
fig, ax = plt.subplots()
|
| 103 |
-
ax.pie(optimal_weights, labels=stock_data.columns, autopct='%1.1f%%', startangle=140)
|
| 104 |
-
ax.axis('equal')
|
| 105 |
-
st.pyplot(fig)
|
| 106 |
-
|
| 107 |
-
st.write("""
|
| 108 |
-
**Interpretasi:**
|
| 109 |
-
- Bobot dalam portofolio menunjukkan proporsi investasi pada masing-masing saham.
|
| 110 |
-
- Semakin besar bobot, semakin besar porsi dana yang dialokasikan ke saham tersebut.
|
| 111 |
-
""")
|
| 112 |
-
|
| 113 |
-
results = generate_efficient_frontier(mean_returns, cov_matrix)
|
| 114 |
-
|
| 115 |
-
st.subheader("Efficient Frontier")
|
| 116 |
-
fig, ax = plt.subplots()
|
| 117 |
-
scatter = ax.scatter(results[1, :], results[0, :], c=results[2, :], cmap="viridis", marker='o')
|
| 118 |
-
ax.set_xlabel("Risiko (Standar Deviasi)")
|
| 119 |
-
ax.set_ylabel("Return Tahunan")
|
| 120 |
-
ax.set_title("Efficient Frontier")
|
| 121 |
-
fig.colorbar(scatter, label="Sharpe Ratio")
|
| 122 |
-
st.pyplot(fig)
|
| 123 |
-
|
| 124 |
-
st.write("""
|
| 125 |
-
**Penjelasan Efficient Frontier:**
|
| 126 |
-
- Grafik ini menunjukkan hubungan antara risiko dan return dari berbagai kombinasi portofolio.
|
| 127 |
-
- Portofolio yang berada di frontier efisien memberikan return terbaik untuk tingkat risiko tertentu.
|
| 128 |
-
""")
|
| 129 |
-
else:
|
| 130 |
-
st.error("Optimasi portofolio gagal. Coba dengan saham yang berbeda.")
|
|
|
|
| 6 |
import scipy.optimize as sco
|
| 7 |
|
| 8 |
def get_stock_data(tickers, start, end):
|
|
|
|
| 9 |
data = yf.download(tickers, start=start, end=end)
|
| 10 |
|
| 11 |
if data.empty:
|
|
|
|
| 22 |
return None
|
| 23 |
|
| 24 |
def calculate_returns(data):
|
|
|
|
| 25 |
log_returns = np.log(data / data.shift(1))
|
| 26 |
return log_returns.mean() * 252, log_returns.cov() * 252
|
| 27 |
|
| 28 |
def optimize_portfolio(returns, cov_matrix):
|
|
|
|
| 29 |
num_assets = len(returns)
|
| 30 |
|
| 31 |
def sharpe_ratio(weights):
|
|
|
|
| 41 |
return result.x if result.success else None
|
| 42 |
|
| 43 |
def generate_efficient_frontier(returns, cov_matrix, num_portfolios=5000):
|
|
|
|
| 44 |
num_assets = len(returns)
|
| 45 |
results = np.zeros((3, num_portfolios))
|
| 46 |
|
|
|
|
| 58 |
|
| 59 |
st.title("Analisis Portofolio Saham Optimal (Model Markowitz)")
|
| 60 |
|
| 61 |
+
st.markdown("""
|
| 62 |
+
### Teori Markowitz
|
| 63 |
+
Model Markowitz, atau Modern Portfolio Theory (MPT), digunakan untuk membangun portofolio investasi optimal dengan memaksimalkan return untuk tingkat risiko tertentu.
|
| 64 |
+
|
| 65 |
+
Portofolio yang optimal ditemukan dengan menghitung kombinasi terbaik dari aset yang tersedia untuk meminimalkan risiko dan memaksimalkan return.
|
| 66 |
""")
|
| 67 |
|
| 68 |
def get_recommended_stocks():
|
|
|
|
| 75 |
return False
|
| 76 |
return True
|
| 77 |
|
| 78 |
+
st.write("Rekomendasi Saham yang Bertahan Saat COVID-19:")
|
| 79 |
st.write(get_recommended_stocks())
|
| 80 |
|
| 81 |
tickers_list = st.text_input("Masukkan ticker saham", "KLBF.JK, SIDO.JK, KAEF.JK").split(", ")
|
|
|
|
| 84 |
|
| 85 |
if st.button("Analisis Portofolio"):
|
| 86 |
if validate_tickers(tickers_list):
|
| 87 |
+
stock_data = get_stock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|