Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,14 +53,30 @@ def analyze_stock(symbol, history_period, prediction_days=30):
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error analyzing {symbol}: {e}")
|
| 55 |
empty_fig = gr.Plot.update(value=None)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
empty_predictions = {
|
| 58 |
"high_30d": 0,
|
| 59 |
"low_30d": 0,
|
| 60 |
"change_pct": 0,
|
| 61 |
-
"summary": "Prediction unavailable.",
|
| 62 |
"q01": [],
|
| 63 |
-
"q09": []
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
return {}, {}, {}, empty_fig, empty_fig, empty_fig, empty_predictions
|
| 66 |
|
|
@@ -74,11 +90,16 @@ def update_analysis(symbol, history_period, prediction_days):
|
|
| 74 |
fig_technical,
|
| 75 |
fig_prediction,
|
| 76 |
predictions,
|
| 77 |
-
) = analyze_stock(symbol, history_period, prediction_days)
|
| 78 |
|
| 79 |
if not fundamental_info:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return (
|
| 81 |
-
"
|
| 82 |
gr.Plot.update(value=None),
|
| 83 |
gr.Plot.update(value=None),
|
| 84 |
gr.Plot.update(value=None),
|
|
@@ -112,8 +133,8 @@ def update_analysis(symbol, history_period, prediction_days):
|
|
| 112 |
"""
|
| 113 |
|
| 114 |
# Hitung Min/Max dari seluruh rentang kepercayaan (Q0.1 dan Q0.9)
|
| 115 |
-
band_min = float(min(predictions.get('q01', [0]))) if predictions.get('q01') else 0
|
| 116 |
-
band_max = float(max(predictions.get('q09', [0]))) if predictions.get('q09') else 0
|
| 117 |
|
| 118 |
prediction = f"""
|
| 119 |
<h4>30-DAY AI FORECAST (CHRONOS-2)</h4>
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error analyzing {symbol}: {e}")
|
| 55 |
empty_fig = gr.Plot.update(value=None)
|
| 56 |
+
|
| 57 |
+
# Ambil harga terakhir untuk menghitung TP/SL yang tetap valid
|
| 58 |
+
try:
|
| 59 |
+
stock = yf.Ticker(symbol)
|
| 60 |
+
data = stock.history(period="1d", interval="1d")
|
| 61 |
+
last_price = data['Close'].iloc[-1] if not data.empty else 0
|
| 62 |
+
except:
|
| 63 |
+
last_price = 0
|
| 64 |
+
|
| 65 |
+
# Jika harga terakhir berhasil diambil, hitung TP/SL default
|
| 66 |
+
tp1_default = last_price * 1.005 # 0.5% dari last price
|
| 67 |
+
tp2_default = last_price * 1.01 # 1% dari last price
|
| 68 |
+
sl_default = last_price * 0.95
|
| 69 |
+
|
| 70 |
empty_predictions = {
|
| 71 |
"high_30d": 0,
|
| 72 |
"low_30d": 0,
|
| 73 |
"change_pct": 0,
|
| 74 |
+
"summary": f"Prediction unavailable. Model error: {e}", # Tampilkan error yang ditangkap
|
| 75 |
"q01": [],
|
| 76 |
+
"q09": [],
|
| 77 |
+
"tp1": tp1_default,
|
| 78 |
+
"tp2": tp2_default,
|
| 79 |
+
"sl": sl_default,
|
| 80 |
}
|
| 81 |
return {}, {}, {}, empty_fig, empty_fig, empty_fig, empty_predictions
|
| 82 |
|
|
|
|
| 90 |
fig_technical,
|
| 91 |
fig_prediction,
|
| 92 |
predictions,
|
| 93 |
+
) = analyze_stock(symbol, history_period, prediction_days)
|
| 94 |
|
| 95 |
if not fundamental_info:
|
| 96 |
+
# Jika fundamental gagal diambil (misalnya simbol salah), berikan pesan error umum
|
| 97 |
+
error_msg = f"Unable to fetch stock data for {symbol.upper()}. Please check the symbol."
|
| 98 |
+
# Gunakan prediksi yang dikembalikan (meski kosong) untuk mendapatkan TP/SL default
|
| 99 |
+
tp_sl_info = f"<b>TP1:</b> Rp{predictions.get('tp1', 0):,.2f}<br><b>TP2:</b> Rp{predictions.get('tp2', 0):,.2f}<br><b>Stop Loss:</b> Rp{predictions.get('sl', 0):,.2f}<br><br><b>Model Insight:</b><br>Data fetching failed. Cannot proceed with analysis."
|
| 100 |
+
|
| 101 |
return (
|
| 102 |
+
f"""<div style="color: red; padding: 10px; border: 1px solid red;">{error_msg}</div><br>{tp_sl_info}""",
|
| 103 |
gr.Plot.update(value=None),
|
| 104 |
gr.Plot.update(value=None),
|
| 105 |
gr.Plot.update(value=None),
|
|
|
|
| 133 |
"""
|
| 134 |
|
| 135 |
# Hitung Min/Max dari seluruh rentang kepercayaan (Q0.1 dan Q0.9)
|
| 136 |
+
band_min = float(min(predictions.get('q01', [0]))) if predictions.get('q01') and predictions.get('q01').size > 0 else 0
|
| 137 |
+
band_max = float(max(predictions.get('q09', [0]))) if predictions.get('q09') and predictions.get('q09').size > 0 else 0
|
| 138 |
|
| 139 |
prediction = f"""
|
| 140 |
<h4>30-DAY AI FORECAST (CHRONOS-2)</h4>
|