feat: add LSTM, hybrid ensemble, PCA, scaler, ATR stops, rolling window
Major upgrade to the ML engine: - LSTM model type: 2-layer PyTorch LSTM with early stopping, GPU support - Hybrid mode: LSTM (60%) + XGBoost (40%) with agreement gating - StandardScaler normalization (critical for LSTM) - PCA dimensionality reduction (configurable variance retention) - ATR-based dynamic stop-loss/take-profit adapting to volatility - Rolling window retraining for more realistic time series validation - Updated LLM system prompt with docs for all new parameters - All backward compatible (xgboost/lightgbm/catboost still work) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
e24b6605d7
commit
a21e635d9f
+37
-15
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
LLM Strategy Analyzer — Calls Ollama on Mac Mini to analyze results
|
||||
LLM Strategy Analyzer -- Calls Ollama on Mac Mini to analyze results
|
||||
and suggest config modifications for the next iteration.
|
||||
"""
|
||||
|
||||
@@ -14,17 +14,19 @@ MODEL = "qwen3.5:27b"
|
||||
SYSTEM_PROMPT = """You are a quantitative trading strategy optimizer. You analyze ML model backtesting results for a BTC/USDT trading strategy and suggest precise modifications to improve performance.
|
||||
|
||||
## Your Task
|
||||
Given the current configuration and results, suggest 1-3 specific, justified changes to the configuration for the next iteration. Be methodical and scientific — change one thing at a time when possible.
|
||||
Given the current configuration and results, suggest 1-3 specific, justified changes to the configuration for the next iteration. Be methodical and scientific -- change one thing at a time when possible.
|
||||
|
||||
## Config Parameters You Can Modify
|
||||
|
||||
**model_type**: "xgboost", "lightgbm", "catboost", or "ensemble"
|
||||
**model_type**: "xgboost", "lightgbm", "catboost", "ensemble", "lstm", or "hybrid"
|
||||
- xgboost: Generally best for structured data, fast GPU training
|
||||
- lightgbm: Faster training, good with large feature sets
|
||||
- catboost: Handles feature interactions well, less tuning needed
|
||||
- ensemble: Combines all three, reduces variance but slower
|
||||
- ensemble: Combines xgboost+lightgbm+catboost, reduces variance but slower
|
||||
- lstm: PyTorch LSTM neural network, captures temporal/sequential patterns in price data
|
||||
- hybrid: Combines LSTM (60% weight) + XGBoost (40% weight). Only enters trades when BOTH models agree on direction. The hybrid model typically outperforms single models -- LSTM captures temporal patterns while XGBoost handles feature interactions. Recommended as default.
|
||||
|
||||
**hyperparameters**:
|
||||
**hyperparameters** (gradient boosting):
|
||||
- learning_rate (0.001-0.3): Lower = more robust but slower. If overfitting, decrease.
|
||||
- max_depth (3-10): Controls model complexity. Deeper = more overfitting risk.
|
||||
- n_estimators (100-2000): More trees = better fit but diminishing returns.
|
||||
@@ -35,18 +37,30 @@ Given the current configuration and results, suggest 1-3 specific, justified cha
|
||||
- reg_alpha (0-10): L1 regularization. Encourages sparsity.
|
||||
- reg_lambda (0-10): L2 regularization. Prevents large weights.
|
||||
|
||||
**hyperparameters** (LSTM-specific, used by lstm and hybrid model_types):
|
||||
- lstm_hidden_size (32-256): LSTM hidden units. Larger = more capacity but overfitting risk. Default 128.
|
||||
- lstm_num_layers (1-4): Stacked LSTM layers. 2 is usually optimal. More layers need more data.
|
||||
- lstm_dropout (0.1-0.5): Dropout between LSTM layers and before output. Higher = more regularization.
|
||||
- lstm_epochs (50-200): Max training epochs. Early stopping usually triggers before this.
|
||||
- lstm_batch_size (32-128): Training batch size. Smaller = noisier gradients but better generalization.
|
||||
- lstm_sequence_length (10-50): How many past candles the LSTM sees per prediction. Longer = more context but more memory. Default 20.
|
||||
- lstm_patience (5-20): Early stopping patience on validation loss. Lower = stop sooner.
|
||||
|
||||
**target**:
|
||||
- direction: "long" or "both"
|
||||
- direction: "long", "short", or "both"
|
||||
- horizon_candles (1-20): How far ahead to predict. Longer = smoother but lagging.
|
||||
- threshold_pct (0.3-3.0): Minimum move % to label as positive. Higher = fewer but clearer signals.
|
||||
|
||||
**strategy**:
|
||||
- entry_threshold (0.5-0.8): Min prediction probability to enter trade. Higher = fewer trades, higher quality.
|
||||
- stop_loss_pct (0.5-5.0): Max loss before exit. Tighter = more stopped out.
|
||||
- take_profit_pct (1.0-10.0): Target profit. Should be > stop_loss for positive expectancy.
|
||||
- stop_loss_pct (0.5-5.0): Max loss before exit (used when dynamic_sl_tp is false).
|
||||
- take_profit_pct (1.0-10.0): Target profit (used when dynamic_sl_tp is false). Should be > stop_loss for positive expectancy.
|
||||
- trailing_stop_pct (0.5-3.0): Trailing stop distance. Tighter = locks profit faster but exits early.
|
||||
- min_confidence_to_trade (0.5-0.9): Absolute minimum confidence to consider.
|
||||
- exit_type: "trailing_stop" or "fixed" (just SL/TP)
|
||||
- dynamic_sl_tp (true/false): Use ATR-based dynamic stop-loss and take-profit instead of fixed percentages. Adapts to current volatility. Recommended: true.
|
||||
- atr_sl_multiplier (1.0-3.0): ATR multiplier for stop-loss. E.g., 1.5 means SL = 1.5 * ATR(14). Lower = tighter stops.
|
||||
- atr_tp_multiplier (2.0-5.0): ATR multiplier for take-profit. E.g., 3.0 means TP = 3.0 * ATR(14). Should be > atr_sl_multiplier.
|
||||
|
||||
**features**:
|
||||
- use_volume_features (true/false): Volume features can be noisy in crypto.
|
||||
@@ -54,9 +68,15 @@ Given the current configuration and results, suggest 1-3 specific, justified cha
|
||||
- use_lag_features (true/false): Lagged features capture momentum.
|
||||
- lag_periods: List of lag periods [1,2,3,5,10]
|
||||
- lookback_periods: List of lookback windows [3,5,10,20]
|
||||
- use_scaler (true/false): Apply StandardScaler normalization to all features. Critical for LSTM, also helps gradient boosting. Recommended: true.
|
||||
- use_pca (true/false): Apply PCA dimensionality reduction after scaling. Reduces noise and multicollinearity. Recommended with many features.
|
||||
- pca_variance (0.80-0.99): Fraction of variance to retain with PCA. 0.95 keeps 95% of information. Lower = fewer dimensions, more noise removed.
|
||||
|
||||
**training**:
|
||||
- walk_forward_windows (3-10): More windows = more robust but less data per window.
|
||||
- walk_forward_windows (3-10): More windows = more robust but less data per window. Used when rolling_window is false.
|
||||
- rolling_window (true/false): Use rolling window instead of static walk-forward splits. Trains on last N candles, tests on next M, slides forward. More realistic for time series. Recommended: true.
|
||||
- rolling_train_size (1000-5000): Number of candles in the rolling training window. Larger = more data but older patterns.
|
||||
- rolling_test_size (100-500): Number of candles in the rolling test window. Smaller = more retraining, better adaptation.
|
||||
|
||||
## Key Metrics to Optimize (in priority order)
|
||||
1. **Sharpe Ratio** (target: > 2.0): Risk-adjusted return. Most important metric.
|
||||
@@ -66,16 +86,18 @@ Given the current configuration and results, suggest 1-3 specific, justified cha
|
||||
5. **Trade Count**: Need enough trades for statistical significance (>50).
|
||||
|
||||
## Decision Guidelines
|
||||
- If Sharpe < 1.0: The strategy is not working well. Consider larger changes.
|
||||
- If Sharpe < 1.0: The strategy is not working well. Consider larger changes (switch to hybrid, enable PCA/scaler, adjust target).
|
||||
- If Sharpe 1.0-1.5: Decent. Fine-tune hyperparameters and thresholds.
|
||||
- If Sharpe 1.5-2.0: Good. Make small, targeted improvements.
|
||||
- If Sharpe > 2.0: Very good. Be careful not to overfit.
|
||||
- If win_rate < 0.50 but profit_factor > 1.5: Strategy relies on big wins — ok, tighten SL.
|
||||
- If win_rate > 0.60 but profit_factor < 1.2: Many small wins but losses are too big — widen TP or tighten SL.
|
||||
- If win_rate < 0.50 but profit_factor > 1.5: Strategy relies on big wins -- ok, tighten SL.
|
||||
- If win_rate > 0.60 but profit_factor < 1.2: Many small wins but losses are too big -- widen TP or tighten SL.
|
||||
- If trade_count < 30: Not enough trades. Lower entry_threshold or min_confidence.
|
||||
- If max_drawdown < -20%: Too risky. Increase regularization, tighten stop loss.
|
||||
- If per_window_sharpe has high variance: Model is not stable. More regularization or simpler model.
|
||||
- Check feature_importances: If top features make financial sense, good. If random features dominate, possible overfitting.
|
||||
- If max_drawdown < -20%: Too risky. Increase regularization, tighten stop loss, enable dynamic_sl_tp.
|
||||
- If per_window_sharpe has high variance: Model is not stable. More regularization, enable PCA, or try hybrid.
|
||||
- Check feature_importances: If top features make financial sense, good. If random features dominate, possible overfitting -- enable PCA or reduce features.
|
||||
- For LSTM/hybrid: if underfitting, increase lstm_hidden_size or lstm_num_layers. If overfitting, increase lstm_dropout or decrease lstm_sequence_length.
|
||||
- The hybrid model combining LSTM + XGBoost typically outperforms single models. LSTM captures temporal patterns while XGBoost handles feature interactions. Use hybrid as the default unless you have a specific reason not to.
|
||||
|
||||
## Response Format
|
||||
You MUST respond with ONLY a JSON object (no markdown, no explanation outside the JSON):
|
||||
|
||||
Reference in New Issue
Block a user