Files
Freqtrade/tools/sklearn/crash_detection.py
Jérôme Delacotte c039aa29bd Crash detection
2025-12-13 13:53:51 +01:00

37 lines
1.2 KiB
Python

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# ================================
# 1. PREPARE DATA
# ================================
df = pd.read_feather("/home/jerome/Perso/freqtradeDocker/user_data/data/binance/BTC_USDC-1h.feather")
# features
df['returns'] = df['close'].pct_change()
df['atr'] = (df['high'] - df['low']).rolling(14).mean()
df['slope'] = df['close'].rolling(20).mean().diff()
df['drawdown'] = (df['close'] - df['close'].rolling(48).max()) / df['close'].rolling(48).max()
# label : crash si -12% dans les 48h
future = df['close'].shift(-48)
df['future_dd'] = (future - df['close']) / df['close']
df['crash'] = (df['future_dd'] < -0.12).astype(int)
df = df.dropna()
X = df[['returns','atr','slope','drawdown']]
y = df['crash']
# ================================
# 2. TRAIN MODEL
# ================================
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
model = RandomForestClassifier(n_estimators=200)
model.fit(X_train, y_train)
print("Accuracy:", model.score(X_test, y_test))
print("Feature importance:", model.feature_importances_)