Spaces:
Sleeping
Sleeping
File size: 4,331 Bytes
fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 1bfa112 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 fbb2dd4 9488241 20ec18d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import joblib
import os
# Nouveau nom pour forcer la création d'un modèle neuf
ML_MODEL_FILE = "ml_model_v9.pkl"
def prepare_ml_features(df):
"""
Extrait la structure du marché (Market Structure).
Les indicateurs (RSI, EMA, ATR) sont déjà calculés proprement par app.py !
"""
df = df.copy()
# 🎯 Target (Prédiction de la prochaine bougie)
# Si la prochaine bougie (shift -1) ferme plus haut, on note 1 (Hausse), sinon 0
df['target'] = (df['close'].shift(-1) > df['close']).astype(int)
# Nettoyage : On supprime la toute dernière ligne car on ne connaît pas encore son "futur"
df = df.dropna()
# Les 7 piliers de la décision (Déjà existants dans le df)
features = [
"RSI", "Dist_High_24h", "Dist_Low_24h",
"EMA_dist", "EMA_slope", "ATR_ratio", "VOL_ratio"
]
return df[features], df['target']
def train_model(df):
"""
Entraîne le RandomForest avec une limitation stricte pour protéger la RAM.
"""
print("🧠 [V9] Entraînement du Core ML (RandomForest) avec Market Structure...")
X, y = prepare_ml_features(df)
# Optimisation Quant pour CPU 2 cœurs / 4 threads
model = RandomForestClassifier(
n_estimators=100,
max_depth=5, # Anti-overfitting + RAM light
min_samples_split=10,
n_jobs=-1, # Utilise 100% du CPU dispo
random_state=42
)
model.fit(X, y)
joblib.dump(model, ML_MODEL_FILE)
print("✅ [V9] Core ML sauvegardé ! (Spécialiste des Ranges)")
return model
def load_model():
if os.path.exists(ML_MODEL_FILE):
return joblib.load(ML_MODEL_FILE)
return None
def predict_prob(model, df):
"""
Renvoie la probabilité (0 à 1) que la prochaine bougie soit verte.
"""
features = [
"RSI", "Dist_High_24h", "Dist_Low_24h",
"EMA_dist", "EMA_slope", "ATR_ratio", "VOL_ratio"
]
# On isole la toute dernière ligne du marché
last_row = df[features].iloc[[-1]]
return model.predict_proba(last_row)[0][1]
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import joblib
import os
# Nouveau nom pour forcer la création d'un modèle neuf
ML_MODEL_FILE = "ml_model_v9.pkl"
def prepare_ml_features(df):
"""
Extrait la structure du marché (Market Structure).
Les indicateurs (RSI, EMA, ATR) sont déjà calculés proprement par app.py !
"""
df = df.copy()
# 🎯 Target (Prédiction de la prochaine bougie)
# Si la prochaine bougie (shift -1) ferme plus haut, on note 1 (Hausse), sinon 0
df['target'] = (df['close'].shift(-1) > df['close']).astype(int)
# Nettoyage : On supprime la toute dernière ligne car on ne connaît pas encore son "futur"
df = df.dropna()
# Les 7 piliers de la décision (Déjà existants dans le df)
features = [
"RSI", "Dist_High_24h", "Dist_Low_24h",
"EMA_dist", "EMA_slope", "ATR_ratio", "VOL_ratio"
]
return df[features], df['target']
def train_model(df):
"""
Entraîne le RandomForest avec une limitation stricte pour protéger la RAM.
"""
print("🧠 [V9] Entraînement du Core ML (RandomForest) avec Market Structure...")
X, y = prepare_ml_features(df)
# Optimisation Quant pour CPU 2 cœurs / 4 threads
model = RandomForestClassifier(
n_estimators=100,
max_depth=5, # Anti-overfitting + RAM light
min_samples_split=10,
n_jobs=-1, # Utilise 100% du CPU dispo
random_state=42
)
model.fit(X, y)
joblib.dump(model, ML_MODEL_FILE)
print("✅ [V9] Core ML sauvegardé ! (Spécialiste des Ranges)")
return model
def load_model():
if os.path.exists(ML_MODEL_FILE):
return joblib.load(ML_MODEL_FILE)
return None
def predict_prob(model, df):
"""
Renvoie la probabilité (0 à 1) que la prochaine bougie soit verte.
"""
features = [
"RSI", "Dist_High_24h", "Dist_Low_24h",
"EMA_dist", "EMA_slope", "ATR_ratio", "VOL_ratio"
]
# On isole la toute dernière ligne du marché
last_row = df[features].iloc[[-1]]
return model.predict_proba(last_row)[0][1] |