Spaces:
Sleeping
Sleeping
import numpy as np | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.preprocessing import StandardScaler | |
def prepare_features(df): | |
"""Prepare features for prediction model""" | |
features = df[['RSI', 'MACD', 'MACD_Signal', 'MACD_Hist']].copy() | |
features['SMA_Ratio'] = df['SMA_20'] / df['SMA_50'] | |
features['Price_Change'] = df['Close'].pct_change() | |
features['Volatility'] = df['Close'].rolling(window=20).std() | |
return features.dropna() | |
def predict_movement(df, lookback_period=30): | |
"""Predict price movement for next 30 minutes""" | |
features = prepare_features(df) | |
if len(features) < lookback_period: | |
return None, None | |
# Prepare training data | |
X = features[:-1].values | |
y = (df['Close'].shift(-1) > df['Close'])[:-1].values | |
# Train model | |
model = RandomForestClassifier(n_estimators=100, random_state=42) | |
model.fit(X, y) | |
# Make prediction | |
latest_features = features.iloc[-1:].values | |
prediction = model.predict(latest_features)[0] | |
probability = model.predict_proba(latest_features)[0] | |
return prediction, probability | |