"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(ml-strategy): a skipped retrain still predicts with the previous model (#1522)
Haozhe Wu950b1247dbf4
diff --git a/agent/src/skills/ml-strategy/SKILL.md b/agent/src/skills/ml-strategy/SKILL.mdindex 47b431f27..5304ee41a 100644--- a/agent/src/skills/ml-strategy/SKILL.md+++ b/agent/src/skills/ml-strategy/SKILL.md@@ -147,36 +147,34 @@ def walk_forward_predict(X_train = X_train[valid]y_train = y_train[valid]- if len(X_train) < 50:- continue-- # A single-class window (e.g. a sustained one-directional trend)- # crashes fit()/predict_proba() on every model type below. Skip- # the retrain and keep serving the previous model, same as the- # too-few-samples case above.- if len(np.unique(y_train)) < 2:- continue-- # Standardization: fit only on training set- scaler = StandardScaler()- X_train = scaler.fit_transform(X_train)-- # Build the model- if model_type == "random_forest":- model = RandomForestClassifier(- n_estimators=100, max_depth=5, random_state=42,- )- elif model_type == "gradient_boosting":- model = GradientBoostingClassifier(- n_estimators=100, max_depth=3, learning_rate=0.05,- random_state=42,- )- elif model_type == "ridge":- model = LogisticRegression(penalty="l2", C=1.0, random_state=42)- else:- raise ValueError(f"Unsupported model_type: {model_type}")-- model.fit(X_train, y_train)+ # Too few rows, or a single class (a sustained one-directional+ # trend, which crashes fit()/predict_proba() on every model type+ # below): skip the retrain. The previous model keeps serving; before+ # the first model there is no prediction.+ if len(X_train) >= 50 and len(np.unique(y_train)) >= 2:+ # Standardization: fit only on training set+ scaler = StandardScaler()+ X_train = scaler.fit_transform(X_train)++ # Build the model+ if model_type == "random_forest":+ model = RandomForestClassifier(+ n_estimators=100, max_depth=5, random_state=42,+ )+ elif model_type == "gradient_boosting":+ model = GradientBoostingClassifier(+ n_estimators=100, max_depth=3, learning_rate=0.05,+ random_state=42,+ )+ elif model_type == "ridge":+ model = LogisticRegression(penalty="l2", C=1.0, random_state=42)+ else:+ raise ValueError(f"Unsupported model_type: {model_type}")++ model.fit(X_train, y_train)++ if model is None:+ continue# Predict todayX_today = features.iloc[i : i + 1].valuesdiff --git a/agent/tests/test_ml_strategy_skill.py b/agent/tests/test_ml_strategy_skill.pyindex 0c5b76791..65421c328 100644--- a/agent/tests/test_ml_strategy_skill.py+++ b/agent/tests/test_ml_strategy_skill.py@@ -128,6 +128,46 @@ def test_walk_forward_skips_a_single_class_window_with_a_real_classifier() -> Noassert result.between(-1.0, 1.0).all()+def test_a_skipped_retrain_keeps_serving_the_previous_model() -> None:+ """The retrain day itself is predicted too, not left at 0.0 (#1522)."""+ from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier+ from sklearn.linear_model import LogisticRegression+ from sklearn.preprocessing import StandardScaler++ namespace = {+ "np": np,+ "pd": pd,+ "StandardScaler": StandardScaler,+ "RandomForestClassifier": RandomForestClassifier,+ "GradientBoostingClassifier": GradientBoostingClassifier,+ "LogisticRegression": LogisticRegression,+ }+ walk_forward_predict = _load_node("walk_forward_predict", ast.FunctionDef, namespace)++ rng = np.random.default_rng(1)+ features = pd.DataFrame({f"f{i}": rng.standard_normal(260) for i in range(3)})+ # Two classes early, one class from row 120: a 60-row sliding window that+ # starts at or after row 120 holds a single class.+ labels = pd.Series(np.where(np.arange(260) < 120, np.arange(260) % 2, 1.0))++ result = walk_forward_predict(+ features,+ labels,+ min_train_size=100,+ retrain_freq=20,+ model_type="ridge",+ window_type="sliding",+ sliding_size=60,+ prediction_horizon=1,+ )++ single_class_retrain_days = [+ i for i in range(100, 260) if (i - 100) % 20 == 0 and max(0, i - 60) >= 120+ ]+ assert single_class_retrain_days, "the fixture must reach a single-class window"+ assert all(result.iloc[i] != 0.0 for i in single_class_retrain_days)++def test_signal_engine_preserves_unavailable_future_labels_as_nan() -> None:captured: dict[str, object] = {}