# πŸ—οΈ Architecture DΓ©taillΓ©e - Trading AI Secure ## πŸ“‹ Table des MatiΓ¨res 1. [Vue d'ensemble](#vue-densemble) 2. [Architecture Globale](#architecture-globale) 3. [Modules Core](#modules-core) 4. [Flux de DonnΓ©es](#flux-de-donnΓ©es) 5. [Patterns et Principes](#patterns-et-principes) 6. [SΓ©curitΓ©](#sΓ©curitΓ©) 7. [ScalabilitΓ©](#scalabilitΓ©) --- ## 🎯 Vue d'ensemble ### Principes Architecturaux 1. **Separation of Concerns** : Chaque module a une responsabilitΓ© unique 2. **Dependency Injection** : Facilite tests et modularitΓ© 3. **Event-Driven** : Communication asynchrone entre composants 4. **Fail-Safe** : DΓ©gradation gracieuse en cas d'erreur 5. **Observable** : Monitoring et logging Γ  tous les niveaux ### Stack Technologique ```yaml Backend: Language: Python 3.11+ Framework: FastAPI Async: asyncio + threading Data: Storage: PostgreSQL (positions, trades) Cache: Redis (market data, signals) Time-Series: InfluxDB (mΓ©triques) ML/AI: Core: scikit-learn, XGBoost, LightGBM Optimization: Optuna Deep Learning: TensorFlow/PyTorch (optionnel) Monitoring: Metrics: Prometheus Visualization: Grafana Logging: ELK Stack (Elasticsearch, Logstash, Kibana) UI: Dashboard: Streamlit API Docs: Swagger/OpenAPI ``` --- ## πŸ›οΈ Architecture Globale ### Diagramme de Haut Niveau ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ TRADING AI SECURE β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ UI LAYER β”‚ β”‚ API LAYER β”‚ β”‚ MONITORING β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Streamlit β”‚ β”‚ FastAPI β”‚ β”‚ Prometheus β”‚ β”‚ β”‚ β”‚ Dashboard β”‚ β”‚ REST API β”‚ β”‚ Grafana β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ ORCHESTRATION LAYER β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ Strategy β”‚ β”‚ Risk β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Engine β”‚ β”‚ Manager β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ (Singleton) β”‚ β”‚ (Singleton) β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ CORE SERVICES β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ Data β”‚ β”‚ ML β”‚ β”‚ Order β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Service β”‚ β”‚ Engine β”‚ β”‚ Manager β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ DATA & INTEGRATION LAYER β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ Market β”‚ β”‚ IG β”‚ β”‚ Cache β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Data β”‚ β”‚ API β”‚ β”‚ (Redis) β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Sources β”‚ β”‚Connector β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ PERSISTENCE LAYER β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚PostgreSQLβ”‚ β”‚ InfluxDB β”‚ β”‚ File β”‚ β”‚ β”‚ β”‚ β”‚ β”‚(Trades, β”‚ β”‚(Metrics, β”‚ β”‚ System β”‚ β”‚ β”‚ β”‚ β”‚ β”‚Positions)β”‚ β”‚TimeSeriesβ”‚ β”‚ (Logs) β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ”§ Modules Core ### 1. Strategy Engine **ResponsabilitΓ©** : Orchestration des stratΓ©gies de trading ```python # src/core/strategy_engine.py class StrategyEngine: """ Moteur central de gestion des stratΓ©gies ResponsabilitΓ©s: - Charger et initialiser stratΓ©gies - Distribuer donnΓ©es marchΓ© - Collecter signaux - Coordonner exΓ©cution """ def __init__(self): self.strategies: Dict[str, BaseStrategy] = {} self.active_signals: List[Signal] = [] self.risk_manager = RiskManager() self.order_manager = OrderManager() async def run(self): """Boucle principale""" while True: # 1. RΓ©cupΓ©rer donnΓ©es marchΓ© market_data = await self.fetch_market_data() # 2. Analyser avec chaque stratΓ©gie signals = await self.analyze_strategies(market_data) # 3. Filtrer avec risk manager valid_signals = self.risk_manager.filter_signals(signals) # 4. ExΓ©cuter signaux valides await self.execute_signals(valid_signals) # 5. Mettre Γ  jour positions await self.update_positions() # 6. Sleep jusqu'Γ  prochaine itΓ©ration await asyncio.sleep(self.interval) ``` ### 2. Risk Manager (Singleton) **ResponsabilitΓ©** : Gestion centralisΓ©e du risque ```python # src/core/risk_manager.py class RiskManager: """ Singleton Risk Manager Garantit: - Une seule instance - Γ‰tat global cohΓ©rent - Thread-safe """ _instance = None _lock = threading.Lock() def __new__(cls): if cls._instance is None: with cls._lock: if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance def __init__(self): if not hasattr(self, 'initialized'): self.initialized = True self.positions = {} self.portfolio_value = 0.0 self.peak_value = 0.0 # ... autres attributs ``` ### 3. Data Service **ResponsabilitΓ©** : Abstraction des sources de donnΓ©es ```python # src/data/data_service.py class DataService: """ Service unifiΓ© d'accΓ¨s aux donnΓ©es Features: - Multi-source avec failover - Cache intelligent - Validation donnΓ©es - Rate limiting """ def __init__(self): self.sources = self._initialize_sources() self.cache = RedisCache() self.validator = DataValidator() async def get_market_data( self, symbol: str, timeframe: str, start: datetime, end: datetime ) -> pd.DataFrame: """ RΓ©cupΓ¨re donnΓ©es marchΓ© avec failover """ # 1. Check cache cached = await self.cache.get(symbol, timeframe, start, end) if cached: return cached # 2. Essayer sources par prioritΓ© for source in self.sources: try: data = await source.fetch(symbol, timeframe, start, end) # 3. Valider if self.validator.validate(data): # 4. Cache await self.cache.set(symbol, timeframe, data) return data except Exception as e: logger.warning(f"Source {source.name} failed: {e}") continue raise DataUnavailableError("All sources failed") ``` ### 4. ML Engine **ResponsabilitΓ©** : Intelligence artificielle adaptative ```python # src/ml/ml_engine.py class MLEngine: """ Moteur ML adaptatif Features: - Ensemble de modΓ¨les - Auto-retraining - Parameter optimization - Regime detection """ def __init__(self): self.models = self._initialize_models() self.optimizer = OptunaOptimizer() self.regime_detector = RegimeDetector() async def predict(self, features: pd.DataFrame) -> Dict: """ PrΓ©diction avec ensemble """ # 1. DΓ©tecter rΓ©gime regime = self.regime_detector.detect(features) # 2. SΓ©lectionner modΓ¨les selon rΓ©gime active_models = self._select_models(regime) # 3. PrΓ©dictions individuelles predictions = [] for model in active_models: pred = model.predict(features) predictions.append(pred) # 4. AgrΓ©gation (stacking) final_prediction = self._aggregate(predictions) return { 'prediction': final_prediction, 'confidence': self._calculate_confidence(predictions), 'regime': regime } async def optimize_daily(self): """ Optimisation quotidienne des paramΓ¨tres """ # 1. RΓ©cupΓ©rer performance rΓ©cente recent_performance = self._get_recent_performance() # 2. DΓ©tecter drift if self._detect_drift(recent_performance): # 3. Lancer optimisation Optuna new_params = await self.optimizer.optimize() # 4. Valider avec backtesting if self._validate_params(new_params): # 5. Appliquer nouveaux paramΓ¨tres self._update_parameters(new_params) ``` --- ## πŸ”„ Flux de DonnΓ©es ### Flux Principal (Trading Loop) ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ TRADING LOOP (60s) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ 1. FETCH MARKET DATA β”‚ β”‚ β”œβ”€ Check cache β”‚ β”‚ β”œβ”€ Fetch from sources (failover) β”‚ β”‚ └─ Validate & store β”‚ β”‚ β”‚ β”‚ 2. ANALYZE STRATEGIES β”‚ β”‚ β”œβ”€ Calculate indicators β”‚ β”‚ β”œβ”€ ML predictions β”‚ β”‚ β”œβ”€ Generate signals β”‚ β”‚ └─ Calculate confidence β”‚ β”‚ β”‚ β”‚ 3. RISK VALIDATION β”‚ β”‚ β”œβ”€ Check portfolio risk β”‚ β”‚ β”œβ”€ Validate position size β”‚ β”‚ β”œβ”€ Check correlation β”‚ β”‚ β”œβ”€ Verify margin β”‚ β”‚ └─ Circuit breakers β”‚ β”‚ β”‚ β”‚ 4. ORDER EXECUTION β”‚ β”‚ β”œβ”€ Place orders (IG API) β”‚ β”‚ β”œβ”€ Confirm execution β”‚ β”‚ └─ Update positions β”‚ β”‚ β”‚ β”‚ 5. MONITORING β”‚ β”‚ β”œβ”€ Update metrics β”‚ β”‚ β”œβ”€ Check alerts β”‚ β”‚ └─ Log events β”‚ β”‚ β”‚ β”‚ 6. SLEEP (until next iteration) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Flux Optimisation (Daily) ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ OPTIMIZATION LOOP (Daily 00:00) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ 1. COLLECT PERFORMANCE DATA β”‚ β”‚ β”œβ”€ Last 30 days trades β”‚ β”‚ β”œβ”€ Calculate metrics β”‚ β”‚ └─ Detect drift β”‚ β”‚ β”‚ β”‚ 2. PARAMETER OPTIMIZATION (if drift detected) β”‚ β”‚ β”œβ”€ Define search space β”‚ β”‚ β”œβ”€ Run Optuna (Bayesian) β”‚ β”‚ β”œβ”€ Backtest candidates β”‚ β”‚ └─ Select best parameters β”‚ β”‚ β”‚ β”‚ 3. VALIDATION β”‚ β”‚ β”œβ”€ Walk-forward analysis β”‚ β”‚ β”œβ”€ Monte Carlo simulation β”‚ β”‚ └─ Out-of-sample test β”‚ β”‚ β”‚ β”‚ 4. A/B TESTING β”‚ β”‚ β”œβ”€ Deploy variant in paper trading β”‚ β”‚ β”œβ”€ Monitor 7 days β”‚ β”‚ └─ Compare vs control β”‚ β”‚ β”‚ β”‚ 5. DEPLOYMENT (if validated) β”‚ β”‚ β”œβ”€ Update strategy parameters β”‚ β”‚ β”œβ”€ Retrain ML models β”‚ β”‚ └─ Notify operators β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## 🎨 Patterns et Principes ### Design Patterns UtilisΓ©s #### 1. Singleton (Risk Manager) ```python class RiskManager: _instance = None _lock = threading.Lock() def __new__(cls): if cls._instance is None: with cls._lock: if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance ``` **Pourquoi** : Garantir une seule instance pour Γ©tat global cohΓ©rent #### 2. Strategy Pattern (StratΓ©gies de Trading) ```python class BaseStrategy(ABC): @abstractmethod def analyze(self, data): pass class ScalpingStrategy(BaseStrategy): def analyze(self, data): # ImplΓ©mentation scalping pass class IntradayStrategy(BaseStrategy): def analyze(self, data): # ImplΓ©mentation intraday pass ``` **Pourquoi** : Facilite ajout de nouvelles stratΓ©gies #### 3. Observer Pattern (Events) ```python class EventBus: def __init__(self): self.subscribers = {} def subscribe(self, event_type, callback): if event_type not in self.subscribers: self.subscribers[event_type] = [] self.subscribers[event_type].append(callback) def publish(self, event_type, data): for callback in self.subscribers.get(event_type, []): callback(data) # Usage event_bus.subscribe('trade_executed', log_trade) event_bus.subscribe('trade_executed', update_metrics) event_bus.publish('trade_executed', trade_data) ``` **Pourquoi** : DΓ©couplage entre composants #### 4. Factory Pattern (CrΓ©ation StratΓ©gies) ```python class StrategyFactory: @staticmethod def create(strategy_type: str, config: Dict) -> BaseStrategy: if strategy_type == 'scalping': return ScalpingStrategy(config) elif strategy_type == 'intraday': return IntradayStrategy(config) elif strategy_type == 'swing': return SwingStrategy(config) else: raise ValueError(f"Unknown strategy: {strategy_type}") ``` **Pourquoi** : Centralise logique de crΓ©ation ### Principes SOLID - **S**ingle Responsibility : Chaque classe une responsabilitΓ© - **O**pen/Closed : Ouvert extension, fermΓ© modification - **L**iskov Substitution : Sous-classes substituables - **I**nterface Segregation : Interfaces spΓ©cifiques - **D**ependency Inversion : DΓ©pendre d'abstractions --- ## πŸ”’ SΓ©curitΓ© ### Niveaux de SΓ©curitΓ© ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SECURITY LAYERS β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ LAYER 1: Authentication & Authorization β”‚ β”‚ β”œβ”€ API Key management β”‚ β”‚ β”œβ”€ OAuth 2.0 (IG Markets) β”‚ β”‚ └─ Role-based access control β”‚ β”‚ β”‚ β”‚ LAYER 2: Data Encryption β”‚ β”‚ β”œβ”€ Credentials encrypted at rest β”‚ β”‚ β”œβ”€ TLS/SSL for API calls β”‚ β”‚ └─ Database encryption β”‚ β”‚ β”‚ β”‚ LAYER 3: Input Validation β”‚ β”‚ β”œβ”€ Pydantic models β”‚ β”‚ β”œβ”€ SQL injection prevention β”‚ β”‚ └─ XSS protection β”‚ β”‚ β”‚ β”‚ LAYER 4: Rate Limiting β”‚ β”‚ β”œβ”€ API rate limiting β”‚ β”‚ β”œβ”€ Brute force protection β”‚ β”‚ └─ DDoS mitigation β”‚ β”‚ β”‚ β”‚ LAYER 5: Audit & Monitoring β”‚ β”‚ β”œβ”€ All actions logged β”‚ β”‚ β”œβ”€ Anomaly detection β”‚ β”‚ └─ Security alerts β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ“ˆ ScalabilitΓ© ### StratΓ©gies de Scaling #### Horizontal Scaling ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ HORIZONTAL SCALING STRATEGY β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ Load Balancer β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€β”€ Instance 1 (Scalping) β”‚ β”‚ β”œβ”€β”€β”€ Instance 2 (Intraday) β”‚ β”‚ └─── Instance 3 (Swing) β”‚ β”‚ β”‚ β”‚ Shared: β”‚ β”‚ β”œβ”€ Redis (Cache) β”‚ β”‚ β”œβ”€ PostgreSQL (Positions) β”‚ β”‚ └─ Message Queue (RabbitMQ) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` #### Vertical Scaling - Augmenter RAM pour cache plus large - Plus de CPU cores pour ML parallΓ¨le - SSD NVMe pour I/O rapide --- ## Structure RΓ©elle du Code (2026-03-08) ``` src/ β”œβ”€β”€ api/ β”‚ β”œβ”€β”€ app.py # FastAPI lifespan (init DB + RiskManager) β”‚ └── routers/ β”‚ β”œβ”€β”€ health.py # GET /health, /ready β”‚ └── trading.py # Toutes les routes trading β”‚ β”œβ”€β”€ core/ β”‚ β”œβ”€β”€ risk_manager.py # Singleton VaR/CVaR/circuit breakers β”‚ β”œβ”€β”€ notifications.py # Telegram + Email β”‚ └── strategy_engine.py # Orchestration des stratΓ©gies β”‚ β”œβ”€β”€ data/ β”‚ β”œβ”€β”€ data_service.py # AgrΓ©gation des sources β”‚ β”œβ”€β”€ yahoo_finance_connector.py β”‚ β”œβ”€β”€ alpha_vantage_connector.py β”‚ β”œβ”€β”€ data_validator.py β”‚ └── base_data_source.py β”‚ β”œβ”€β”€ db/ β”‚ β”œβ”€β”€ models.py # Trade, OHLCVData, BacktestResult, MLModelMeta β”‚ └── session.py # SQLAlchemy engine, get_db(), init_db() β”‚ β”œβ”€β”€ ml/ β”‚ β”œβ”€β”€ ml_engine.py # MLEngine (intΓ©grΓ© Γ  StrategyEngine) β”‚ β”œβ”€β”€ regime_detector.py # HMM β€” 3 rΓ©gimes (trend/range/volatile) β”‚ β”œβ”€β”€ feature_engineering.py # FeatureEngineering (50+ features) β”‚ β”œβ”€β”€ parameter_optimizer.py # Optuna TPE Sampler + walk-forward β”‚ β”œβ”€β”€ walk_forward.py # WalkForwardAnalyzer β”‚ β”œβ”€β”€ position_sizing.py # Kelly Criterion β”‚ β”œβ”€β”€ service.py # Microservice ML FastAPI (port 8200) β”‚ β”œβ”€β”€ ml_strategy_model.py # [NOUVEAU] XGBoost/LightGBM sur features TA β”‚ └── features/ β”‚ β”œβ”€β”€ technical_features.py # [NOUVEAU] TechnicalFeatureBuilder (~50 features) β”‚ └── label_generator.py # [NOUVEAU] Labels LONG/SHORT/NEUTRAL β”‚ β”œβ”€β”€ strategies/ β”‚ β”œβ”€β”€ base_strategy.py # ABC + Signal + StrategyConfig β”‚ β”œβ”€β”€ scalping/ β”‚ β”‚ └── scalping_strategy.py # BB + RSI + MACD + ATR β”‚ β”œβ”€β”€ intraday/ β”‚ β”‚ └── intraday_strategy.py β”‚ β”œβ”€β”€ swing/ β”‚ β”‚ └── swing_strategy.py β”‚ └── ml_driven/ # [NOUVEAU] β”‚ └── ml_strategy.py # MLDrivenStrategy β€” XGBoost pilote les signaux β”‚ β”œβ”€β”€ backtesting/ β”‚ β”œβ”€β”€ backtest_engine.py β”‚ β”œβ”€β”€ paper_trading.py β”‚ └── metrics_calculator.py β”‚ └── ui/ β”œβ”€β”€ dashboard.py # Streamlit β€” 5 onglets β”œβ”€β”€ api_client.py # Client httpx vers trading-api └── pages/ β”œβ”€β”€ live_trading.py β”œβ”€β”€ ml_monitor.py └── analytics.py # Monte Carlo models/ └── ml_strategy/ # [NOUVEAU] ModΓ¨les ML-Driven sauvegardΓ©s β”œβ”€β”€ EURUSD_1h_xgboost.joblib └── EURUSD_1h_xgboost_meta.json ``` --- **Architecture complΓ¨te et Γ©volutive !**