Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,8 @@ import warnings
|
|
| 15 |
warnings.filterwarnings("ignore")
|
| 16 |
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
try:
|
| 20 |
if not symbol.strip():
|
| 21 |
raise ValueError("Please enter a valid stock symbol.")
|
|
@@ -24,10 +25,11 @@ def analyze_stock(symbol, prediction_days=30):
|
|
| 24 |
symbol = symbol.upper() + ".JK"
|
| 25 |
|
| 26 |
stock = yf.Ticker(symbol)
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
if data.empty:
|
| 30 |
-
raise ValueError("No price data available for
|
| 31 |
|
| 32 |
indicators = calculate_technical_indicators(data)
|
| 33 |
signals = generate_trading_signals(data, indicators)
|
|
@@ -38,7 +40,7 @@ def analyze_stock(symbol, prediction_days=30):
|
|
| 38 |
fig_technical = create_technical_chart(data, indicators)
|
| 39 |
fig_prediction = create_prediction_chart(data, predictions)
|
| 40 |
|
| 41 |
-
# kalkulasi TP1, TP2, SL
|
| 42 |
last_price = data['Close'].iloc[-1]
|
| 43 |
tp1 = last_price * (1 + (predictions.get("change_pct", 0) / 200))
|
| 44 |
tp2 = last_price * (1 + (predictions.get("change_pct", 0) / 100))
|
|
@@ -53,16 +55,20 @@ def analyze_stock(symbol, prediction_days=30):
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error analyzing {symbol}: {e}")
|
| 55 |
empty_fig = gr.Plot.update(value=None)
|
|
|
|
| 56 |
empty_predictions = {
|
| 57 |
"high_30d": 0,
|
| 58 |
"low_30d": 0,
|
| 59 |
"change_pct": 0,
|
| 60 |
"summary": "Prediction unavailable.",
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
return {}, {}, {}, empty_fig, empty_fig, empty_fig, empty_predictions
|
| 63 |
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
(
|
| 67 |
fundamental_info,
|
| 68 |
indicators,
|
|
@@ -71,7 +77,7 @@ def update_analysis(symbol, prediction_days):
|
|
| 71 |
fig_technical,
|
| 72 |
fig_prediction,
|
| 73 |
predictions,
|
| 74 |
-
) = analyze_stock(symbol, prediction_days)
|
| 75 |
|
| 76 |
if not fundamental_info:
|
| 77 |
return (
|
|
@@ -107,12 +113,21 @@ def update_analysis(symbol, prediction_days):
|
|
| 107 |
{details_list}
|
| 108 |
</ul>
|
| 109 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
prediction = f"""
|
| 112 |
<h4>30-DAY AI FORECAST (CHRONOS-2)</h4>
|
| 113 |
-
<b>Predicted High:</b> Rp{predictions.get('high_30d', 0):,.2f}<br>
|
| 114 |
-
<b>Predicted Low:</b> Rp{predictions.get('low_30d', 0):,.2f}<br>
|
| 115 |
-
<b>Expected Change:</b> {predictions.get('change_pct', 0):.2f}%<br
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
<b>TP1:</b> Rp{predictions.get('tp1', 0):,.2f}<br>
|
| 117 |
<b>TP2:</b> Rp{predictions.get('tp2', 0):,.2f}<br>
|
| 118 |
<b>Stop Loss:</b> Rp{predictions.get('sl', 0):,.2f}<br><br>
|
|
@@ -148,6 +163,13 @@ with gr.Blocks(
|
|
| 148 |
placeholder="Example: BBCA, TLKM, ADRO, BMRI",
|
| 149 |
interactive=True,
|
| 150 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
prediction_days = gr.Slider(
|
| 152 |
label="FORECAST PERIOD (DAYS)",
|
| 153 |
minimum=5,
|
|
@@ -171,7 +193,7 @@ with gr.Blocks(
|
|
| 171 |
|
| 172 |
analyze_button.click(
|
| 173 |
fn=update_analysis,
|
| 174 |
-
inputs=[symbol, prediction_days],
|
| 175 |
outputs=[report_section, price_chart, technical_chart, prediction_chart],
|
| 176 |
)
|
| 177 |
|
|
|
|
| 15 |
warnings.filterwarnings("ignore")
|
| 16 |
|
| 17 |
|
| 18 |
+
# FUNGSI DIUBAH: Menerima history_period
|
| 19 |
+
def analyze_stock(symbol, history_period, prediction_days=30):
|
| 20 |
try:
|
| 21 |
if not symbol.strip():
|
| 22 |
raise ValueError("Please enter a valid stock symbol.")
|
|
|
|
| 25 |
symbol = symbol.upper() + ".JK"
|
| 26 |
|
| 27 |
stock = yf.Ticker(symbol)
|
| 28 |
+
# MENGGUNAKAN history_period yang dipilih user
|
| 29 |
+
data = stock.history(period=history_period, interval="1d")
|
| 30 |
|
| 31 |
if data.empty:
|
| 32 |
+
raise ValueError(f"No price data available for {history_period}.")
|
| 33 |
|
| 34 |
indicators = calculate_technical_indicators(data)
|
| 35 |
signals = generate_trading_signals(data, indicators)
|
|
|
|
| 40 |
fig_technical = create_technical_chart(data, indicators)
|
| 41 |
fig_prediction = create_prediction_chart(data, predictions)
|
| 42 |
|
| 43 |
+
# kalkulasi TP1, TP2, SL menggunakan median (Q0.5)
|
| 44 |
last_price = data['Close'].iloc[-1]
|
| 45 |
tp1 = last_price * (1 + (predictions.get("change_pct", 0) / 200))
|
| 46 |
tp2 = last_price * (1 + (predictions.get("change_pct", 0) / 100))
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error analyzing {symbol}: {e}")
|
| 57 |
empty_fig = gr.Plot.update(value=None)
|
| 58 |
+
# Pastikan error return juga mengandung q01 dan q09
|
| 59 |
empty_predictions = {
|
| 60 |
"high_30d": 0,
|
| 61 |
"low_30d": 0,
|
| 62 |
"change_pct": 0,
|
| 63 |
"summary": "Prediction unavailable.",
|
| 64 |
+
"q01": [],
|
| 65 |
+
"q09": []
|
| 66 |
}
|
| 67 |
return {}, {}, {}, empty_fig, empty_fig, empty_fig, empty_predictions
|
| 68 |
|
| 69 |
|
| 70 |
+
# FUNGSI DIUBAH: Menerima history_period
|
| 71 |
+
def update_analysis(symbol, history_period, prediction_days):
|
| 72 |
(
|
| 73 |
fundamental_info,
|
| 74 |
indicators,
|
|
|
|
| 77 |
fig_technical,
|
| 78 |
fig_prediction,
|
| 79 |
predictions,
|
| 80 |
+
) = analyze_stock(symbol, history_period, prediction_days) # Mengoper history_period
|
| 81 |
|
| 82 |
if not fundamental_info:
|
| 83 |
return (
|
|
|
|
| 113 |
{details_list}
|
| 114 |
</ul>
|
| 115 |
"""
|
| 116 |
+
|
| 117 |
+
# Hitung Min/Max dari seluruh rentang kepercayaan (Q0.1 dan Q0.9)
|
| 118 |
+
band_min = float(min(predictions.get('q01', [0]))) if predictions.get('q01') else 0
|
| 119 |
+
band_max = float(max(predictions.get('q09', [0]))) if predictions.get('q09') else 0
|
| 120 |
|
| 121 |
prediction = f"""
|
| 122 |
<h4>30-DAY AI FORECAST (CHRONOS-2)</h4>
|
| 123 |
+
<b>Predicted Median High:</b> Rp{predictions.get('high_30d', 0):,.2f}<br>
|
| 124 |
+
<b>Predicted Median Low:</b> Rp{predictions.get('low_30d', 0):,.2f}<br>
|
| 125 |
+
<b>Expected Change:</b> {predictions.get('change_pct', 0):.2f}%<br>
|
| 126 |
+
---
|
| 127 |
+
<h4>RISK ANALYSIS (90% CONFIDENCE)</h4>
|
| 128 |
+
<b>Min. Possible Price:</b> Rp{band_min:,.2f}<br>
|
| 129 |
+
<b>Max. Possible Price:</b> Rp{band_max:,.2f}<br>
|
| 130 |
+
---
|
| 131 |
<b>TP1:</b> Rp{predictions.get('tp1', 0):,.2f}<br>
|
| 132 |
<b>TP2:</b> Rp{predictions.get('tp2', 0):,.2f}<br>
|
| 133 |
<b>Stop Loss:</b> Rp{predictions.get('sl', 0):,.2f}<br><br>
|
|
|
|
| 163 |
placeholder="Example: BBCA, TLKM, ADRO, BMRI",
|
| 164 |
interactive=True,
|
| 165 |
)
|
| 166 |
+
# INPUT BARU: Kontrol Periode Data Historis
|
| 167 |
+
history_period_input = gr.Radio(
|
| 168 |
+
label="HISTORICAL DATA PERIOD",
|
| 169 |
+
choices=["6mo", "1y", "3y", "5y"],
|
| 170 |
+
value="3y",
|
| 171 |
+
interactive=True,
|
| 172 |
+
)
|
| 173 |
prediction_days = gr.Slider(
|
| 174 |
label="FORECAST PERIOD (DAYS)",
|
| 175 |
minimum=5,
|
|
|
|
| 193 |
|
| 194 |
analyze_button.click(
|
| 195 |
fn=update_analysis,
|
| 196 |
+
inputs=[symbol, history_period_input, prediction_days], # Input diubah
|
| 197 |
outputs=[report_section, price_chart, technical_chart, prediction_chart],
|
| 198 |
)
|
| 199 |
|