# ensemble.py import sqlite3 import os DB_FILE = "alphatrade_v9.db" # Chemin relatif pour Pterodactyl def get_dynamic_weights(symbol, regime): # 1. Poids de base (Théorie V10 avec LSTM) # On donne 40% au LSTM par défaut car c'est ton Deep Learning if regime == 0: w_time, w_ml, w_lstm, w_sent = 0.25, 0.15, 0.40, 0.20 # BULL elif regime == 1: w_time, w_ml, w_lstm, w_sent = 0.25, 0.15, 0.20, 0.40 # PANIC (News prioritaires) elif regime == 2: w_time, w_ml, w_lstm, w_sent = 0.25, 0.30, 0.25, 0.20 # PULLBACK else: w_time, w_ml, w_lstm, w_sent = 0.20, 0.40, 0.20, 0.20 # RANGE if not os.path.exists(DB_FILE): return w_time, w_ml, w_lstm, w_sent try: conn = sqlite3.connect(DB_FILE) cursor = conn.cursor() cursor.execute('''SELECT direction, status, prob_time, prob_ml, prob_lstm FROM signals WHERE symbol=? AND regime=? AND status != 'EN_COURS' ORDER BY id DESC LIMIT 10''', (symbol, regime)) trades = cursor.fetchall() conn.close() if len(trades) >= 3: time_score, ml_score, lstm_score = 0, 0, 0 for t in trades: direction, status, p_time, p_ml, p_lstm = t actual_up = (direction == 'HAUSSIER 📈' and 'GAGNÉ' in status) or \ (direction == 'BAISSIER 📉' and 'PERDU' not in status and 'GAGNÉ' not in status) # Logique simplifiée if (p_time > 0.5) == actual_up: time_score += 1 if (p_ml > 0.5) == actual_up: ml_score += 1 if (p_lstm > 0.5) == actual_up: lstm_score += 1 # Punition/Récompense (+/- 10%) if time_score > ml_score + 2: w_time += 0.10; w_ml -= 0.10 if lstm_score > time_score + 2: w_lstm += 0.10; w_time -= 0.10 except: pass return round(w_time, 2), round(w_ml, 2), round(w_lstm, 2), round(w_sent, 2) def combine_scores(symbol, time_p, ml_p, lstm_p, sentiment_p, regime): w_t, w_m, w_l, w_s = get_dynamic_weights(symbol, regime) # Filtre de Sentiment senti_calc = 0.5 if 0.35 <= sentiment_p <= 0.65 else sentiment_p # Moyenne pondérée V10 base_p = (time_p * w_t) + (ml_p * w_m) + (lstm_p * w_l) + (senti_calc * w_s) # Bonus de Convergence (Si XGB et LSTM sont d'accord) bonus = 0.04 if abs(time_p - lstm_p) < 0.05 else 0 final_p = base_p + bonus if (time_p + lstm_p)/2 > 0.5 else base_p - bonus return max(0.0, min(1.0, final_p)), w_t, w_m, w_l, w_s