Spaces:
Running
Running
Fix sniper reliability upsert and trading game run
Browse files
backend/app/services/market_sniper.py
CHANGED
|
@@ -913,6 +913,7 @@ def update_r_metrics(db: Session, simulations: list[dict]) -> None:
|
|
| 913 |
|
| 914 |
def update_signal_reliability_matrix(db: Session, rows: list[tuple[HistoricalPrediction, PredictionOutcome]], simulations: list[dict]) -> None:
|
| 915 |
by_prediction = {item["prediction_id"]: item for item in simulations if item.get("prediction_id")}
|
|
|
|
| 916 |
for prediction, outcome in rows:
|
| 917 |
sim = by_prediction.get(prediction.id)
|
| 918 |
if not sim:
|
|
@@ -931,21 +932,34 @@ def update_signal_reliability_matrix(db: Session, rows: list[tuple[HistoricalPre
|
|
| 931 |
"asset_class": prediction.asset_type or "Unknown",
|
| 932 |
"liquidity_bucket": "unknown",
|
| 933 |
}
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
SignalReliabilityMatrix.liquidity_bucket == key["liquidity_bucket"],
|
| 944 |
-
).limit(1)
|
| 945 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 946 |
if row is None:
|
| 947 |
row = SignalReliabilityMatrix(**key)
|
| 948 |
db.add(row)
|
|
|
|
| 949 |
values = (row.evidence or {}).get("r_values", [])
|
| 950 |
values = (values + [r_value])[-240:]
|
| 951 |
row.sample_count = len(values)
|
|
|
|
| 913 |
|
| 914 |
def update_signal_reliability_matrix(db: Session, rows: list[tuple[HistoricalPrediction, PredictionOutcome]], simulations: list[dict]) -> None:
|
| 915 |
by_prediction = {item["prediction_id"]: item for item in simulations if item.get("prediction_id")}
|
| 916 |
+
row_cache: dict[tuple[str, str, str, str, str, str, str, str], SignalReliabilityMatrix] = {}
|
| 917 |
for prediction, outcome in rows:
|
| 918 |
sim = by_prediction.get(prediction.id)
|
| 919 |
if not sim:
|
|
|
|
| 932 |
"asset_class": prediction.asset_type or "Unknown",
|
| 933 |
"liquidity_bucket": "unknown",
|
| 934 |
}
|
| 935 |
+
cache_key = (
|
| 936 |
+
key["signal_name"],
|
| 937 |
+
key["setup_type"],
|
| 938 |
+
key["timeframe"],
|
| 939 |
+
key["sector"],
|
| 940 |
+
key["market_regime"],
|
| 941 |
+
key["volatility_state"],
|
| 942 |
+
key["asset_class"],
|
| 943 |
+
key["liquidity_bucket"],
|
|
|
|
|
|
|
| 944 |
)
|
| 945 |
+
row = row_cache.get(cache_key)
|
| 946 |
+
if row is None:
|
| 947 |
+
row = db.scalar(
|
| 948 |
+
select(SignalReliabilityMatrix).where(
|
| 949 |
+
SignalReliabilityMatrix.signal_name == key["signal_name"],
|
| 950 |
+
SignalReliabilityMatrix.setup_type == key["setup_type"],
|
| 951 |
+
SignalReliabilityMatrix.timeframe == key["timeframe"],
|
| 952 |
+
SignalReliabilityMatrix.sector == key["sector"],
|
| 953 |
+
SignalReliabilityMatrix.market_regime == key["market_regime"],
|
| 954 |
+
SignalReliabilityMatrix.volatility_state == key["volatility_state"],
|
| 955 |
+
SignalReliabilityMatrix.asset_class == key["asset_class"],
|
| 956 |
+
SignalReliabilityMatrix.liquidity_bucket == key["liquidity_bucket"],
|
| 957 |
+
).limit(1)
|
| 958 |
+
)
|
| 959 |
if row is None:
|
| 960 |
row = SignalReliabilityMatrix(**key)
|
| 961 |
db.add(row)
|
| 962 |
+
row_cache[cache_key] = row
|
| 963 |
values = (row.evidence or {}).get("r_values", [])
|
| 964 |
values = (values + [r_value])[-240:]
|
| 965 |
row.sample_count = len(values)
|