Spaces:
Sleeping
Sleeping
Update trader.py
Browse files
trader.py
CHANGED
|
@@ -474,6 +474,10 @@ def compute_technical_score(indicators: dict) -> float:
|
|
| 474 |
return max(0, min(100, score))
|
| 475 |
|
| 476 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
def compute_composite_score(
|
| 478 |
predicted_prices: list,
|
| 479 |
current_price: float,
|
|
@@ -481,13 +485,17 @@ def compute_composite_score(
|
|
| 481 |
indicators: dict | None = None,
|
| 482 |
sentiment_score: float | None = None,
|
| 483 |
xgb_score: float | None = None,
|
|
|
|
| 484 |
) -> dict:
|
| 485 |
pred_day5 = predicted_prices[-1]
|
| 486 |
pred_change_pct = ((pred_day5 - current_price) / current_price) * 100
|
| 487 |
days_above = sum(1 for p in predicted_prices if p > current_price)
|
| 488 |
|
|
|
|
|
|
|
|
|
|
| 489 |
lstm_score = 50.0
|
| 490 |
-
lstm_score +=
|
| 491 |
lstm_score += (days_above - 2.5) * 6
|
| 492 |
lstm_score += (confidence - 50) * 0.3
|
| 493 |
lstm_score = max(0, min(100, lstm_score))
|
|
@@ -502,8 +510,14 @@ def compute_composite_score(
|
|
| 502 |
else:
|
| 503 |
sent_score = 50.0
|
| 504 |
|
| 505 |
-
#
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
composite = (lstm_score * LSTM_W) + (xgb_score * XGB_W) + (sent_score * SENT_W)
|
| 508 |
composite = round(max(0, min(100, composite)), 1)
|
| 509 |
|
|
@@ -513,7 +527,9 @@ def compute_composite_score(
|
|
| 513 |
"xgb_score": round(xgb_score, 1),
|
| 514 |
"sent_score": round(sent_score, 1),
|
| 515 |
"pred_change_pct": round(pred_change_pct, 2),
|
|
|
|
| 516 |
"days_above": days_above,
|
|
|
|
| 517 |
}
|
| 518 |
|
| 519 |
|
|
@@ -616,6 +632,7 @@ def evaluate_trade_signal(
|
|
| 616 |
scores = compute_composite_score(
|
| 617 |
predicted_prices, current_price, confidence,
|
| 618 |
indicators, sentiment_score,
|
|
|
|
| 619 |
)
|
| 620 |
composite = scores["composite_score"]
|
| 621 |
|
|
|
|
| 474 |
return max(0, min(100, score))
|
| 475 |
|
| 476 |
|
| 477 |
+
# Plausible 5-day move cap: avoid impossible V-spikes / apocalyptic plunges from raw LSTM
|
| 478 |
+
PRED_CHANGE_PCT_CAP = 4.0 # ±4% over 5 days used for scoring; raw pred still reported
|
| 479 |
+
|
| 480 |
+
|
| 481 |
def compute_composite_score(
|
| 482 |
predicted_prices: list,
|
| 483 |
current_price: float,
|
|
|
|
| 485 |
indicators: dict | None = None,
|
| 486 |
sentiment_score: float | None = None,
|
| 487 |
xgb_score: float | None = None,
|
| 488 |
+
regime: str | None = None,
|
| 489 |
) -> dict:
|
| 490 |
pred_day5 = predicted_prices[-1]
|
| 491 |
pred_change_pct = ((pred_day5 - current_price) / current_price) * 100
|
| 492 |
days_above = sum(1 for p in predicted_prices if p > current_price)
|
| 493 |
|
| 494 |
+
# Cap extreme LSTM moves for scoring so one-off model spikes don't dominate
|
| 495 |
+
effective_pred_pct = max(-PRED_CHANGE_PCT_CAP, min(PRED_CHANGE_PCT_CAP, pred_change_pct))
|
| 496 |
+
|
| 497 |
lstm_score = 50.0
|
| 498 |
+
lstm_score += effective_pred_pct * 5
|
| 499 |
lstm_score += (days_above - 2.5) * 6
|
| 500 |
lstm_score += (confidence - 50) * 0.3
|
| 501 |
lstm_score = max(0, min(100, lstm_score))
|
|
|
|
| 510 |
else:
|
| 511 |
sent_score = 50.0
|
| 512 |
|
| 513 |
+
# High-vol regimes: downweight news so isolated headlines (e.g. one positive in a sell-off)
|
| 514 |
+
# don't override macro; keep 45% LSTM, 45% XGBoost, 10% news in calm regimes only
|
| 515 |
+
high_vol = regime in ("BEAR_HIGH_VOL", "BULL_HIGH_VOL")
|
| 516 |
+
if high_vol:
|
| 517 |
+
LSTM_W, XGB_W, SENT_W = 0.485, 0.485, 0.03 # 3% news in high vol
|
| 518 |
+
else:
|
| 519 |
+
LSTM_W, XGB_W, SENT_W = 0.45, 0.45, 0.10
|
| 520 |
+
|
| 521 |
composite = (lstm_score * LSTM_W) + (xgb_score * XGB_W) + (sent_score * SENT_W)
|
| 522 |
composite = round(max(0, min(100, composite)), 1)
|
| 523 |
|
|
|
|
| 527 |
"xgb_score": round(xgb_score, 1),
|
| 528 |
"sent_score": round(sent_score, 1),
|
| 529 |
"pred_change_pct": round(pred_change_pct, 2),
|
| 530 |
+
"pred_change_pct_capped": round(effective_pred_pct, 2),
|
| 531 |
"days_above": days_above,
|
| 532 |
+
"sentiment_weight_used": SENT_W,
|
| 533 |
}
|
| 534 |
|
| 535 |
|
|
|
|
| 632 |
scores = compute_composite_score(
|
| 633 |
predicted_prices, current_price, confidence,
|
| 634 |
indicators, sentiment_score,
|
| 635 |
+
regime=regime["regime"],
|
| 636 |
)
|
| 637 |
composite = scores["composite_score"]
|
| 638 |
|