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_)
