Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,4 +84,44 @@ end_date = st.date_input("Pilih tanggal akhir", pd.to_datetime("2023-12-31"))
|
|
| 84 |
|
| 85 |
if st.button("Analisis Portofolio"):
|
| 86 |
if validate_tickers(tickers_list):
|
| 87 |
-
stock_data =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if st.button("Analisis Portofolio"):
|
| 86 |
if validate_tickers(tickers_list):
|
| 87 |
+
stock_data = get_stock_data(tickers_list, start_date, end_date)
|
| 88 |
+
if stock_data is not None:
|
| 89 |
+
mean_returns, cov_matrix = calculate_returns(stock_data)
|
| 90 |
+
optimal_weights = optimize_portfolio(mean_returns, cov_matrix)
|
| 91 |
+
|
| 92 |
+
st.subheader("Statistik Saham")
|
| 93 |
+
st.write(stock_data.describe())
|
| 94 |
+
|
| 95 |
+
if optimal_weights is not None:
|
| 96 |
+
st.subheader("Bobot Portofolio Optimal")
|
| 97 |
+
portfolio_weights = {stock: weight for stock, weight in zip(stock_data.columns, optimal_weights)}
|
| 98 |
+
st.write(portfolio_weights)
|
| 99 |
+
|
| 100 |
+
# Pie Chart dengan filter saham dengan bobot signifikan
|
| 101 |
+
filtered_weights = {k: v for k, v in portfolio_weights.items() if v > 0.01}
|
| 102 |
+
fig, ax = plt.subplots()
|
| 103 |
+
ax.pie(filtered_weights.values(), labels=filtered_weights.keys(), autopct='%1.1f%%', startangle=140)
|
| 104 |
+
ax.axis('equal')
|
| 105 |
+
st.pyplot(fig)
|
| 106 |
+
|
| 107 |
+
# Efficient Frontier
|
| 108 |
+
results = generate_efficient_frontier(mean_returns, cov_matrix)
|
| 109 |
+
|
| 110 |
+
st.subheader("Efficient Frontier")
|
| 111 |
+
fig, ax = plt.subplots()
|
| 112 |
+
scatter = ax.scatter(results[1, :], results[0, :], c=results[2, :], cmap="viridis", marker='o')
|
| 113 |
+
ax.set_xlabel("Risiko (Standar Deviasi)")
|
| 114 |
+
ax.set_ylabel("Return Tahunan")
|
| 115 |
+
ax.set_title("Efficient Frontier")
|
| 116 |
+
fig.colorbar(scatter, label="Sharpe Ratio")
|
| 117 |
+
st.pyplot(fig)
|
| 118 |
+
|
| 119 |
+
st.markdown("""
|
| 120 |
+
**Interpretasi:**
|
| 121 |
+
- Bobot dalam portofolio menunjukkan proporsi investasi pada masing-masing saham.
|
| 122 |
+
- Semakin besar bobot, semakin besar porsi dana yang dialokasikan ke saham tersebut.
|
| 123 |
+
- Grafik Efficient Frontier menunjukkan hubungan antara risiko dan return dari berbagai kombinasi portofolio.
|
| 124 |
+
- Portofolio yang berada di frontier efisien memberikan return terbaik untuk tingkat risiko tertentu.
|
| 125 |
+
""")
|
| 126 |
+
else:
|
| 127 |
+
st.error("Optimasi portofolio gagal. Coba dengan saham yang berbeda.")
|