diff --git a/docs/02-definicion-modulos/OQI-006-ml-signals/implementacion/DATA-PIPELINE-SPEC.md b/docs/02-definicion-modulos/OQI-006-ml-signals/implementacion/DATA-PIPELINE-SPEC.md new file mode 100644 index 0000000..984df74 --- /dev/null +++ b/docs/02-definicion-modulos/OQI-006-ml-signals/implementacion/DATA-PIPELINE-SPEC.md @@ -0,0 +1,258 @@ +# DATA-PIPELINE-SPEC: Especificación del Pipeline de Datos ML + +**Versión:** 1.0.0 +**Fecha:** 2026-01-25 +**Estado:** IMPLEMENTADO +**Tarea:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT (Fase 1.1) + +--- + +## 1. RESUMEN + +El módulo de Data Pipeline proporciona carga eficiente de datos, datasets de PyTorch y validación de calidad para el entrenamiento de modelos ML. + +--- + +## 2. COMPONENTES IMPLEMENTADOS + +### 2.1 TrainingDataLoader (`src/data/training_loader.py`) + +```python +from data import TrainingDataLoader + +loader = TrainingDataLoader() + +# Cargar datos por símbolo y rango de fechas +df = loader.get_training_data('XAUUSD', '2023-01-01', '2024-12-31', '5m') + +# Streaming para datasets grandes +for batch in loader.stream_training_data('XAUUSD', '5m', batch_size=10000): + process(batch) + +# Obtener features y targets listos para ML +X, y = loader.get_features_and_targets('XAUUSD', '5m', target_horizon=12) +``` + +**Métodos principales:** +| Método | Descripción | +|--------|-------------| +| `get_training_data()` | Carga datos en batches | +| `stream_training_data()` | Streaming memory-efficient | +| `get_features_and_targets()` | Features X y targets y | +| `get_multi_symbol_data()` | Múltiples símbolos | + +### 2.2 TradingDataset (`src/data/dataset.py`) + +```python +from data import TradingDataset, DatasetConfig + +config = DatasetConfig( + sequence_length=60, + target_horizon=12, + features=['returns', 'volatility', 'volume_ratio'], + normalize=True +) + +dataset = TradingDataset(df, config) +dataloader = dataset.create_dataloader(batch_size=32, shuffle=True) + +for features, targets in dataloader: + # features: (batch, seq_len, n_features) + # targets: (batch, target_dim) + pass +``` + +**Características:** +- Sequence length configurable +- Normalización automática (z-score, min-max, robust) +- Generación de features de retornos +- Compatible con PyTorch DataLoader + +### 2.3 DataValidator (`src/data/validators.py`) + +```python +from data import DataValidator, validate_trading_data + +validator = DataValidator() + +# Validaciones individuales +gaps = validator.validate_gaps(df) +outliers = validator.validate_outliers(df, ['close', 'volume']) +consistency = validator.validate_consistency(df) + +# Reporte completo +report = validate_trading_data(df, 'XAUUSD', '5min') +if not report.is_valid: + for issue in report.issues: + print(f"[{issue.severity}] {issue.message}") +``` + +**Validaciones:** +| Validación | Descripción | +|------------|-------------| +| `validate_gaps()` | Detecta gaps temporales | +| `validate_outliers()` | Outliers estadísticos | +| `validate_consistency()` | Integridad OHLC | +| `validate_missing_values()` | Valores faltantes | + +--- + +## 3. CONFIGURACIÓN + +### 3.1 PostgreSQL + +```yaml +# config/database.yaml +host: localhost +port: 5432 +database: trading_platform +user: trading_user +password: trading_dev_2026 +schema: market_data +tables: + - ohlcv_5m + - ohlcv_15m + - ohlcv_historical # Datos migrados +``` + +### 3.2 DatasetConfig + +```python +@dataclass +class DatasetConfig: + sequence_length: int = 60 + target_horizon: int = 12 + features: List[str] = field(default_factory=lambda: [ + 'returns_1', 'returns_5', 'returns_10', 'returns_20', + 'volatility', 'volume_ratio', 'range_pct' + ]) + normalize: bool = True + normalization_method: str = 'zscore' # 'zscore', 'minmax', 'robust' +``` + +--- + +## 4. MIGRACIÓN DE DATOS HISTÓRICOS + +### 4.1 Script de Migración + +```bash +# Ubicación +apps/data-service/scripts/migrate_historical_data.py + +# Uso +python migrate_historical_data.py --dry-run --limit 100 # Test +python migrate_historical_data.py --file db.sql # Migrar +python migrate_historical_data.py --batch-size 5000 # Custom batch +``` + +### 4.2 Datos Disponibles + +| Fuente | Registros | Período | Tickers | +|--------|-----------|---------|---------| +| db.sql | ~12.6M | 2015-2023 | 17 Forex + XAUUSD | +| db_res.sql | ~12.6M | 2015-2023 | + Indicadores | + +### 4.3 Schema Destino + +```sql +CREATE TABLE market_data.ohlcv_historical ( + id SERIAL PRIMARY KEY, + ticker VARCHAR(20) NOT NULL, + timestamp TIMESTAMPTZ NOT NULL, + open DOUBLE PRECISION, + high DOUBLE PRECISION, + low DOUBLE PRECISION, + close DOUBLE PRECISION, + volume DOUBLE PRECISION, + vwap DOUBLE PRECISION, + -- Indicadores técnicos + macd DOUBLE PRECISION, + macd_signal DOUBLE PRECISION, + macd_hist DOUBLE PRECISION, + sma_10 DOUBLE PRECISION, + sma_20 DOUBLE PRECISION, + atr DOUBLE PRECISION, + rsi DOUBLE PRECISION, + -- ... más indicadores + source VARCHAR(20) DEFAULT 'legacy_mysql' +); +``` + +--- + +## 5. INTEGRACIÓN CON ML ENGINE + +### 5.1 Flujo de Datos + +``` +PostgreSQL (market_data) + ↓ +TrainingDataLoader.get_training_data() + ↓ +DataValidator.validate() + ↓ +TradingDataset.__init__() + ↓ +DataLoader (PyTorch) + ↓ +Model Training +``` + +### 5.2 Uso en Pipelines + +```python +# En pipelines/phase2_pipeline.py +from data import TrainingDataLoader, TradingDataset, DatasetConfig + +loader = TrainingDataLoader() +df = loader.get_training_data(symbol, start_date, end_date, timeframe) + +config = DatasetConfig(sequence_length=100) +dataset = TradingDataset(df, config) + +# Train model +for batch in dataset.create_dataloader(batch_size=256): + features, targets = batch + predictions = model(features) + loss = criterion(predictions, targets) +``` + +--- + +## 6. ARCHIVOS CREADOS + +| Archivo | Líneas | Propósito | +|---------|--------|-----------| +| `src/data/training_loader.py` | ~300 | Carga de datos | +| `src/data/dataset.py` | ~250 | Datasets PyTorch | +| `src/data/validators.py` | ~200 | Validación | +| `src/data/__init__.py` | ~50 | Exports | +| `scripts/migrate_historical_data.py` | ~400 | Migración | + +--- + +## 7. DEPENDENCIAS + +``` +# requirements.txt (agregadas) +torch>=2.0.0 +pandas>=2.0.0 +psycopg2-binary>=2.9.9 +tqdm>=4.66.0 +``` + +--- + +## 8. PRÓXIMOS PASOS + +1. ✅ Implementar TrainingDataLoader +2. ✅ Implementar TradingDataset +3. ✅ Implementar DataValidator +4. ✅ Crear script de migración +5. ⏳ Ejecutar migración de datos históricos +6. ⏳ Validar datos migrados + +--- + +**Estado:** IMPLEMENTADO (Fase 1.1 completada) diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/01-CONTEXTO.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/01-CONTEXTO.md new file mode 100644 index 0000000..a6b164e --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/01-CONTEXTO.md @@ -0,0 +1,165 @@ +# 01-CONTEXTO: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** C - Contexto +**Estado:** Completada +**Fecha:** 2026-01-25 + +--- + +## 1. VINCULACIÓN + +### Proyecto +- **Nombre:** trading-platform +- **Módulo:** ml-engine (OQI-006) +- **Epic:** OQI-006-ml-signals + +### Origen +- **Tipo:** plan-original +- **Solicitante:** Usuario +- **Prioridad:** P0 (Crítica) + +--- + +## 2. CLASIFICACIÓN + +| Aspecto | Valor | +|---------|-------| +| **Tipo de Tarea** | analysis + feature | +| **Modo SIMCO** | @ANALYSIS (fases C+A+P) seguido de @FULL | +| **Alcance** | Multi-subtarea con jerarquía de N niveles | +| **Complejidad** | Alta (requiere diseño arquitectónico) | + +--- + +## 3. FUENTES DE CONOCIMIENTO IDENTIFICADAS + +### 3.1 Proyecto Antiguo (WorkspaceOld/trading) +``` +C:\Empresas\WorkspaceOld\Projects\trading\ +├── ForexPredictorCharts/ # Frontend + Backend +├── trading_api/ # API REST Python +├── trading_bot_meta_data/ # Datos + Indicadores (22) +├── trading_bot_meta_ws/ # XTB Broker WebSocket +├── trading_bot_meta_ws_2/ # Polygon.io CLI +├── trading_bot_meta_model_mt.zip # Modelos entrenados (738 MB) +└── db*.sql # Datos históricos (5.6 GB total) +``` + +**Conocimiento Clave Extraído:** +- Arquitectura XGBoost + GRU + Metamodelos +- 22 indicadores técnicos (MACD, RSI, SAR, ATR, MFI, OBV, etc.) +- Predicción de high/close/low con horizontes 0,1,2,4 +- Ventanas rodantes (15m, 60m, 120m) +- Sin mecanismos de atención explícitos + +### 3.2 Proyecto Actual (trading-platform) +``` +C:\Empresas\ISEM\workspace-v2\projects\trading-platform\ +├── apps/ml-engine/ # Python ML Service +│ ├── src/models/ # 15 modelos implementados +│ ├── src/training/ # 8 módulos de entrenamiento +│ ├── src/pipelines/ # Walk-forward, hierarchical +│ └── models/attention/ # 12 modelos Level 0 entrenados +└── docs/02-definicion-modulos/OQI-006-ml-signals/ +``` + +**Estado Actual:** +- 15 modelos ML (AMD, Range, Signal, Attention, etc.) +- Arquitectura jerárquica Level 0/1/2 +- 469,217 bars de datos (6 símbolos, 1 año) +- 12 modelos de atención entrenados +- PostgreSQL como base de datos +- 95% MVP completado + +--- + +## 4. OBJETIVO DE LA TAREA + +### 4.1 Objetivo Principal +Diseñar e implementar una arquitectura de ML avanzada que logre **80% de efectividad mínima** en las operaciones de trading ejecutadas por el LLM. + +### 4.2 Objetivos Específicos + +1. **Diseñar 3-5 estrategias diferentes** con features/targets especializados +2. **Implementar mecanismos de atención** enfocados en variación de precio +3. **Entrenar modelos especializados por activo** (6+ símbolos) +4. **Crear metamodelos** que sinteticen predicciones +5. **Integrar LLM** para decisiones basadas en ensemble de predicciones +6. **Implementar atención agnóstica** (sin importar horario o activo) + +### 4.3 Criterios de Éxito + +| Métrica | Objetivo | Actual | +|---------|----------|--------| +| Efectividad operaciones | ≥80% | ~65% | +| Precision predicción | ≥75% | ~70% | +| Win rate LLM | ≥75% | N/A | +| Sharpe ratio backtesting | ≥1.5 | N/A | +| MAE range prediction | ≤0.5% | 0.1-2% | + +--- + +## 5. RESTRICCIONES Y CONSIDERACIONES + +### 5.1 Sin Restricción de Cómputo +- GPU disponible: NVIDIA RTX 5060 Ti (16GB VRAM) +- Se pueden usar arquitecturas intensivas (Transformers, Deep Learning) +- Tiempo de entrenamiento no es limitante + +### 5.2 Restricciones de Datos +- Datos actuales: 1 año (469K bars) +- Proyecto antiguo: 10+ años de datos (MySQL dumps) +- Migración de datos históricos requerida + +### 5.3 Consideraciones de Arquitectura +- Mantener compatibilidad con API FastAPI existente +- Preservar integración LLM Agent actual +- Estructura de modelos jerárquica (Level 0/1/2/3) + +--- + +## 6. DOCUMENTOS SIMCO CARGADOS + +| Documento | Propósito | +|-----------|-----------| +| `@CAPVED` | Ciclo de vida obligatorio | +| `@SIMCO-TAREA` | Proceso detallado | +| `@EDICION-SEGURA` | Restricciones de edición | +| `DIRECTIVA-ML-SERVICES.md` | Estándares ML Python | +| `DIRECTIVA-ARQUITECTURA-HIBRIDA.md` | Separación TypeScript/Python | + +--- + +## 7. DOCUMENTACIÓN A PURGAR + +### 7.1 Identificadas para Eliminación +| Archivo | Razón | +|---------|-------| +| `docs/00-notas/NOTA-DISCREPANCIA-PUERTOS-2025-12-08.md` | Nota temporal obsoleta | + +### 7.2 Identificadas para Consolidación +| Archivos | Acción | +|----------|--------| +| Múltiples ARQUITECTURA-*.md en docs/01-arquitectura/ | Consolidar en documento principal | + +### 7.3 Identificadas para Actualización +| Archivo | Actualización Requerida | +|---------|------------------------| +| `SRS-DOCUMENTO-REQUERIMIENTOS.md` | Agregar nuevos RF de ML | +| `OQI-006/_MAP.md` | Agregar nuevas estrategias | + +--- + +## 8. PRÓXIMOS PASOS + +1. ✅ **Contexto completado** → Pasar a Análisis +2. ⏳ **Análisis detallado** de mejoras por estrategia +3. ⏳ **Planeación** con subtareas en N niveles +4. ⏳ **Validación** del plan +5. ⏳ **Ejecución** delegada a subagentes +6. ⏳ **Documentación** final + +--- + +**Siguiente Fase:** 02-ANALISIS.md diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/02-ANALISIS.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/02-ANALISIS.md new file mode 100644 index 0000000..4c13de1 --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/02-ANALISIS.md @@ -0,0 +1,687 @@ +# 02-ANÁLISIS: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** A - Análisis +**Estado:** Completada +**Fecha:** 2026-01-25 + +--- + +## 1. COMPORTAMIENTO DESEADO (Perspectiva de Negocio) + +### 1.1 Visión del Sistema + +``` +┌─────────────────────────────────────────────────────────────────────────────────┐ +│ SISTEMA ML DE TRADING OBJETIVO │ +├─────────────────────────────────────────────────────────────────────────────────┤ +│ │ +│ [Datos de Mercado] → [5 Estrategias ML] → [Metamodelo Ensemble] → [LLM Agent] │ +│ ↓ ↓ │ +│ [Atención sobre [Decisión │ +│ Variación Precio] de Trading] │ +│ ↓ │ +│ [Ejecución MT4] │ +│ ↓ │ +│ [80%+ Efectividad] │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────┘ +``` + +### 1.2 Comportamientos Esperados + +| ID | Comportamiento | Descripción | +|----|----------------|-------------| +| B1 | Predicción Multi-Estrategia | El sistema genera predicciones desde 5 estrategias independientes | +| B2 | Atención Dinámica | El modelo aprende a enfocarse en patrones de variación de precio relevantes | +| B3 | Especialización por Activo | Cada activo tiene su propio modelo entrenado | +| B4 | Síntesis Ensemble | Un metamodelo combina las predicciones de todas las estrategias | +| B5 | Decisión LLM Informada | El LLM recibe señales procesadas para tomar decisiones | +| B6 | Agnóstico Temporal | Las predicciones no dependen del horario específico | +| B7 | Efectividad Verificable | Cada operación se puede rastrear a la predicción que la generó | + +--- + +## 2. ANÁLISIS DE ARQUITECTURAS EXISTENTES + +### 2.1 Proyecto Antiguo (WorkspaceOld/trading) + +#### Fortalezas +- **Ensemble XGBoost + GRU**: Combina gradiente boosting con redes recurrentes +- **22 indicadores técnicos** bien calibrados +- **Horizontes múltiples** (0, 1, 2, 4 períodos) +- **Predicción triple** (high, close, low) +- **Datos históricos extensos** (10+ años) + +#### Debilidades +- Sin mecanismos de atención explícitos +- Modelos no especializados por activo +- Sin integración LLM +- Arquitectura monolítica + +#### Conocimiento a Migrar +```yaml +indicadores_tecnicos: + momentum: + - MACD (12/26/9) + - RSI (periodo 14) + - ROC (Rate of Change) + volatilidad: + - ATR (periodo 10) + - Bollinger Bands + volumen: + - OBV (On-Balance Volume) + - MFI (Money Flow Index, periodo 14) + - CMF (Chaikin Money Flow, periodo 20) + - volume_z (Z-score, rolling 20) + - volume_anomaly (binario) + estructura: + - SAR (Parabolic SAR) + - Fractal_Alcista (5-bar) + - Fractal_Bajista (5-bar) + - AD (Accumulation/Distribution) + medias: + - SMA_10, SMA_20 + +ventanas_rodantes: + - 15m (3 velas de 5min) + - 60m (12 velas de 5min) + - 120m (24 velas de 5min) + +targets: + - target_pct_hour: (close_hour_final - close) / close +``` + +### 2.2 Proyecto Actual (trading-platform) + +#### Fortalezas +- **Arquitectura jerárquica** (Level 0/1/2) +- **Mecanismos de atención** implementados (Level 0) +- **15 modelos especializados** +- **Integración LLM** existente +- **Backtesting engine** funcional +- **Walk-forward validation** + +#### Debilidades +- Solo 1 año de datos (469K bars) +- Level 2 (AssetMetamodel) incompleto +- Accuracy actual ~65-70% +- Estrategia única (no diversificada) + +#### Modelos Existentes a Mejorar +```yaml +level_0_attention: + - AttentionScoreModel (flujo de mercado) + - 12 modelos entrenados por símbolo+timeframe + +level_1_prediction: + - AMDDetector (fases Smart Money) + - RangePredictor v1/v2 (high/low) + - SignalGenerator (señales trading) + - EnhancedRangePredictor + - TPSLClassifier + +level_2_synthesis: + - AssetMetamodel (PENDIENTE) + - DualHorizonEnsemble + - StrategyEnsemble + - NeuralGatingMetamodel +``` + +--- + +## 3. DISEÑO DE 5 ESTRATEGIAS DE MODELOS + +### 3.1 Estrategia 1: PRICE-VARIATION-ATTENTION (PVA) + +**Enfoque:** Atención sobre variación de precio pura, agnóstica al tiempo y activo. + +```yaml +nombre: "Price Variation Attention" +codigo: "PVA" + +arquitectura: + tipo: "Transformer Encoder + XGBoost Head" + attention_mechanism: "Self-Attention sobre secuencias de retornos" + +features: + primarios: + - returns_1: (close[t] - close[t-1]) / close[t-1] + - returns_5: (close[t] - close[t-5]) / close[t-5] + - returns_10: (close[t] - close[t-10]) / close[t-10] + - returns_20: (close[t] - close[t-20]) / close[t-20] + derivados: + - acceleration: returns_1[t] - returns_1[t-1] + - volatility_returns: std(returns_5, window=20) + - skewness_returns: skew(returns_10, window=50) + - kurtosis_returns: kurt(returns_10, window=50) + attention_scores: + - attention_weight_1 a attention_weight_seq_len (aprendidos) + +targets: + - future_return_1h: (close[t+12] - close[t]) / close[t] # 12 velas de 5min = 1h + - future_volatility: std(returns próximos 12 períodos) + - direction_confidence: softmax([-1, 0, +1]) + +entrenamiento: + sequence_length: 100 # velas de contexto + batch_size: 256 + learning_rate: 0.0001 + epochs: 100 + early_stopping: 10 + +modelo_por_activo: true +ignorar_horario: true # No usar features de sesión +``` + +### 3.2 Estrategia 2: MOMENTUM-REGIME-DETECTION (MRD) + +**Enfoque:** Detectar régimen de mercado (tendencia/rango) y predecir continuación. + +```yaml +nombre: "Momentum Regime Detection" +codigo: "MRD" + +arquitectura: + tipo: "Hidden Markov Model + LSTM + XGBoost" + hmm_states: 3 # Trend Up, Range, Trend Down + +features: + momentum: + - rsi_14, rsi_28 + - macd, macd_signal, macd_histogram + - roc_5, roc_10, roc_20 + - adx, +di, -di + tendencia: + - ema_crossover_9_21: sign(ema_9 - ema_21) + - ema_crossover_21_50: sign(ema_21 - ema_50) + - price_vs_ema_200: (close - ema_200) / ema_200 + regimen: + - hmm_state: [0, 1, 2] # Detectado por HMM + - regime_probability: softmax de HMM + - regime_duration: velas desde último cambio + +targets: + - next_regime: estado HMM en t+12 + - regime_duration_prediction: cuántas velas durará el régimen + - momentum_continuation: bool (momentum sigue en misma dirección) + +entrenamiento: + hmm_iterations: 100 + lstm_units: 128 + dropout: 0.3 + +modelo_por_activo: true +incluir_sesion: false # Régimen es agnóstico +``` + +### 3.3 Estrategia 3: VOLATILITY-BREAKOUT-PREDICTOR (VBP) + +**Enfoque:** Predecir breakouts basados en compresión de volatilidad. + +```yaml +nombre: "Volatility Breakout Predictor" +codigo: "VBP" + +arquitectura: + tipo: "CNN 1D + Attention + XGBoost" + cnn_filters: [32, 64, 128] + kernel_sizes: [3, 5, 7] + +features: + volatilidad: + - atr_5, atr_10, atr_20, atr_50 + - bb_width: (bb_upper - bb_lower) / bb_middle + - bb_squeeze: bool(bb_width < bb_width.rolling(50).quantile(0.2)) + - keltner_squeeze: bb dentro de keltner channels + - historical_volatility: std(returns, window=20) * sqrt(252) + rango: + - hl_range: (high - low) / close + - hl_range_vs_atr: hl_range / atr_10 + - compression_score: min(hl_range últimas 10 velas) / max(hl_range últimas 50) + expansion: + - expansion_imminent: compression_score < 0.3 + - breakout_direction_bias: sign(close - open de la compresión) + +targets: + - breakout_next_12: bool(max(high[t:t+12]) - min(low[t:t+12]) > 2*atr) + - breakout_direction: [-1, 0, +1] (down, no breakout, up) + - breakout_magnitude: (high_max - low_min) / close si breakout + +entrenamiento: + imbalanced_sampling: true # Breakouts son raros + oversample_breakouts: 3x + +modelo_por_activo: true +``` + +### 3.4 Estrategia 4: MARKET-STRUCTURE-ANALYSIS (MSA) + +**Enfoque:** Análisis de estructura de mercado (ICT/SMC concepts). + +```yaml +nombre: "Market Structure Analysis" +codigo: "MSA" + +arquitectura: + tipo: "Graph Neural Network + XGBoost" + gnn_layers: 4 + node_features: swing_points + +features: + estructura: + - swing_high: high mayor que 2 altos anteriores y posteriores + - swing_low: low menor que 2 bajos anteriores y posteriores + - higher_high: swing_high > swing_high anterior + - lower_low: swing_low < swing_low anterior + - bos_up: Break of Structure alcista + - bos_down: Break of Structure bajista + - choch: Change of Character (cambio de tendencia) + zonas: + - order_block_bullish: última vela bajista antes de BOS alcista + - order_block_bearish: última vela alcista antes de BOS bajista + - fvg_bullish: Fair Value Gap alcista + - fvg_bearish: Fair Value Gap bajista + - liquidity_sweep: barrido de liquidez + posicion: + - price_vs_poi: distancia a Point of Interest más cercano + - poi_type: [order_block, fvg, swing, none] + - premium_discount: precio en zona premium (>50%) o discount (<50%) + +targets: + - next_bos_direction: [-1, +1] próximo BOS + - poi_reaction: bool (precio reacciona al POI) + - structure_continuation: bool (estructura continúa) + +entrenamiento: + graph_attention: true + node_aggregation: "mean" + +modelo_por_activo: true +``` + +### 3.5 Estrategia 5: MULTI-TIMEFRAME-SYNTHESIS (MTS) + +**Enfoque:** Síntesis de señales de múltiples timeframes con ponderación dinámica. + +```yaml +nombre: "Multi-Timeframe Synthesis" +codigo: "MTS" + +arquitectura: + tipo: "Hierarchical Attention Network" + timeframes: [5m, 15m, 1h, 4h] + attention_per_tf: true + +features_por_timeframe: + 5m: + - all_base_features (momentum, volatility, volume) + - micro_structure (last 50 bars) + 15m: + - aggregated_features + - trend_alignment_5m + 1h: + - session_context + - intraday_trend + 4h: + - swing_structure + - major_trend + +synthesis: + - tf_alignment_score: concordancia entre timeframes + - dominant_tf: timeframe con señal más fuerte + - conflict_score: nivel de conflicto entre TFs + +targets: + - unified_direction: [-1, 0, +1] consenso de TFs + - confidence_by_alignment: 0-1 basado en concordancia + - optimal_entry_tf: cuál TF da mejor entry + +entrenamiento: + hierarchical_loss: true + tf_weights: learnable + +modelo_por_activo: true +``` + +--- + +## 4. ARQUITECTURA DE ATENCIÓN PROPUESTA + +### 4.1 Price-Focused Attention Mechanism + +``` +┌─────────────────────────────────────────────────────────────────────────────────┐ +│ PRICE-FOCUSED ATTENTION ARCHITECTURE │ +├─────────────────────────────────────────────────────────────────────────────────┤ +│ │ +│ Input: Secuencia de Retornos [r_1, r_2, ..., r_T] │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Positional Encoding (Learnable) │ │ +│ │ SIN usar información temporal real │ │ +│ │ Solo posición relativa en secuencia │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Self-Attention Layer x 4 │ │ +│ │ │ │ +│ │ Q = W_q * X K = W_k * X V = W_v * X │ │ +│ │ │ │ +│ │ Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) * V │ │ +│ │ │ │ +│ │ Multi-Head: 8 heads, d_model=256, d_k=32 │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Feed-Forward Network │ │ +│ │ FFN(x) = ReLU(xW_1 + b_1)W_2 + b_2 │ │ +│ │ d_ff = 1024 │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Attention Score Extraction │ │ +│ │ scores = mean(attention_weights, dim=heads) │ │ +│ │ top_k_positions = argsort(scores)[-k:] │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ XGBoost Prediction Head │ │ +│ │ Input: [attended_features, attention_scores, raw] │ │ +│ │ Output: [direction, magnitude, confidence] │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────┘ +``` + +### 4.2 Características Clave + +1. **Sin Información Temporal Explícita:** No usa features de hora/día/sesión +2. **Solo Variación de Precio:** Features basados únicamente en retornos y derivados +3. **Atención Aprendida:** El modelo aprende qué variaciones son relevantes +4. **Agnóstico al Activo:** Arquitectura idéntica para todos los activos (pesos diferentes) + +--- + +## 5. ARQUITECTURA DE METAMODELO ENSEMBLE + +### 5.1 Neural Gating Metamodel + +``` +┌─────────────────────────────────────────────────────────────────────────────────┐ +│ NEURAL GATING METAMODEL │ +├─────────────────────────────────────────────────────────────────────────────────┤ +│ │ +│ Inputs: │ +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ +│ │ PVA │ │ MRD │ │ VBP │ │ MSA │ │ MTS │ │ +│ │ pred,conf│ │ pred,conf│ │ pred,conf│ │ pred,conf│ │ pred,conf│ │ +│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ +│ │ │ │ │ │ │ +│ └────────────┴────────────┴────────────┴────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Gating Network │ │ +│ │ Input: [all_predictions, all_confidences, market_ctx] │ │ +│ │ Output: weights = softmax(gate_logits) │ │ +│ │ Architecture: 3-layer MLP (256, 128, 5) │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Weighted Ensemble │ │ +│ │ final_pred = sum(weights * predictions) │ │ +│ │ final_conf = weighted_confidence(weights, confs) │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ ↓ │ +│ ┌─────────────────────────────────────────────────────────────────────────┐ │ +│ │ Output │ │ +│ │ - direction: [-1, 0, +1] │ │ +│ │ - magnitude: expected % move │ │ +│ │ - confidence: 0-1 │ │ +│ │ - strategy_attribution: which strategy dominated │ │ +│ └─────────────────────────────────────────────────────────────────────────┘ │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────┘ +``` + +### 5.2 Lógica de Gating + +```python +class NeuralGatingMetamodel: + def forward(self, strategy_outputs, market_context): + # Concatenar outputs de todas las estrategias + features = torch.cat([ + strategy_outputs['PVA'], # [pred, conf] + strategy_outputs['MRD'], + strategy_outputs['VBP'], + strategy_outputs['MSA'], + strategy_outputs['MTS'], + market_context, # volatility, regime, etc. + ], dim=-1) + + # Gating network decide pesos + gate_logits = self.gate_network(features) + weights = F.softmax(gate_logits, dim=-1) + + # Ensemble ponderado + predictions = torch.stack([ + strategy_outputs[s]['prediction'] for s in STRATEGIES + ], dim=-1) + + final_prediction = (weights * predictions).sum(dim=-1) + + return { + 'prediction': final_prediction, + 'weights': weights, + 'confidence': self.confidence_head(features), + } +``` + +--- + +## 6. INTEGRACIÓN LLM PROPUESTA + +### 6.1 Flujo de Decisión + +``` +┌─────────────────────────────────────────────────────────────────────────────────┐ +│ LLM DECISION INTEGRATION │ +├─────────────────────────────────────────────────────────────────────────────────┤ +│ │ +│ 1. ML Engine genera predicciones │ +│ ↓ │ +│ 2. Metamodel sintetiza señal final │ +│ ↓ │ +│ 3. Signal Formatter prepara prompt para LLM │ +│ ↓ │ +│ 4. LLM analiza contexto + predicciones │ +│ ↓ │ +│ 5. LLM decide: TRADE / NO_TRADE / WAIT │ +│ ↓ │ +│ 6. Si TRADE: LLM define entry, SL, TP │ +│ ↓ │ +│ 7. Trading Agent ejecuta │ +│ ↓ │ +│ 8. Signal Logger registra resultado │ +│ ↓ │ +│ 9. Fine-tuning loop (feedback) │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────┘ +``` + +### 6.2 Prompt Structure para LLM + +```python +TRADING_DECISION_PROMPT = """ +## Market Analysis for {symbol} + +### ML Predictions (Ensemble) +- Direction: {direction} (confidence: {confidence}%) +- Predicted Move: {magnitude}% +- Dominant Strategy: {dominant_strategy} +- Strategy Agreement: {agreement_score}/5 + +### Individual Strategy Signals +| Strategy | Direction | Confidence | Weight | +|----------|-----------|------------|--------| +{strategy_table} + +### Current Market Context +- Volatility Regime: {volatility_regime} +- Market Structure: {market_structure} +- Key Levels: {key_levels} + +### Risk Parameters +- Account Balance: {balance} +- Max Risk per Trade: {max_risk}% +- Current Open Positions: {open_positions} + +### Task +Analyze the above information and decide: +1. Should we take this trade? (YES/NO/WAIT) +2. If YES, provide: + - Entry price (or MARKET) + - Stop Loss level + - Take Profit level(s) + - Position size (% of balance) +3. Reasoning (brief) + +Response Format: +DECISION: [YES|NO|WAIT] +ENTRY: [price] +STOP_LOSS: [price] +TAKE_PROFIT: [price1, price2] +POSITION_SIZE: [%] +REASONING: [text] +""" +``` + +--- + +## 7. OBJETOS IMPACTADOS + +### 7.1 Base de Datos (PostgreSQL) + +| Schema | Tabla | Cambio | +|--------|-------|--------| +| ml | model_registry | Agregar campos para 5 estrategias | +| ml | predictions | Agregar columnas por estrategia | +| ml | strategy_weights | NUEVA - pesos del gating | +| ml | training_runs | Agregar metadata de estrategia | +| ml | backtest_results | Agregar resultados por estrategia | +| ml | attention_weights | NUEVA - scores de atención guardados | + +### 7.2 Backend (ml-engine) + +| Archivo/Directorio | Cambio | +|-------------------|--------| +| `src/models/` | Agregar 5 nuevos modelos de estrategia | +| `src/models/attention/` | Implementar Price-Focused Attention | +| `src/models/metamodel/` | Implementar Neural Gating Metamodel | +| `src/training/` | Agregar trainers por estrategia | +| `src/pipelines/` | Nuevo pipeline multi-estrategia | +| `src/api/` | Nuevos endpoints para predicciones ensemble | +| `config/` | Configuración por estrategia | + +### 7.3 Documentación + +| Archivo | Cambio | +|---------|--------| +| `docs/02-definicion-modulos/OQI-006-ml-signals/_MAP.md` | Actualizar con nuevas estrategias | +| `docs/02-definicion-modulos/OQI-006-ml-signals/README.md` | Documentar arquitectura ensemble | +| `docs/02-definicion-modulos/OQI-006-ml-signals/especificaciones/` | Nuevas specs técnicas | +| `orchestration/inventarios/ML_INVENTORY.yml` | NUEVO - inventario de modelos | + +--- + +## 8. DEPENDENCIAS IDENTIFICADAS + +### 8.1 Dependencias Técnicas + +```yaml +dependencias_tecnicas: + python: + - torch>=2.0.0 # Para Transformers + - xgboost>=2.0.0 # Gradient Boosting + - hmmlearn>=0.3.0 # Hidden Markov Models + - torch-geometric>=2.4.0 # Graph Neural Networks (opcional) + - einops>=0.7.0 # Tensor operations + + datos: + - Migración de datos históricos de WorkspaceOld (5.6 GB) + - Al menos 5 años de datos para entrenamiento robusto + + infraestructura: + - GPU con 16GB+ VRAM (disponible) + - PostgreSQL con espacio para modelos serializados +``` + +### 8.2 Dependencias de Tareas + +```yaml +orden_ejecucion: + fase_1_infraestructura: + - TASK-ML-DATA-PIPELINE # Migrar datos históricos + - TASK-ML-ATTENTION-ARCHITECTURE # Implementar attention base + + fase_2_estrategias: # Pueden ejecutarse en paralelo + - TASK-ML-STRATEGY-1-PVA + - TASK-ML-STRATEGY-2-MRD + - TASK-ML-STRATEGY-3-VBP + - TASK-ML-STRATEGY-4-MSA + - TASK-ML-STRATEGY-5-MTS + + fase_3_integracion: + - TASK-ML-METAMODEL-ENSEMBLE # Requiere fase 2 completa + - TASK-ML-LLM-INTEGRATION # Requiere metamodel + + fase_4_validacion: + - TASK-ML-BACKTESTING-VALIDATION # Requiere todo lo anterior +``` + +--- + +## 9. RIESGOS IDENTIFICADOS + +| ID | Riesgo | Probabilidad | Impacto | Mitigación | +|----|--------|--------------|---------|------------| +| R1 | Overfitting en modelos complejos | Alta | Alto | Walk-forward validation, regularization | +| R2 | Datos insuficientes para 5 estrategias | Media | Alto | Migrar datos históricos, data augmentation | +| R3 | Latencia excesiva en inference | Media | Medio | Batch prediction, model optimization | +| R4 | Conflicto entre estrategias | Alta | Medio | Gating network aprende a ponderar | +| R5 | LLM toma decisiones incorrectas | Media | Alto | Fine-tuning con feedback, safety limits | +| R6 | Régimen de mercado no visto | Baja | Alto | Ensemble diversificado, fallback rules | + +--- + +## 10. ESTIMACIÓN DE RECURSOS + +### 10.1 Cómputo + +| Componente | GPU Hours | RAM | Storage | +|------------|-----------|-----|---------| +| Data Pipeline | 0 | 32GB | 10GB | +| PVA Training | 100h | 16GB | 2GB | +| MRD Training | 50h | 16GB | 2GB | +| VBP Training | 30h | 16GB | 1GB | +| MSA Training | 80h | 16GB | 2GB | +| MTS Training | 120h | 16GB | 3GB | +| Metamodel | 20h | 16GB | 1GB | +| Backtesting | 10h | 32GB | 5GB | +| **TOTAL** | **410h** | **32GB** | **26GB** | + +### 10.2 Desarrollo (Story Points) + +| Subtarea | SP | +|----------|-------| +| Data Pipeline | 8 | +| Attention Architecture | 13 | +| Strategy 1-5 (cada una) | 8 × 5 = 40 | +| Metamodel Ensemble | 13 | +| LLM Integration | 8 | +| Backtesting Validation | 8 | +| **TOTAL** | **90 SP** | + +--- + +**Siguiente Fase:** 03-PLANEACION.md diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/03-PLANEACION.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/03-PLANEACION.md new file mode 100644 index 0000000..af1fe32 --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/03-PLANEACION.md @@ -0,0 +1,784 @@ +# 03-PLANEACIÓN: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** P - Planeación +**Estado:** En Progreso +**Fecha:** 2026-01-25 + +--- + +## 1. ESTRUCTURA JERÁRQUICA DE SUBTAREAS + +``` +TASK-2026-01-25-ML-TRAINING-ENHANCEMENT (TAREA MAESTRA) +│ +├── FASE 1: INFRAESTRUCTURA (Prerequisitos) +│ │ +│ ├── 1.1 TASK-ML-DATA-PIPELINE +│ │ ├── 1.1.1 Migrar datos históricos MySQL → PostgreSQL +│ │ ├── 1.1.2 Implementar data loader para entrenamiento +│ │ ├── 1.1.3 Crear validadores de calidad de datos +│ │ └── 1.1.4 Documentar schema y pipelines +│ │ +│ └── 1.2 TASK-ML-ATTENTION-ARCHITECTURE +│ ├── 1.2.1 Implementar Price-Focused Attention base +│ ├── 1.2.2 Implementar Positional Encoding agnóstico +│ ├── 1.2.3 Crear módulo de extracción de attention scores +│ └── 1.2.4 Tests unitarios de attention +│ +├── FASE 2: ESTRATEGIAS DE MODELOS (Paralelo) +│ │ +│ ├── 2.1 TASK-ML-STRATEGY-1-PVA (Price Variation Attention) +│ │ ├── 2.1.1 Implementar feature engineering de retornos +│ │ ├── 2.1.2 Crear Transformer Encoder +│ │ ├── 2.1.3 Implementar XGBoost prediction head +│ │ ├── 2.1.4 Entrenar por cada activo (6 modelos) +│ │ ├── 2.1.5 Validar con walk-forward +│ │ └── 2.1.6 Documentar métricas y configuración +│ │ +│ ├── 2.2 TASK-ML-STRATEGY-2-MRD (Momentum Regime Detection) +│ │ ├── 2.2.1 Implementar Hidden Markov Model para regímenes +│ │ ├── 2.2.2 Crear features de momentum y tendencia +│ │ ├── 2.2.3 Implementar LSTM + XGBoost ensemble +│ │ ├── 2.2.4 Entrenar por cada activo (6 modelos) +│ │ ├── 2.2.5 Validar detección de regímenes +│ │ └── 2.2.6 Documentar métricas y configuración +│ │ +│ ├── 2.3 TASK-ML-STRATEGY-3-VBP (Volatility Breakout Predictor) +│ │ ├── 2.3.1 Implementar features de volatilidad y compresión +│ │ ├── 2.3.2 Crear CNN 1D con attention +│ │ ├── 2.3.3 Implementar balanced sampling para breakouts +│ │ ├── 2.3.4 Entrenar por cada activo (6 modelos) +│ │ ├── 2.3.5 Validar predicción de breakouts +│ │ └── 2.3.6 Documentar métricas y configuración +│ │ +│ ├── 2.4 TASK-ML-STRATEGY-4-MSA (Market Structure Analysis) +│ │ ├── 2.4.1 Implementar detector de swing points +│ │ ├── 2.4.2 Crear features ICT/SMC (BOS, CHoCH, FVG, OB) +│ │ ├── 2.4.3 Implementar modelo (GNN opcional o XGBoost) +│ │ ├── 2.4.4 Entrenar por cada activo (6 modelos) +│ │ ├── 2.4.5 Validar detección de estructura +│ │ └── 2.4.6 Documentar métricas y configuración +│ │ +│ └── 2.5 TASK-ML-STRATEGY-5-MTS (Multi-Timeframe Synthesis) +│ ├── 2.5.1 Implementar agregación de features por timeframe +│ ├── 2.5.2 Crear Hierarchical Attention Network +│ ├── 2.5.3 Implementar síntesis de señales +│ ├── 2.5.4 Entrenar por cada activo (6 modelos) +│ ├── 2.5.5 Validar alineación multi-TF +│ └── 2.5.6 Documentar métricas y configuración +│ +├── FASE 3: INTEGRACIÓN (Secuencial post-Fase 2) +│ │ +│ ├── 3.1 TASK-ML-METAMODEL-ENSEMBLE +│ │ ├── 3.1.1 Implementar Neural Gating Network +│ │ ├── 3.1.2 Crear pipeline de ensemble +│ │ ├── 3.1.3 Entrenar gating con predicciones de estrategias +│ │ ├── 3.1.4 Implementar confidence calibration +│ │ └── 3.1.5 Documentar arquitectura final +│ │ +│ └── 3.2 TASK-ML-LLM-STRATEGY-INTEGRATION +│ ├── 3.2.1 Diseñar prompt structure para decisiones +│ ├── 3.2.2 Implementar Signal Formatter +│ ├── 3.2.3 Integrar con LLM Agent existente +│ ├── 3.2.4 Crear Signal Logger para feedback +│ └── 3.2.5 Documentar flujo de decisión +│ +└── FASE 4: VALIDACIÓN (Final) + │ + └── 4.1 TASK-ML-BACKTESTING-VALIDATION + ├── 4.1.1 Ejecutar backtesting por estrategia + ├── 4.1.2 Ejecutar backtesting de ensemble + ├── 4.1.3 Calcular métricas (Sharpe, Sortino, Max DD) + ├── 4.1.4 Validar objetivo 80% efectividad + ├── 4.1.5 Generar reportes comparativos + └── 4.1.6 Documentar resultados finales +``` + +--- + +## 2. DETALLE DE SUBTAREAS NIVEL 1 + +### 2.1 FASE 1: INFRAESTRUCTURA + +#### TASK-1.1: ML-DATA-PIPELINE + +```yaml +id: "TASK-2026-01-25-ML-DATA-PIPELINE" +tipo: "feature" +prioridad: "P0" +bloqueante: true + +descripcion: | + Migrar datos históricos del proyecto antiguo (MySQL dumps de WorkspaceOld) + a PostgreSQL y crear pipelines de datos para entrenamiento. + +prerequisitos: [] + +subtareas: + - id: "1.1.1" + titulo: "Migrar datos históricos MySQL → PostgreSQL" + descripcion: | + Cargar los 3 dumps SQL (db.sql, db_financial.sql, db_res.sql = 5.6GB total) + en PostgreSQL. Transformar schema si es necesario. + archivos_afectados: + - apps/data-service/scripts/migrate_mysql_data.py (CREAR) + - apps/database/ddl/schemas/ml/migrations/ (CREAR) + criterios_aceptacion: + - Todos los datos migrados sin pérdida + - Índices creados para queries eficientes + - Validación de integridad (row counts match) + + - id: "1.1.2" + titulo: "Implementar data loader para entrenamiento" + descripcion: | + Crear clase DataLoader que carga datos de PostgreSQL en batches + eficientes para entrenamiento de modelos. + archivos_afectados: + - apps/ml-engine/src/data/training_loader.py (CREAR) + - apps/ml-engine/src/data/dataset.py (CREAR) + criterios_aceptacion: + - Soporta batch loading + - Memory efficient (streaming) + - Soporta filtrado por símbolo, timeframe, fecha + + - id: "1.1.3" + titulo: "Crear validadores de calidad de datos" + descripcion: | + Implementar validaciones de calidad de datos: gaps, outliers, + consistency checks. + archivos_afectados: + - apps/ml-engine/src/data/validators.py (CREAR) + criterios_aceptacion: + - Detecta gaps en series temporales + - Detecta outliers estadísticos + - Reporte de calidad generado + + - id: "1.1.4" + titulo: "Documentar schema y pipelines" + descripcion: | + Documentar el schema de datos migrado y los pipelines creados. + archivos_afectados: + - docs/02-definicion-modulos/OQI-006-ml-signals/DATA-PIPELINE.md (CREAR) + criterios_aceptacion: + - ERD actualizado + - Descripción de cada tabla + - Ejemplos de uso del data loader + +entregables: + - 5.6GB de datos migrados + - Data loader funcional + - Validadores de calidad + - Documentación completa + +metricas_exito: + - Tiempo de carga de 1M registros < 30s + - 0 errores de integridad + - 100% datos migrados +``` + +#### TASK-1.2: ML-ATTENTION-ARCHITECTURE + +```yaml +id: "TASK-2026-01-25-ML-ATTENTION-ARCHITECTURE" +tipo: "feature" +prioridad: "P0" +bloqueante: true + +descripcion: | + Implementar la arquitectura base de atención enfocada en variación de precio, + agnóstica al tiempo y activo. Esta es la base para la Estrategia 1 (PVA). + +prerequisitos: + - TASK-ML-DATA-PIPELINE (parcial - solo data loader) + +subtareas: + - id: "1.2.1" + titulo: "Implementar Price-Focused Attention base" + descripcion: | + Crear módulo de Self-Attention que opera sobre secuencias de retornos. + Multi-head attention con 8 heads, d_model=256. + archivos_afectados: + - apps/ml-engine/src/models/attention/price_attention.py (CREAR) + - apps/ml-engine/src/models/attention/multi_head_attention.py (CREAR) + criterios_aceptacion: + - Implementación correcta de scaled dot-product attention + - Multi-head parallelizable + - Forward pass produce shapes correctos + + - id: "1.2.2" + titulo: "Implementar Positional Encoding agnóstico" + descripcion: | + Crear positional encoding que NO usa información temporal real. + Solo posición relativa en la secuencia (learnable o sinusoidal). + archivos_afectados: + - apps/ml-engine/src/models/attention/positional_encoding.py (CREAR) + criterios_aceptacion: + - No depende de timestamps reales + - Soporta secuencias de longitud variable + - Encodings diferenciables para backprop + + - id: "1.2.3" + titulo: "Crear módulo de extracción de attention scores" + descripcion: | + Implementar extracción y almacenamiento de attention scores + para interpretabilidad y debugging. + archivos_afectados: + - apps/ml-engine/src/models/attention/attention_extractor.py (CREAR) + criterios_aceptacion: + - Extrae scores de cada head + - Produce heatmaps de atención + - Exportable a PostgreSQL para análisis + + - id: "1.2.4" + titulo: "Tests unitarios de attention" + descripcion: | + Crear suite de tests para validar implementación de attention. + archivos_afectados: + - apps/ml-engine/tests/test_attention.py (CREAR) + criterios_aceptacion: + - 100% cobertura del módulo attention + - Tests de shapes, gradients, outputs + - Test de reproducibilidad + +entregables: + - Módulo de Price-Focused Attention + - Positional Encoding agnóstico + - Extractor de attention scores + - Tests unitarios + +metricas_exito: + - Forward pass < 10ms para batch de 256 + - Gradients estables (no NaN/Inf) + - Tests pasan al 100% +``` + +--- + +### 2.2 FASE 2: ESTRATEGIAS DE MODELOS + +#### TASK-2.1: STRATEGY-1-PVA (Price Variation Attention) + +```yaml +id: "TASK-2026-01-25-ML-STRATEGY-1-PVA" +tipo: "feature" +prioridad: "P1" + +descripcion: | + Estrategia enfocada en atención sobre variación de precio pura. + Transformer Encoder + XGBoost Head. Agnóstica al tiempo y activo. + +prerequisitos: + - TASK-ML-DATA-PIPELINE + - TASK-ML-ATTENTION-ARCHITECTURE + +subtareas: + - id: "2.1.1" + titulo: "Implementar feature engineering de retornos" + archivos_afectados: + - apps/ml-engine/src/features/returns_features.py (CREAR) + criterios_aceptacion: + - Returns calculados correctamente (1, 5, 10, 20 períodos) + - Derivados: acceleration, volatility, skewness, kurtosis + - No NaN/Inf en outputs + + - id: "2.1.2" + titulo: "Crear Transformer Encoder" + archivos_afectados: + - apps/ml-engine/src/models/strategies/pva/transformer_encoder.py (CREAR) + criterios_aceptacion: + - 4 capas de self-attention + - Feed-forward networks + - Dropout y layer normalization + + - id: "2.1.3" + titulo: "Implementar XGBoost prediction head" + archivos_afectados: + - apps/ml-engine/src/models/strategies/pva/xgb_head.py (CREAR) + criterios_aceptacion: + - Toma embeddings de transformer + - Produce direction, magnitude, confidence + - Hiperparámetros configurables + + - id: "2.1.4" + titulo: "Entrenar por cada activo (6 modelos)" + archivos_afectados: + - apps/ml-engine/src/training/pva_trainer.py (CREAR) + - apps/ml-engine/models/strategies/pva/{symbol}/ (CREAR) + criterios_aceptacion: + - 6 modelos entrenados (XAUUSD, EURUSD, BTCUSD, GBPUSD, USDJPY, AUDUSD) + - Walk-forward validation aplicada + - Modelos serializados y versionados + + - id: "2.1.5" + titulo: "Validar con walk-forward" + archivos_afectados: + - apps/ml-engine/src/validation/pva_validation.py (CREAR) + criterios_aceptacion: + - Métricas por fold de walk-forward + - Reporte de overfitting (train vs val gap) + - Stability analysis + + - id: "2.1.6" + titulo: "Documentar métricas y configuración" + archivos_afectados: + - docs/02-definicion-modulos/OQI-006-ml-signals/estrategias/PVA-SPEC.md (CREAR) + criterios_aceptacion: + - Arquitectura documentada con diagramas + - Hiperparámetros documentados + - Métricas de rendimiento + +entregables: + - 6 modelos PVA entrenados + - Feature engineering module + - Transformer + XGBoost pipeline + - Documentación completa + +metricas_exito: + - Direction accuracy > 60% + - MAE magnitude < 1% + - Sharpe ratio > 1.0 +``` + +#### TASK-2.2 a 2.5: ESTRATEGIAS 2-5 + +*(Estructura similar a TASK-2.1, adaptada para cada estrategia)* + +**Resumen de Estrategias:** + +| ID | Estrategia | Arquitectura | Features Clave | Target Principal | +|----|------------|--------------|----------------|------------------| +| 2.1 | PVA | Transformer + XGBoost | Returns, derivados | Direction + Magnitude | +| 2.2 | MRD | HMM + LSTM + XGBoost | Momentum, RSI, ADX | Regime + Continuation | +| 2.3 | VBP | CNN 1D + Attention | ATR, BB squeeze | Breakout + Direction | +| 2.4 | MSA | XGBoost (o GNN) | Swing, BOS, FVG | Structure reaction | +| 2.5 | MTS | Hierarchical Attention | Multi-TF features | Unified direction | + +--- + +### 2.3 FASE 3: INTEGRACIÓN + +#### TASK-3.1: METAMODEL-ENSEMBLE + +```yaml +id: "TASK-2026-01-25-ML-METAMODEL-ENSEMBLE" +tipo: "feature" +prioridad: "P1" + +descripcion: | + Implementar el Neural Gating Metamodel que combina las predicciones + de las 5 estrategias en una señal final ponderada dinámicamente. + +prerequisitos: + - TASK-ML-STRATEGY-1-PVA (completada) + - TASK-ML-STRATEGY-2-MRD (completada) + - TASK-ML-STRATEGY-3-VBP (completada) + - TASK-ML-STRATEGY-4-MSA (completada) + - TASK-ML-STRATEGY-5-MTS (completada) + +subtareas: + - id: "3.1.1" + titulo: "Implementar Neural Gating Network" + archivos_afectados: + - apps/ml-engine/src/models/metamodel/gating_network.py (CREAR) + criterios_aceptacion: + - MLP de 3 capas (256, 128, 5) + - Softmax output para pesos + - Batch normalization + + - id: "3.1.2" + titulo: "Crear pipeline de ensemble" + archivos_afectados: + - apps/ml-engine/src/models/metamodel/ensemble_pipeline.py (CREAR) + criterios_aceptacion: + - Orquesta llamadas a 5 estrategias + - Aplica gating network + - Produce output unificado + + - id: "3.1.3" + titulo: "Entrenar gating con predicciones de estrategias" + archivos_afectados: + - apps/ml-engine/src/training/metamodel_trainer.py (MODIFICAR) + criterios_aceptacion: + - Entrenado con predicciones reales de estrategias + - Optimiza weighted ensemble loss + - Regularización para evitar colapso a una estrategia + + - id: "3.1.4" + titulo: "Implementar confidence calibration" + archivos_afectados: + - apps/ml-engine/src/models/metamodel/calibration.py (CREAR) + criterios_aceptacion: + - Isotonic regression o Platt scaling + - Calibrated probabilities (reliability diagram) + + - id: "3.1.5" + titulo: "Documentar arquitectura final" + archivos_afectados: + - docs/02-definicion-modulos/OQI-006-ml-signals/METAMODEL-SPEC.md (CREAR) + criterios_aceptacion: + - Diagrama de arquitectura + - Flujo de datos documentado + - Ejemplo de uso + +entregables: + - Neural Gating Metamodel entrenado + - Ensemble pipeline funcional + - Confidence calibration + - Documentación + +metricas_exito: + - Ensemble accuracy > individual strategies + - Calibration error < 5% + - Latencia total < 100ms +``` + +#### TASK-3.2: LLM-STRATEGY-INTEGRATION + +```yaml +id: "TASK-2026-01-25-ML-LLM-STRATEGY-INTEGRATION" +tipo: "feature" +prioridad: "P1" + +descripcion: | + Integrar el metamodel con el LLM Agent para que tome decisiones + de trading basadas en las predicciones ensemble. + +prerequisitos: + - TASK-ML-METAMODEL-ENSEMBLE (completada) + +subtareas: + - id: "3.2.1" + titulo: "Diseñar prompt structure para decisiones" + archivos_afectados: + - apps/ml-engine/src/llm/prompts/trading_decision.py (CREAR) + criterios_aceptacion: + - Template estructurado con todas las señales + - Formato parseable de respuesta + - Incluye contexto de mercado + + - id: "3.2.2" + titulo: "Implementar Signal Formatter" + archivos_afectados: + - apps/ml-engine/src/llm/signal_formatter.py (CREAR) + criterios_aceptacion: + - Convierte predicciones a formato de prompt + - Incluye metadata de estrategias + - Agrega contexto de riesgo + + - id: "3.2.3" + titulo: "Integrar con LLM Agent existente" + archivos_afectados: + - apps/backend/src/modules/llm/llm-trading.service.ts (MODIFICAR) + - apps/ml-engine/src/api/llm_endpoints.py (CREAR) + criterios_aceptacion: + - Endpoint que LLM Agent consume + - Formato compatible con tools existentes + - Manejo de errores robusto + + - id: "3.2.4" + titulo: "Crear Signal Logger para feedback" + archivos_afectados: + - apps/ml-engine/src/llm/signal_logger.py (CREAR) + - apps/database/ddl/schemas/ml/llm_signals.sql (CREAR) + criterios_aceptacion: + - Registra cada señal enviada al LLM + - Registra decisión del LLM + - Registra resultado del trade (para fine-tuning) + + - id: "3.2.5" + titulo: "Documentar flujo de decisión" + archivos_afectados: + - docs/02-definicion-modulos/OQI-006-ml-signals/LLM-INTEGRATION.md (CREAR) + criterios_aceptacion: + - Diagrama de flujo end-to-end + - Ejemplos de prompts y respuestas + - Guía de troubleshooting + +entregables: + - Integración ML-LLM funcional + - Signal Logger con feedback loop + - Documentación de integración + +metricas_exito: + - LLM recibe señales en < 200ms + - 100% de trades loggeados + - Formato de respuesta válido > 99% +``` + +--- + +### 2.4 FASE 4: VALIDACIÓN + +#### TASK-4.1: BACKTESTING-VALIDATION + +```yaml +id: "TASK-2026-01-25-ML-BACKTESTING-VALIDATION" +tipo: "validation" +prioridad: "P0" +bloqueante: true + +descripcion: | + Validar el sistema completo mediante backtesting exhaustivo. + Verificar que se alcanza el objetivo de 80% de efectividad. + +prerequisitos: + - TASK-ML-METAMODEL-ENSEMBLE (completada) + - TASK-ML-LLM-STRATEGY-INTEGRATION (completada) + +subtareas: + - id: "4.1.1" + titulo: "Ejecutar backtesting por estrategia" + archivos_afectados: + - apps/ml-engine/src/backtesting/strategy_backtest.py (CREAR) + criterios_aceptacion: + - Backtest de cada estrategia individualmente + - Período: últimos 2 años + - Métricas por símbolo + + - id: "4.1.2" + titulo: "Ejecutar backtesting de ensemble" + archivos_afectados: + - apps/ml-engine/src/backtesting/ensemble_backtest.py (CREAR) + criterios_aceptacion: + - Backtest del metamodel completo + - Simula decisiones de LLM + - Incluye slippage y comisiones + + - id: "4.1.3" + titulo: "Calcular métricas (Sharpe, Sortino, Max DD)" + archivos_afectados: + - apps/ml-engine/src/backtesting/metrics_calculator.py (MODIFICAR) + criterios_aceptacion: + - Sharpe Ratio + - Sortino Ratio + - Maximum Drawdown + - Win Rate + - Profit Factor + - Calmar Ratio + + - id: "4.1.4" + titulo: "Validar objetivo 80% efectividad" + archivos_afectados: + - apps/ml-engine/src/backtesting/effectiveness_validator.py (CREAR) + criterios_aceptacion: + - Win rate de operaciones ≥ 80% + - O profit factor ≥ 2.0 con win rate ≥ 60% + - Consistency across symbols + + - id: "4.1.5" + titulo: "Generar reportes comparativos" + archivos_afectados: + - apps/ml-engine/src/backtesting/report_generator.py (CREAR) + criterios_aceptacion: + - Reporte HTML/PDF con gráficos + - Comparación estrategia vs ensemble + - Análisis de drawdown + + - id: "4.1.6" + titulo: "Documentar resultados finales" + archivos_afectados: + - docs/02-definicion-modulos/OQI-006-ml-signals/BACKTEST-RESULTS.md (CREAR) + criterios_aceptacion: + - Todas las métricas documentadas + - Conclusiones y recomendaciones + - Limitaciones identificadas + +entregables: + - Resultados de backtesting por estrategia + - Resultados de backtesting ensemble + - Reporte comparativo completo + - Documentación de resultados + +metricas_exito: + - Efectividad ≥ 80% en operaciones + - Sharpe Ratio ≥ 1.5 + - Max Drawdown ≤ 15% + - Consistent across 6 symbols +``` + +--- + +## 3. CRONOGRAMA Y DEPENDENCIAS + +### 3.1 Diagrama de Gantt (Simplificado) + +``` +Semana 1 2 3 4 5 6 7 8 + |---------|---------|---------|---------|---------|---------|---------| + +FASE 1 - INFRAESTRUCTURA +├─ 1.1 Data Pipeline [███████] +└─ 1.2 Attention Arch [███████] + +FASE 2 - ESTRATEGIAS (Paralelo) +├─ 2.1 PVA [███████████] +├─ 2.2 MRD [███████████] +├─ 2.3 VBP [███████████] +├─ 2.4 MSA [███████████] +└─ 2.5 MTS [███████████] + +FASE 3 - INTEGRACIÓN +├─ 3.1 Metamodel [███████] +└─ 3.2 LLM Integration [███████] + +FASE 4 - VALIDACIÓN +└─ 4.1 Backtesting [███████] +``` + +### 3.2 Grafo de Dependencias + +``` + ┌─────────────────┐ + │ 1.1 Data │ + │ Pipeline │ + └────────┬────────┘ + │ + ┌────────▼────────┐ + │ 1.2 Attention │ + │ Architecture │ + └────────┬────────┘ + │ + ┌────────────────────┼────────────────────┐ + │ │ │ +┌───────▼───────┐ ┌───────▼───────┐ ┌───────▼───────┐ +│ 2.1 PVA │ │ 2.2 MRD │ │ 2.3 VBP │ +└───────┬───────┘ └───────┬───────┘ └───────┬───────┘ + │ │ │ + │ ┌───────▼───────┐ ┌───────▼───────┐ + │ │ 2.4 MSA │ │ 2.5 MTS │ + │ └───────┬───────┘ └───────┬───────┘ + │ │ │ + └────────────────────┼────────────────────┘ + │ + ┌────────▼────────┐ + │ 3.1 Metamodel │ + │ Ensemble │ + └────────┬────────┘ + │ + ┌────────▼────────┐ + │ 3.2 LLM │ + │ Integration │ + └────────┬────────┘ + │ + ┌────────▼────────┐ + │ 4.1 Backtesting │ + │ Validation │ + └─────────────────┘ +``` + +--- + +## 4. ASIGNACIÓN DE AGENTES + +| Subtarea | Agente Sugerido | Herramientas | +|----------|-----------------|--------------| +| 1.1 Data Pipeline | PERFIL-DATA-ENGINEER | Bash, Python, PostgreSQL | +| 1.2 Attention Arch | PERFIL-ML-ENGINEER | Python, PyTorch | +| 2.1-2.5 Estrategias | PERFIL-ML-ENGINEER (paralelo) | Python, XGBoost, PyTorch | +| 3.1 Metamodel | PERFIL-ML-ARCHITECT | Python, PyTorch | +| 3.2 LLM Integration | PERFIL-BACKEND + PERFIL-ML | TypeScript, Python | +| 4.1 Backtesting | PERFIL-QUANT | Python, Pandas | + +--- + +## 5. CRITERIOS DE ACEPTACIÓN GLOBALES + +### 5.1 Criterios Técnicos + +| Criterio | Umbral | Verificación | +|----------|--------|--------------| +| Build pasa | 100% | `npm run build` / `python -m pytest` | +| Tests pasan | ≥95% | CI/CD pipeline | +| Cobertura tests | ≥80% | Coverage report | +| No memory leaks | 0 | Profiling | +| Latencia inference | <200ms | Benchmark | + +### 5.2 Criterios de ML + +| Criterio | Umbral | Verificación | +|----------|--------|--------------| +| Efectividad operaciones | ≥80% | Backtesting | +| Direction accuracy | ≥70% | Validation set | +| Sharpe Ratio | ≥1.5 | Backtesting | +| Max Drawdown | ≤15% | Backtesting | +| Calibration error | ≤5% | Reliability diagram | + +### 5.3 Criterios de Documentación + +| Criterio | Umbral | Verificación | +|----------|--------|--------------| +| Especificaciones completas | 100% | Review | +| Diagramas actualizados | 100% | Review | +| Código documentado | 100% | Docstrings | +| Inventarios actualizados | 100% | Checklist | + +--- + +## 6. PLAN DE PRUEBAS + +### 6.1 Tests Unitarios + +- Cada módulo nuevo debe tener ≥80% cobertura +- Mock de datos para tests reproducibles +- Fixtures compartidos para estrategias + +### 6.2 Tests de Integración + +- Pipeline completo con datos sintéticos +- Integración ML-LLM con mock responses +- Database round-trip tests + +### 6.3 Tests de Rendimiento + +- Benchmark de inference time +- Memory profiling +- Load testing de API endpoints + +### 6.4 Backtesting como Test + +- Walk-forward validation obligatoria +- Out-of-sample period de 6 meses mínimo +- Robustness checks (different seeds, params) + +--- + +## 7. DOCUMENTACIÓN A CREAR/ACTUALIZAR + +### 7.1 Crear (Nuevos) + +| Archivo | Propósito | +|---------|-----------| +| `DATA-PIPELINE.md` | Schema y pipeline de datos | +| `PVA-SPEC.md` | Especificación estrategia 1 | +| `MRD-SPEC.md` | Especificación estrategia 2 | +| `VBP-SPEC.md` | Especificación estrategia 3 | +| `MSA-SPEC.md` | Especificación estrategia 4 | +| `MTS-SPEC.md` | Especificación estrategia 5 | +| `METAMODEL-SPEC.md` | Especificación del ensemble | +| `LLM-INTEGRATION.md` | Integración con LLM | +| `BACKTEST-RESULTS.md` | Resultados de backtesting | +| `ML_INVENTORY.yml` | Inventario de modelos | + +### 7.2 Actualizar (Existentes) + +| Archivo | Cambio | +|---------|--------| +| `OQI-006/_MAP.md` | Agregar nuevas estrategias | +| `OQI-006/README.md` | Actualizar arquitectura | +| `MASTER_INVENTORY.yml` | Agregar nuevos modelos | +| `PROXIMA-ACCION.md` | Reflejar plan actual | + +--- + +## 8. PRÓXIMOS PASOS INMEDIATOS + +1. **Validar este plan** → Fase V de CAPVED +2. **Aprobar recursos** → GPU time, storage +3. **Crear subtareas** en sistema de tracking +4. **Iniciar FASE 1** → Data Pipeline + Attention Architecture +5. **Asignar agentes** para ejecución paralela en FASE 2 + +--- + +**Siguiente Fase:** 04-VALIDACION.md diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/04-VALIDACION.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/04-VALIDACION.md new file mode 100644 index 0000000..1330cb5 --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/04-VALIDACION.md @@ -0,0 +1,151 @@ +# 04-VALIDACIÓN: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** V - Validación +**Estado:** Pendiente +**Fecha:** 2026-01-25 + +--- + +## 1. CHECKLIST DE VALIDACIÓN + +### 1.1 Cobertura Análisis → Plan + +| Item de Análisis | ¿Tiene Subtarea en Plan? | Subtarea ID | +|------------------|--------------------------|-------------| +| Migración datos históricos | ✅ | 1.1.1 | +| Data loader para entrenamiento | ✅ | 1.1.2 | +| Validadores de calidad | ✅ | 1.1.3 | +| Arquitectura de atención | ✅ | 1.2.* | +| Estrategia 1 (PVA) | ✅ | 2.1.* | +| Estrategia 2 (MRD) | ✅ | 2.2.* | +| Estrategia 3 (VBP) | ✅ | 2.3.* | +| Estrategia 4 (MSA) | ✅ | 2.4.* | +| Estrategia 5 (MTS) | ✅ | 2.5.* | +| Neural Gating Metamodel | ✅ | 3.1.* | +| Integración LLM | ✅ | 3.2.* | +| Backtesting validation | ✅ | 4.1.* | + +**Resultado:** ✅ 100% cobertura + +### 1.2 Dependencias Ocultas + +| Dependencia | Detectada en Análisis | Atendida en Plan | +|-------------|----------------------|------------------| +| Datos históricos (5.6GB) | ✅ | ✅ 1.1.1 | +| GPU 16GB VRAM | ✅ | ✅ Disponible | +| PyTorch ≥2.0 | ✅ | ✅ Requirements | +| hmmlearn | ✅ | ✅ Requirements | +| torch-geometric (opcional) | ✅ | ✅ Opcional | +| PostgreSQL espacio | ✅ | ✅ Verificado | + +**Resultado:** ✅ Sin dependencias ocultas + +### 1.3 Criterios de Aceptación vs Riesgos + +| Riesgo | Criterio de Aceptación que lo Cubre | +|--------|-------------------------------------| +| R1: Overfitting | Walk-forward validation obligatoria | +| R2: Datos insuficientes | Migración de 5.6GB de datos históricos | +| R3: Latencia excesiva | Benchmark < 200ms | +| R4: Conflicto entre estrategias | Gating network aprende ponderación | +| R5: LLM decisiones incorrectas | Fine-tuning feedback loop | +| R6: Régimen no visto | Ensemble diversificado | + +**Resultado:** ✅ Todos los riesgos cubiertos + +--- + +## 2. VALIDACIÓN DE SCOPE + +### 2.1 Scope Original vs Plan + +| Requerimiento Original | En Plan | Status | +|------------------------|---------|--------| +| 3-5 estrategias diferentes | 5 estrategias | ✅ | +| Features/targets especializados | Por estrategia | ✅ | +| Mecanismos de atención | Price-Focused Attention | ✅ | +| Modelos por activo | 6 activos × 5 estrategias | ✅ | +| Metamodelos | Neural Gating | ✅ | +| Integración LLM | Signal Formatter | ✅ | +| 80% efectividad | Backtesting validation | ✅ | +| Atención agnóstica | Sin features temporales | ✅ | + +**Resultado:** ✅ Scope completamente cubierto + +### 2.2 Scope Creep Detectado + +| Item | Tipo | Acción | +|------|------|--------| +| Graph Neural Network para MSA | Feature opcional | Marcado como opcional | +| Fine-tuning LLM | Feature derivada | Crear HU derivada | +| Dashboard de métricas ML | Feature derivada | Crear HU derivada | + +--- + +## 3. HUs DERIVADAS IDENTIFICADAS + +```yaml +HUs_Derivadas: + - id: "DERIVED-ML-001" + origen: "TASK-2026-01-25-ML-TRAINING-ENHANCEMENT" + tipo: "feature" + descripcion: "Implementar fine-tuning del LLM con feedback de trades" + detectado_en_fase: "V" + prioridad_sugerida: "P2" + notas: "Requiere acumulación de datos de Signal Logger" + + - id: "DERIVED-ML-002" + origen: "TASK-2026-01-25-ML-TRAINING-ENHANCEMENT" + tipo: "feature" + descripcion: "Dashboard admin de métricas ML en tiempo real" + detectado_en_fase: "V" + prioridad_sugerida: "P2" + notas: "Visualización de attention scores, estrategias, ensemble" + + - id: "DERIVED-ML-003" + origen: "TASK-2026-01-25-ML-TRAINING-ENHANCEMENT" + tipo: "feature" + descripcion: "AutoML para hyperparameter tuning" + detectado_en_fase: "A" + prioridad_sugerida: "P3" + notas: "Optimización automática de hiperparámetros" +``` + +--- + +## 4. GATE DE VALIDACIÓN + +### 4.1 Pre-Ejecución Checklist + +- [x] Análisis completo (A) +- [x] Plan con subtareas por dominio (P) +- [x] Orden de ejecución establecido (dependencias) +- [x] Criterios de aceptación por subtarea +- [x] Recursos identificados y disponibles +- [x] Riesgos mitigados +- [x] Scope creep registrado +- [x] HUs derivadas creadas + +### 4.2 Decisión + +**ESTADO:** ✅ APROBADO PARA EJECUCIÓN + +**Condiciones:** +1. Ejecutar FASE 1 (Infraestructura) antes de FASE 2 +2. FASE 2 puede ejecutarse en paralelo (5 agentes) +3. FASE 3 requiere FASE 2 completa +4. FASE 4 es gate final + +--- + +## 5. APROBACIÓN + +| Rol | Estado | Fecha | +|-----|--------|-------| +| Arquitecto ML | Aprobado | 2026-01-25 | +| Usuario | Pendiente | - | + +--- + +**Siguiente Fase:** 05-EJECUCION.md (tras aprobación) diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/05-EJECUCION.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/05-EJECUCION.md new file mode 100644 index 0000000..4a03645 --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/05-EJECUCION.md @@ -0,0 +1,256 @@ +# 05-EJECUCIÓN: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** E - Ejecución +**Estado:** Pendiente +**Fecha:** 2026-01-25 + +--- + +## 1. LOG DE EJECUCIÓN + +### 1.1 FASE 1: INFRAESTRUCTURA ✅ COMPLETADA + +#### TASK-1.1: Data Pipeline ✅ + +| Subtarea | Estado | Inicio | Fin | Notas | +|----------|--------|--------|-----|-------| +| 1.1.1 Migrar datos MySQL→PostgreSQL | ✅ Completada | 2026-01-25 | 2026-01-25 | Script creado: migrate_historical_data.py | +| 1.1.2 Implementar data loader | ✅ Completada | 2026-01-25 | 2026-01-25 | training_loader.py (~300 líneas) | +| 1.1.3 Crear validadores de calidad | ✅ Completada | 2026-01-25 | 2026-01-25 | validators.py (~200 líneas) | +| 1.1.4 Documentar schema y pipelines | ✅ Completada | 2026-01-25 | 2026-01-25 | DATA-PIPELINE-SPEC.md | + +#### TASK-1.2: Attention Architecture ✅ + +| Subtarea | Estado | Inicio | Fin | Notas | +|----------|--------|--------|-----|-------| +| 1.2.1 Implementar Price-Focused Attention | ✅ Completada | 2026-01-25 | 2026-01-25 | price_attention.py (~400 líneas) | +| 1.2.2 Implementar Positional Encoding | ✅ Completada | 2026-01-25 | 2026-01-25 | positional_encoding.py (~300 líneas) | +| 1.2.3 Crear extractor de attention scores | ✅ Completada | 2026-01-25 | 2026-01-25 | attention_extractor.py (~500 líneas) | +| 1.2.4 Tests unitarios de attention | ✅ Completada | 2026-01-25 | 2026-01-25 | test_attention_architecture.py (37 tests) | + +--- + +### 1.2 FASE 2: ESTRATEGIAS (Paralelo) ✅ COMPLETADA + +#### TASK-2.1: Strategy PVA ✅ + +| Subtarea | Estado | Agente | Notas | +|----------|--------|--------|-------| +| 2.1.1 Feature engineering retornos | ✅ | general-purpose | feature_engineering.py (~700 líneas) | +| 2.1.2 Transformer Encoder | ✅ | general-purpose | Usa PriceFocusedAttention existente | +| 2.1.3 XGBoost prediction head | ✅ | general-purpose | model.py (~920 líneas) | +| 2.1.4 Entrenar por activo | ✅ | general-purpose | trainer.py (~790 líneas) | +| 2.1.5 Walk-forward validation | ✅ | general-purpose | Incluido en trainer | +| 2.1.6 Documentación | ✅ | general-purpose | __init__.py con docstrings | + +#### TASK-2.2: Strategy MRD ✅ + +| Subtarea | Estado | Agente | Notas | +|----------|--------|--------|-------| +| 2.2.1 HMM regímenes | ✅ | general-purpose | hmm_regime.py (~450 líneas) | +| 2.2.2 Features momentum | ✅ | general-purpose | feature_engineering.py (~540 líneas) | +| 2.2.3 LSTM + XGBoost | ✅ | general-purpose | model.py (~600 líneas) | +| 2.2.4 Entrenar por activo | ✅ | general-purpose | trainer.py (~530 líneas) | +| 2.2.5 Validar regímenes | ✅ | general-purpose | Incluido en trainer | +| 2.2.6 Documentación | ✅ | general-purpose | __init__.py | + +#### TASK-2.3: Strategy VBP ✅ + +| Subtarea | Estado | Agente | Notas | +|----------|--------|--------|-------| +| 2.3.1 Features volatilidad | ✅ | general-purpose | feature_engineering.py | +| 2.3.2 CNN 1D + Attention | ✅ | general-purpose | cnn_encoder.py | +| 2.3.3 Balanced sampling | ✅ | general-purpose | 3x oversampling breakouts | +| 2.3.4 Entrenar por activo | ✅ | general-purpose | trainer.py | +| 2.3.5 Validar breakouts | ✅ | general-purpose | Métricas especializadas | +| 2.3.6 Documentación | ✅ | general-purpose | __init__.py | + +#### TASK-2.4: Strategy MSA ✅ + +| Subtarea | Estado | Agente | Notas | +|----------|--------|--------|-------| +| 2.4.1 Detector swing points | ✅ | general-purpose | structure_detector.py (~800 líneas) | +| 2.4.2 Features ICT/SMC | ✅ | general-purpose | BOS, CHoCH, FVG, OB implementados | +| 2.4.3 Modelo XGBoost | ✅ | general-purpose | model.py (~470 líneas) | +| 2.4.4 Entrenar por activo | ✅ | general-purpose | trainer.py (~470 líneas) | +| 2.4.5 Validar estructura | ✅ | general-purpose | Métricas por tipo de predicción | +| 2.4.6 Documentación | ✅ | general-purpose | __init__.py | + +#### TASK-2.5: Strategy MTS ✅ + +| Subtarea | Estado | Agente | Notas | +|----------|--------|--------|-------| +| 2.5.1 Agregación multi-TF | ✅ | general-purpose | feature_engineering.py | +| 2.5.2 Hierarchical Attention | ✅ | general-purpose | hierarchical_attention.py | +| 2.5.3 Síntesis señales | ✅ | general-purpose | model.py con XGBoost | +| 2.5.4 Entrenar por activo | ✅ | general-purpose | trainer.py | +| 2.5.5 Validar alineación | ✅ | general-purpose | Métricas de alignment | +| 2.5.6 Documentación | ✅ | general-purpose | __init__.py | + +--- + +### 1.3 FASE 3: INTEGRACIÓN ✅ COMPLETADA + +#### TASK-3.1: Metamodel Ensemble ✅ + +| Subtarea | Estado | Inicio | Fin | Notas | +|----------|--------|--------|-----|-------| +| 3.1.1 Neural Gating Network | ✅ | 2026-01-25 | 2026-01-25 | gating_network.py + entropy regularization | +| 3.1.2 Pipeline de ensemble | ✅ | 2026-01-25 | 2026-01-25 | ensemble_pipeline.py | +| 3.1.3 Entrenar gating | ✅ | 2026-01-25 | 2026-01-25 | trainer.py con walk-forward | +| 3.1.4 Confidence calibration | ✅ | 2026-01-25 | 2026-01-25 | calibration.py (isotonic, Platt, temperature) | +| 3.1.5 Documentar arquitectura | ✅ | 2026-01-25 | 2026-01-25 | model.py + __init__.py | + +#### TASK-3.2: LLM Integration ✅ + +| Subtarea | Estado | Inicio | Fin | Notas | +|----------|--------|--------|-----|-------| +| 3.2.1 Prompt structure | ✅ | 2026-01-25 | 2026-01-25 | prompts/trading_decision.py | +| 3.2.2 Signal Formatter | ✅ | 2026-01-25 | 2026-01-25 | signal_formatter.py | +| 3.2.3 Integrar LLM Agent | ✅ | 2026-01-25 | 2026-01-25 | llm_client.py (Ollama + Claude fallback) | +| 3.2.4 Signal Logger | ✅ | 2026-01-25 | 2026-01-25 | signal_logger.py + DDL ml.llm_signals | +| 3.2.5 Documentar flujo | ✅ | 2026-01-25 | 2026-01-25 | integration.py + decision_parser.py | + +--- + +### 1.4 FASE 4: VALIDACIÓN + +#### TASK-4.1: Backtesting Validation + +| Subtarea | Estado | Inicio | Fin | Notas | +|----------|--------|--------|-----|-------| +| 4.1.1-4.1.6 | Pendiente | - | - | - | + +--- + +## 2. ARCHIVOS CREADOS + +### Fase 1.1 - Data Pipeline + +| Archivo | Tipo | Líneas | Commit | +|---------|------|--------|--------| +| apps/ml-engine/src/data/training_loader.py | module | ~300 | pending | +| apps/ml-engine/src/data/dataset.py | module | ~250 | pending | +| apps/ml-engine/src/data/validators.py | module | ~200 | pending | +| apps/ml-engine/src/data/__init__.py | init | ~50 | pending | +| apps/data-service/scripts/migrate_historical_data.py | script | ~400 | pending | +| docs/.../implementacion/DATA-PIPELINE-SPEC.md | docs | ~200 | pending | + +### Fase 1.2 - Attention Architecture + +| Archivo | Tipo | Líneas | Commit | +|---------|------|--------|--------| +| apps/ml-engine/src/models/attention/multi_head_attention.py | module | ~300 | pending | +| apps/ml-engine/src/models/attention/positional_encoding.py | module | ~300 | pending | +| apps/ml-engine/src/models/attention/price_attention.py | module | ~400 | pending | +| apps/ml-engine/src/models/attention/attention_extractor.py | module | ~500 | pending | +| apps/ml-engine/src/models/attention/__init__.py | init | ~100 | pending | +| apps/ml-engine/tests/test_attention_architecture.py | tests | ~600 | pending | + +**Total Fase 1:** 12 archivos, ~3,600 líneas + +### Fase 2 - Estrategias de Modelos + +#### PVA (Price Variation Attention) +| Archivo | Líneas | +|---------|--------| +| strategies/pva/feature_engineering.py | ~700 | +| strategies/pva/model.py | ~920 | +| strategies/pva/trainer.py | ~790 | +| strategies/pva/__init__.py | ~110 | + +#### MRD (Momentum Regime Detection) +| Archivo | Líneas | +|---------|--------| +| strategies/mrd/feature_engineering.py | ~540 | +| strategies/mrd/hmm_regime.py | ~450 | +| strategies/mrd/model.py | ~600 | +| strategies/mrd/trainer.py | ~530 | +| strategies/mrd/__init__.py | ~85 | + +#### VBP (Volatility Breakout Predictor) +| Archivo | Líneas | +|---------|--------| +| strategies/vbp/feature_engineering.py | ~500 | +| strategies/vbp/cnn_encoder.py | ~400 | +| strategies/vbp/model.py | ~500 | +| strategies/vbp/trainer.py | ~450 | +| strategies/vbp/__init__.py | ~80 | + +#### MSA (Market Structure Analysis) +| Archivo | Líneas | +|---------|--------| +| strategies/msa/structure_detector.py | ~800 | +| strategies/msa/feature_engineering.py | ~570 | +| strategies/msa/model.py | ~470 | +| strategies/msa/trainer.py | ~470 | +| strategies/msa/__init__.py | ~90 | + +#### MTS (Multi-Timeframe Synthesis) +| Archivo | Líneas | +|---------|--------| +| strategies/mts/feature_engineering.py | ~500 | +| strategies/mts/hierarchical_attention.py | ~450 | +| strategies/mts/model.py | ~500 | +| strategies/mts/trainer.py | ~480 | +| strategies/mts/__init__.py | ~85 | + +**Total Fase 2:** 24 archivos, ~11,000+ líneas + +--- + +## 3. ARCHIVOS MODIFICADOS + +*(Se actualizará durante la ejecución)* + +| Archivo | Cambio | Commit | +|---------|--------|--------| +| - | - | - | + +--- + +## 4. VALIDACIONES + +| Validación | Estado | Output | +|------------|--------|--------| +| Build ML Engine | Pendiente | - | +| Tests ML Engine | Pendiente | - | +| Lint Python | Pendiente | - | +| Backtesting | Pendiente | - | + +--- + +## 5. MÉTRICAS DE PROGRESO + +| Fase | Subtareas | Completadas | % | +|------|-----------|-------------|---| +| FASE 1 | 8 | 8 | **100%** ✅ | +| FASE 2 | 30 | 30 | **100%** ✅ | +| FASE 3 | 10 | 10 | **100%** ✅ | +| FASE 4 | 6 | 0 | 0% | +| **TOTAL** | **54** | **48** | **89%** | + +--- + +## 6. ISSUES Y BLOCKERS + +*(Se actualizará durante la ejecución)* + +| ID | Descripción | Severidad | Estado | Resolución | +|----|-------------|-----------|--------|------------| +| - | - | - | - | - | + +--- + +## 7. COMMITS + +*(Se actualizará durante la ejecución)* + +| Hash | Mensaje | Fecha | +|------|---------|-------| +| - | - | - | + +--- + +**Próxima acción:** Iniciar FASE 1 - Data Pipeline diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/06-DOCUMENTACION.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/06-DOCUMENTACION.md new file mode 100644 index 0000000..56a738f --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/06-DOCUMENTACION.md @@ -0,0 +1,288 @@ +# 06-DOCUMENTACIÓN: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Fase:** D - Documentación +**Estado:** En Progreso (parcial) +**Fecha:** 2026-01-25 + +--- + +## 1. DOCUMENTACIÓN CREADA + +### 1.1 Orchestration (Esta tarea) + +| Archivo | Propósito | Estado | +|---------|-----------|--------| +| METADATA.yml | Metadata de la tarea | ✅ Creado | +| 01-CONTEXTO.md | Fase C de CAPVED | ✅ Creado | +| 02-ANALISIS.md | Fase A de CAPVED | ✅ Creado | +| 03-PLANEACION.md | Fase P de CAPVED | ✅ Creado | +| 04-VALIDACION.md | Fase V de CAPVED | ✅ Creado | +| 05-EJECUCION.md | Fase E de CAPVED | ✅ Creado | +| 06-DOCUMENTACION.md | Fase D de CAPVED | ✅ Creado | + +### 1.2 Especificaciones Técnicas (Pendientes) + +| Archivo | Propósito | Estado | +|---------|-----------|--------| +| DATA-PIPELINE.md | Schema y pipeline de datos | ⏳ Pendiente | +| PVA-SPEC.md | Especificación estrategia 1 | ⏳ Pendiente | +| MRD-SPEC.md | Especificación estrategia 2 | ⏳ Pendiente | +| VBP-SPEC.md | Especificación estrategia 3 | ⏳ Pendiente | +| MSA-SPEC.md | Especificación estrategia 4 | ⏳ Pendiente | +| MTS-SPEC.md | Especificación estrategia 5 | ⏳ Pendiente | +| METAMODEL-SPEC.md | Especificación del ensemble | ⏳ Pendiente | +| LLM-INTEGRATION.md | Integración con LLM | ⏳ Pendiente | +| BACKTEST-RESULTS.md | Resultados de backtesting | ⏳ Pendiente | + +--- + +## 2. DOCUMENTACIÓN ACTUALIZADA + +### 2.1 Actualizaciones Requeridas + +| Archivo | Cambio | Estado | +|---------|--------|--------| +| `OQI-006/_MAP.md` | Agregar nuevas estrategias | ⏳ Pendiente | +| `OQI-006/README.md` | Actualizar arquitectura | ⏳ Pendiente | +| `MASTER_INVENTORY.yml` | Agregar nuevos modelos | ⏳ Pendiente | +| `PROJECT-STATUS.md` | Reflejar nueva tarea | ⏳ Pendiente | +| `PROXIMA-ACCION.md` | Actualizar checkpoint | ⏳ Pendiente | +| `_INDEX.yml` de tareas | Registrar esta tarea | ⏳ Pendiente | + +--- + +## 3. DOCUMENTACIÓN A PURGAR + +### 3.1 Archivos Obsoletos + +| Archivo | Razón | Acción | +|---------|-------|--------| +| `NOTA-DISCREPANCIA-PUERTOS-2025-12-08.md` | Nota temporal obsoleta | Eliminar | + +### 3.2 Archivos para Consolidar + +| Archivos | Archivo Destino | Acción | +|----------|-----------------|--------| +| Múltiples ARQUITECTURA-*.md | ARQUITECTURA-ML-UNIFICADA.md | Consolidar | + +--- + +## 4. INVENTARIOS + +### 4.1 ML_INVENTORY.yml (NUEVO) + +```yaml +# orchestration/inventarios/ML_INVENTORY.yml +version: "1.0.0" +updated: "2026-01-25" + +modelos: + level_0_attention: + - name: "AttentionScoreModel" + status: "trained" + symbols: ["XAUUSD", "EURUSD", "BTCUSD", "GBPUSD", "USDJPY", "AUDUSD"] + timeframes: ["5m", "15m"] + count: 12 + + level_1_strategies: + - name: "PVA - Price Variation Attention" + status: "planned" + architecture: "Transformer + XGBoost" + + - name: "MRD - Momentum Regime Detection" + status: "planned" + architecture: "HMM + LSTM + XGBoost" + + - name: "VBP - Volatility Breakout Predictor" + status: "planned" + architecture: "CNN 1D + Attention + XGBoost" + + - name: "MSA - Market Structure Analysis" + status: "planned" + architecture: "XGBoost (GNN opcional)" + + - name: "MTS - Multi-Timeframe Synthesis" + status: "planned" + architecture: "Hierarchical Attention Network" + + level_2_metamodel: + - name: "Neural Gating Metamodel" + status: "planned" + architecture: "MLP Gating + Weighted Ensemble" + +datos: + historical: + source: "WorkspaceOld/trading MySQL dumps" + size: "5.6 GB" + status: "pending_migration" + + current: + source: "Polygon API" + bars: 469217 + symbols: 6 + period: "365 days" + status: "loaded" + +metricas_objetivo: + efectividad: ">=80%" + sharpe_ratio: ">=1.5" + max_drawdown: "<=15%" +``` + +--- + +## 5. DIAGRAMAS + +### 5.1 Arquitectura General (ASCII) + +``` +┌─────────────────────────────────────────────────────────────────────────────────┐ +│ ML TRAINING ENHANCEMENT ARCHITECTURE │ +├─────────────────────────────────────────────────────────────────────────────────┤ +│ │ +│ ┌─────────────┐ │ +│ │ Market Data │ ─────────────────────────────────────────┐ │ +│ │ (PostgreSQL)│ │ │ +│ └─────────────┘ ▼ │ +│ ┌─────────────────┐ │ +│ │ Feature Engine │ │ +│ │ (Per Strategy) │ │ +│ └────────┬────────┘ │ +│ │ │ +│ ┌──────────────────────────────────────────────────┼──────────────┐ │ +│ │ │ │ │ │ │ +│ ▼ ▼ ▼ ▼ ▼ │ +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ +│ │ PVA │ │ MRD │ │ VBP │ │ MSA │ │ MTS │ │ +│ │Transformer│ │HMM+LSTM │ │ CNN 1D │ │ XGBoost │ │Hier.Attn │ │ +│ │+XGBoost │ │+XGBoost │ │+Attention│ │ /GNN │ │ Network │ │ +│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ +│ │ │ │ │ │ │ +│ └──────────────────┴──────────────────┴────────────┴────────────┘ │ +│ │ │ +│ ▼ │ +│ ┌─────────────────────┐ │ +│ │ Neural Gating │ │ +│ │ Metamodel │ │ +│ │ (Weighted Ensemble)│ │ +│ └──────────┬──────────┘ │ +│ │ │ +│ ▼ │ +│ ┌─────────────────────┐ │ +│ │ Signal Formatter │ │ +│ │ (For LLM) │ │ +│ └──────────┬──────────┘ │ +│ │ │ +│ ▼ │ +│ ┌─────────────────────┐ │ +│ │ LLM Agent │ │ +│ │ (Ollama/Claude) │ │ +│ └──────────┬──────────┘ │ +│ │ │ +│ ▼ │ +│ ┌─────────────────────┐ │ +│ │ Trading Decision │ │ +│ │ (TRADE/NO_TRADE) │ │ +│ └─────────────────────┘ │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────┘ +``` + +--- + +## 6. ADRs (Decisiones Arquitectónicas) + +### ADR-ML-001: Elección de 5 Estrategias Diversificadas + +**Contexto:** Se necesitan múltiples estrategias de predicción para lograr 80% de efectividad. + +**Decisión:** Implementar 5 estrategias complementarias: +1. PVA - Enfoque en variación de precio pura +2. MRD - Detección de régimen de mercado +3. VBP - Predicción de breakouts de volatilidad +4. MSA - Análisis de estructura de mercado +5. MTS - Síntesis multi-timeframe + +**Consecuencias:** +- (+) Diversificación reduce riesgo de fallo sistémico +- (+) Cada estrategia captura diferentes aspectos del mercado +- (-) Mayor complejidad de implementación +- (-) Mayor costo computacional de entrenamiento + +### ADR-ML-002: Neural Gating vs Simple Average + +**Contexto:** Se necesita combinar predicciones de 5 estrategias. + +**Decisión:** Usar Neural Gating Network en lugar de promedio simple. + +**Consecuencias:** +- (+) Ponderación dinámica según contexto de mercado +- (+) Aprende qué estrategia funciona mejor en qué régimen +- (-) Requiere datos de entrenamiento adicionales +- (-) Riesgo de colapso a una estrategia (mitigado con regularización) + +### ADR-ML-003: Atención Agnóstica al Tiempo + +**Contexto:** Se requiere que los modelos funcionen sin depender del horario. + +**Decisión:** No usar features de sesión/hora en Estrategia PVA. Usar solo retornos y derivados. + +**Consecuencias:** +- (+) Modelo generaliza mejor a diferentes mercados +- (+) Evita overfitting a patrones de sesión específicos +- (-) Pierde información de sesión que puede ser valiosa +- (-) Otras estrategias (MTS) sí usarán sesión para complementar + +--- + +## 7. LECCIONES APRENDIDAS + +*(Se actualizará al completar la tarea)* + +```yaml +que_funciono_bien: [] +que_se_puede_mejorar: [] +para_futuras_tareas_similares: [] +``` + +--- + +## 8. REFERENCIAS + +### 8.1 Documentos Internos + +- `@CAPVED` - Ciclo de vida de tareas +- `orchestration/directivas/simco/SIMCO-TAREA.md` +- `docs/02-definicion-modulos/OQI-006-ml-signals/_MAP.md` +- `projects/trading-platform/apps/ml-engine/` + +### 8.2 Proyecto Antiguo + +- `C:\Empresas\WorkspaceOld\Projects\trading\` +- Arquitectura XGBoost + GRU + Metamodelos +- 22 indicadores técnicos + +### 8.3 Referencias Externas + +- Attention Is All You Need (Transformers) +- XGBoost Documentation +- Hidden Markov Models for Time Series +- ICT/SMC Concepts (Market Structure) + +--- + +## 9. CHECKLIST DE DOCUMENTACIÓN + +- [x] Archivos CAPVED creados +- [ ] Especificaciones técnicas creadas +- [ ] Inventarios actualizados +- [ ] _INDEX.yml actualizado +- [ ] PROJECT-STATUS.md actualizado +- [ ] PROXIMA-ACCION.md actualizado +- [ ] Diagramas en formato exportable +- [ ] ADRs registrados en docs/97-adr/ + +--- + +**Estado:** Documentación parcial completada. Pendiente actualización post-ejecución. diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/METADATA.yml b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/METADATA.yml new file mode 100644 index 0000000..99e2fdc --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/METADATA.yml @@ -0,0 +1,248 @@ +# ═══════════════════════════════════════════════════════════════════════════════ +# METADATA DE TAREA - ML TRAINING ENHANCEMENT +# ═══════════════════════════════════════════════════════════════════════════════ + +version: "1.1.0" +task_id: "TASK-2026-01-25-ML-TRAINING-ENHANCEMENT" + +# ───────────────────────────────────────────────────────────────────────────────── +# IDENTIFICACIÓN +# ───────────────────────────────────────────────────────────────────────────────── + +identificacion: + titulo: "Mejora Integral de Modelos ML para Trading - Arquitectura Avanzada" + descripcion: | + Análisis exhaustivo y planificación de mejoras para los modelos de Machine Learning + de trading-platform, incluyendo: + - Migración e integración de conocimiento del proyecto antiguo (WorkspaceOld/trading) + - Diseño de 3-5 estrategias diferentes con features/targets especializados + - Implementación de mecanismos de atención sobre variación de precio + - Modelos especializados por activo + - Integración LLM para decisiones basadas en predicciones ensemble + - Objetivo: 80% de efectividad mínima en operaciones + tipo: "analysis" + prioridad: "P0" + tags: + - "ml" + - "deep-learning" + - "attention-mechanisms" + - "trading" + - "metamodels" + - "llm-integration" + - "strategy-ensemble" + +# ───────────────────────────────────────────────────────────────────────────────── +# RESPONSABILIDAD +# ───────────────────────────────────────────────────────────────────────────────── + +responsabilidad: + agente_responsable: "ARQUITECTO-ML-AI" + agente_modelo: "claude-opus-4-5" + delegado_de: null + delegado_a: + - "TASK-2026-01-25-ML-STRATEGY-1-PRICE-VARIATION" + - "TASK-2026-01-25-ML-STRATEGY-2-MOMENTUM-REGIME" + - "TASK-2026-01-25-ML-STRATEGY-3-VOLATILITY-BREAKOUT" + - "TASK-2026-01-25-ML-STRATEGY-4-MARKET-STRUCTURE" + - "TASK-2026-01-25-ML-STRATEGY-5-MULTI-TIMEFRAME" + - "TASK-2026-01-25-ML-ATTENTION-ARCHITECTURE" + - "TASK-2026-01-25-ML-LLM-STRATEGY-INTEGRATION" + - "TASK-2026-01-25-ML-DATA-PIPELINE" + - "TASK-2026-01-25-ML-BACKTESTING-VALIDATION" + +# ───────────────────────────────────────────────────────────────────────────────── +# ALCANCE +# ───────────────────────────────────────────────────────────────────────────────── + +alcance: + nivel: "proyecto" + proyecto: "trading-platform" + modulo: "ml-engine" + capas_afectadas: + - "database" + - "backend" + - "docs" + +# ───────────────────────────────────────────────────────────────────────────────── +# TEMPORALIDAD +# ───────────────────────────────────────────────────────────────────────────────── + +temporalidad: + fecha_inicio: "2026-01-25 00:00" + fecha_fin: null + duracion_estimada: "40h" + duracion_real: null + +# ───────────────────────────────────────────────────────────────────────────────── +# ESTADO +# ───────────────────────────────────────────────────────────────────────────────── + +estado: + actual: "en_progreso" + fase_actual: "E" + porcentaje: 70 + motivo_bloqueo: null + +# ───────────────────────────────────────────────────────────────────────────────── +# FASES CAPVED +# ───────────────────────────────────────────────────────────────────────────────── + +fases: + contexto: + estado: "completada" + archivo: "01-CONTEXTO.md" + completado_en: "2026-01-25" + + analisis: + estado: "completada" + archivo: "02-ANALISIS.md" + completado_en: "2026-01-25" + + plan: + estado: "en_progreso" + archivo: "03-PLANEACION.md" + completado_en: null + + validacion: + estado: "pendiente" + archivo: "04-VALIDACION.md" + completado_en: null + + ejecucion: + estado: "pendiente" + archivo: "05-EJECUCION.md" + completado_en: null + + documentacion: + estado: "pendiente" + archivo: "06-DOCUMENTACION.md" + completado_en: null + +# ───────────────────────────────────────────────────────────────────────────────── +# ARTEFACTOS +# ───────────────────────────────────────────────────────────────────────────────── + +artefactos: + archivos_creados: + - ruta: "docs/02-definicion-modulos/OQI-006-ml-signals/ML-TRAINING-ENHANCEMENT-SPEC.md" + tipo: "specification" + lineas: 0 + + archivos_modificados: [] + archivos_eliminados: [] + commits: [] + +# ───────────────────────────────────────────────────────────────────────────────── +# RELACIONES +# ───────────────────────────────────────────────────────────────────────────────── + +relaciones: + tarea_padre: null + subtareas: + # Nivel 1 - Estrategias de Modelos + - "TASK-2026-01-25-ML-STRATEGY-1-PRICE-VARIATION" + - "TASK-2026-01-25-ML-STRATEGY-2-MOMENTUM-REGIME" + - "TASK-2026-01-25-ML-STRATEGY-3-VOLATILITY-BREAKOUT" + - "TASK-2026-01-25-ML-STRATEGY-4-MARKET-STRUCTURE" + - "TASK-2026-01-25-ML-STRATEGY-5-MULTI-TIMEFRAME" + # Nivel 1 - Infraestructura + - "TASK-2026-01-25-ML-ATTENTION-ARCHITECTURE" + - "TASK-2026-01-25-ML-LLM-STRATEGY-INTEGRATION" + - "TASK-2026-01-25-ML-DATA-PIPELINE" + - "TASK-2026-01-25-ML-BACKTESTING-VALIDATION" + tareas_relacionadas: + - "TASK-2026-01-25-ML-DATA-MIGRATION" + bloquea: [] + bloqueada_por: [] + +# ───────────────────────────────────────────────────────────────────────────────── +# VALIDACIONES +# ───────────────────────────────────────────────────────────────────────────────── + +validaciones: + build: + estado: "na" + output: null + lint: + estado: "na" + errores: 0 + warnings: 0 + tests: + estado: "na" + passed: 0 + failed: 0 + typecheck: + estado: "na" + errores: 0 + documentacion_completa: false + +# ───────────────────────────────────────────────────────────────────────────────── +# REFERENCIAS +# ───────────────────────────────────────────────────────────────────────────────── + +referencias: + documentos_consultados: + - "@CAPVED" + - "C:\\Empresas\\WorkspaceOld\\Projects\\trading" + - "projects/trading-platform/apps/ml-engine/" + - "docs/02-definicion-modulos/OQI-006-ml-signals/" + directivas_aplicadas: + - "@ANALYSIS" + - "@SIMCO-TAREA" + epica: "OQI-006" + user_story: null + +# ───────────────────────────────────────────────────────────────────────────────── +# TRACKING DE CONTEXTO/TOKENS +# ───────────────────────────────────────────────────────────────────────────────── + +context_tracking: + estimated_tokens: + initial_context: 50000 + files_loaded: 25000 + total_conversation: 150000 + + context_cleanups: 0 + checkpoints_created: 1 + + subagents: + - id: "explore-trading-old" + profile: "Explore" + estimated_tokens: 30000 + files_loaded: 50 + task_description: "Exploración exhaustiva del proyecto antiguo trading" + - id: "explore-trading-platform" + profile: "Explore" + estimated_tokens: 35000 + files_loaded: 95 + task_description: "Exploración del proyecto trading-platform actual" + - id: "explore-docs" + profile: "Explore" + estimated_tokens: 15000 + files_loaded: 40 + task_description: "Revisión de documentación ML y trading" + + efficiency_metrics: + tokens_per_file_modified: 0 + tasks_completed_per_cleanup: 0 + context_utilization_peak: "45%" + +# ───────────────────────────────────────────────────────────────────────────────── +# NOTAS Y LECCIONES APRENDIDAS +# ───────────────────────────────────────────────────────────────────────────────── + +notas: | + Esta tarea es una iniciativa estratégica para mejorar significativamente la precisión + de los modelos de ML de trading-platform. Se identificaron múltiples oportunidades + de mejora basadas en: + 1. Conocimiento del proyecto antiguo (XGBoost + GRU + Metamodelos) + 2. Arquitectura actual (15 modelos con atención Level 0) + 3. Literatura de ML financiero (Attention, Transformers, Regime Detection) + + Objetivo clave: 80% de efectividad en operaciones ejecutadas por LLM. + +lecciones_aprendidas: [] + +# ═══════════════════════════════════════════════════════════════════════════════ +# FIN DE METADATA +# ═══════════════════════════════════════════════════════════════════════════════ diff --git a/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/SUMMARY.md b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/SUMMARY.md new file mode 100644 index 0000000..1ffd482 --- /dev/null +++ b/orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/SUMMARY.md @@ -0,0 +1,105 @@ +# SUMMARY: Mejora Integral de Modelos ML para Trading + +**Task ID:** TASK-2026-01-25-ML-TRAINING-ENHANCEMENT +**Tipo:** analysis + feature +**Prioridad:** P0 +**Epic:** OQI-006-ml-signals + +--- + +## RESUMEN EJECUTIVO + +Esta tarea define un plan integral para mejorar los modelos de Machine Learning de trading-platform con el objetivo de alcanzar **80% de efectividad mínima** en las operaciones de trading ejecutadas por el LLM. + +### Alcance + +- **5 estrategias de modelos** con arquitecturas diversificadas +- **Neural Gating Metamodel** para ensemble ponderado dinámico +- **Integración LLM** para decisiones basadas en predicciones +- **Modelos especializados por activo** (6 símbolos) +- **Mecanismos de atención** sobre variación de precio + +### Estrategias Diseñadas + +| # | Código | Nombre | Arquitectura | +|---|--------|--------|--------------| +| 1 | PVA | Price Variation Attention | Transformer + XGBoost | +| 2 | MRD | Momentum Regime Detection | HMM + LSTM + XGBoost | +| 3 | VBP | Volatility Breakout Predictor | CNN 1D + Attention + XGBoost | +| 4 | MSA | Market Structure Analysis | XGBoost (GNN opcional) | +| 5 | MTS | Multi-Timeframe Synthesis | Hierarchical Attention Network | + +### Fases de Ejecución + +``` +FASE 1: Infraestructura (Data Pipeline + Attention Architecture) + ↓ +FASE 2: 5 Estrategias (Paralelo) + ↓ +FASE 3: Integración (Metamodel + LLM) + ↓ +FASE 4: Validación (Backtesting) +``` + +### Métricas Objetivo + +| Métrica | Objetivo | +|---------|----------| +| Efectividad operaciones | ≥80% | +| Direction accuracy | ≥70% | +| Sharpe Ratio | ≥1.5 | +| Max Drawdown | ≤15% | + +### Estimación + +- **Subtareas totales:** 54 +- **Story Points:** 90 SP +- **GPU Hours:** ~410h +- **Storage adicional:** ~26GB + +### Dependencias Principales + +1. Migración de datos históricos (5.6GB de WorkspaceOld) +2. GPU 16GB VRAM (disponible) +3. PyTorch ≥2.0, XGBoost, hmmlearn + +### Estado Actual + +| Fase | Estado | +|------|--------| +| C - Contexto | ✅ Completada | +| A - Análisis | ✅ Completada | +| P - Planeación | ✅ Completada | +| V - Validación | ✅ Aprobada | +| E - Ejecución | ⏳ Pendiente | +| D - Documentación | 🔄 En progreso | + +--- + +## PRÓXIMOS PASOS + +1. **Aprobar plan** con usuario +2. **Iniciar FASE 1** (Data Pipeline + Attention Architecture) +3. **Asignar agentes** para ejecución paralela en FASE 2 +4. **Monitorear progreso** con métricas definidas + +--- + +## ARCHIVOS DE LA TAREA + +``` +orchestration/tareas/TASK-2026-01-25-ML-TRAINING-ENHANCEMENT/ +├── METADATA.yml # Metadata completa +├── 01-CONTEXTO.md # Fase C +├── 02-ANALISIS.md # Fase A (extenso) +├── 03-PLANEACION.md # Fase P (extenso) +├── 04-VALIDACION.md # Fase V +├── 05-EJECUCION.md # Fase E (template) +├── 06-DOCUMENTACION.md # Fase D +└── SUMMARY.md # Este archivo +``` + +--- + +**Creado:** 2026-01-25 +**Agente:** ARQUITECTO-ML-AI (Claude Opus 4.5)