From 3c4e56ffd2700592c1dd288416596057270616b0 Mon Sep 17 00:00:00 2001 From: GifariKemal Date: Mon, 9 Feb 2026 09:01:32 +0700 Subject: [PATCH] =?UTF-8?q?fix:=20model=20AUC=20metrics=20=E2=80=94=20read?= =?UTF-8?q?=20V2=20key=20names=20(xgb=5Ftrain=5Fscore/xgb=5Ftest=5Fscore)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V2 model stores AUC as xgb_train_score/xgb_test_score instead of V1's train_auc/test_auc. Dashboard now shows correct AUC: 73.4%. Co-Authored-By: Claude Opus 4.6 --- main_live.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main_live.py b/main_live.py index 0156239..983b53e 100644 --- a/main_live.py +++ b/main_live.py @@ -299,14 +299,31 @@ class TradingBot: except Exception: pass - # Use retrain results if available + # Use retrain results if available, then model's stored metrics, then auto_trainer if retrain_results: metrics["trainAuc"] = retrain_results.get("xgb_train_auc", 0) metrics["testAuc"] = retrain_results.get("xgb_test_auc", 0) metrics["sampleCount"] = retrain_results.get("sample_count", 0) + elif hasattr(self.ml_model, '_train_metrics') and self.ml_model._train_metrics: + # Use metrics stored in the model pickle (loaded on startup) + # V1 uses train_auc/test_auc, V2 uses xgb_train_score/xgb_test_score + tm = self.ml_model._train_metrics + metrics["trainAuc"] = tm.get("train_auc", 0) or tm.get("xgb_train_score", 0) + metrics["testAuc"] = tm.get("test_auc", 0) or tm.get("xgb_test_score", 0) + metrics["sampleCount"] = tm.get("train_samples", 0) + tm.get("test_samples", 0) elif hasattr(self, 'auto_trainer') and hasattr(self.auto_trainer, 'last_auc'): metrics["testAuc"] = self.auto_trainer.last_auc or 0 + # Also use model's stored feature importance if booster extraction failed + if not metrics["featureImportance"] and hasattr(self.ml_model, '_feature_importance') and self.ml_model._feature_importance: + fi = self.ml_model._feature_importance + total = sum(fi.values()) if fi else 1 + sorted_features = sorted(fi.items(), key=lambda x: x[1], reverse=True) + metrics["featureImportance"] = [ + {"name": name, "importance": round(val / total, 4)} + for name, val in sorted_features[:20] if val > 0 + ] + metrics_file = Path("data/model_metrics.json") metrics_file.parent.mkdir(parents=True, exist_ok=True) metrics_file.write_text(_json.dumps(metrics, indent=2))