Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
import yfinance as yf
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
from scipy.optimize import minimize
|
| 7 |
|
| 8 |
# Fungsi untuk mengunduh data saham
|
| 9 |
def get_stock_data(tickers, start, end):
|
| 10 |
data = yf.download(tickers, start=start, end=end)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
return data['Adj Close']
|
|
|
|
|
|
|
| 15 |
else:
|
| 16 |
-
st.error("Kolom 'Adj Close' tidak ditemukan.
|
| 17 |
return None
|
| 18 |
|
| 19 |
-
# Fungsi untuk menghitung return harian
|
| 20 |
-
def calculate_daily_returns(data):
|
| 21 |
-
return data.pct_change().dropna()
|
| 22 |
-
|
| 23 |
-
# Fungsi untuk menghitung portofolio optimal dengan Model Markowitz
|
| 24 |
-
def optimize_portfolio(returns):
|
| 25 |
-
num_assets = len(returns.columns)
|
| 26 |
-
weights = np.random.random(num_assets)
|
| 27 |
-
weights /= np.sum(weights)
|
| 28 |
-
|
| 29 |
-
def portfolio_performance(weights):
|
| 30 |
-
port_return = np.sum(returns.mean() * weights) * 252
|
| 31 |
-
port_volatility = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
|
| 32 |
-
return port_volatility
|
| 33 |
-
|
| 34 |
-
constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
|
| 35 |
-
bounds = tuple((0, 1) for _ in range(num_assets))
|
| 36 |
-
result = minimize(portfolio_performance, weights, method='SLSQP', bounds=bounds, constraints=constraints)
|
| 37 |
-
|
| 38 |
-
return result.x
|
| 39 |
-
|
| 40 |
# Streamlit UI
|
| 41 |
-
st.title("Analisis Portofolio Saham
|
| 42 |
-
|
| 43 |
-
# Input pengguna untuk daftar saham
|
| 44 |
-
tickers = st.text_input("Masukkan kode saham (pisahkan dengan koma)", "BBCA.JK,TLKM.JK,UNVR.JK")
|
| 45 |
start_date = st.date_input("Pilih tanggal mulai", pd.to_datetime("2020-01-01"))
|
| 46 |
end_date = st.date_input("Pilih tanggal akhir", pd.to_datetime("2020-12-31"))
|
| 47 |
|
| 48 |
-
if st.button("
|
| 49 |
-
tickers_list = [t.strip() for t in tickers.split(',')]
|
| 50 |
data = get_stock_data(tickers_list, start_date, end_date)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.write(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
returns = calculate_daily_returns(data)
|
| 57 |
-
optimal_weights = optimize_portfolio(returns)
|
| 58 |
-
|
| 59 |
-
st.write("Bobot Optimal Portofolio:")
|
| 60 |
-
for ticker, weight in zip(tickers_list, optimal_weights):
|
| 61 |
-
st.write(f"{ticker}: {weight:.2%}")
|
| 62 |
-
|
| 63 |
-
# Visualisasi Portofolio
|
| 64 |
-
fig, ax = plt.subplots()
|
| 65 |
-
ax.pie(optimal_weights, labels=tickers_list, autopct='%1.1f%%', startangle=90)
|
| 66 |
-
ax.axis("equal")
|
| 67 |
-
st.pyplot(fig)
|
| 68 |
-
else:
|
| 69 |
-
st.error("Gagal mengambil data saham. Pastikan kode saham benar.")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
|
| 6 |
# Fungsi untuk mengunduh data saham
|
| 7 |
def get_stock_data(tickers, start, end):
|
| 8 |
data = yf.download(tickers, start=start, end=end)
|
| 9 |
+
if data.empty:
|
| 10 |
+
st.error("Data saham tidak ditemukan. Periksa ticker atau tanggal.")
|
| 11 |
+
return None
|
| 12 |
+
elif 'Adj Close' in data.columns:
|
| 13 |
return data['Adj Close']
|
| 14 |
+
elif 'Close' in data.columns:
|
| 15 |
+
return data['Close']
|
| 16 |
else:
|
| 17 |
+
st.error("Kolom 'Adj Close' atau 'Close' tidak ditemukan.")
|
| 18 |
return None
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Streamlit UI
|
| 21 |
+
st.title("Analisis Portofolio Saham")
|
| 22 |
+
tickers_list = st.text_input("Masukkan ticker saham (contoh: BBCA.JK, TLKM.JK)", "BBCA.JK, TLKM.JK").split(",")
|
|
|
|
|
|
|
| 23 |
start_date = st.date_input("Pilih tanggal mulai", pd.to_datetime("2020-01-01"))
|
| 24 |
end_date = st.date_input("Pilih tanggal akhir", pd.to_datetime("2020-12-31"))
|
| 25 |
|
| 26 |
+
if st.button("Dapatkan Data"):
|
|
|
|
| 27 |
data = get_stock_data(tickers_list, start_date, end_date)
|
| 28 |
+
if data is not None:
|
| 29 |
+
st.write("Data Saham:")
|
| 30 |
+
st.write(data.tail())
|
| 31 |
+
# Plot harga saham
|
| 32 |
+
st.line_chart(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|