Academy/ML for Trading/Supervised Learning: Predicting Price Direction
ML for TradingLesson 2

Supervised Learning: Predicting Price Direction

Learn how to train machine learning models to predict if prices will go up or down.

14 minute read
4 key takeaways

Classification Models for Trading

We want to predict a discrete outcome: Will AAPL be higher in 5 days? (yes/no). This is a binary classification problem.

The ML Pipeline

  • Feature Engineering: Create X (features) and y (labels)
  • Train-Test Split: Keep some data for final evaluation (don't cheat)
  • Model Selection: Choose which algorithm
  • Training: Fit model on training data
  • Evaluation: Test on holdout data
  • Production: Generate live signals

Model Options

ModelProsConsBest For
Logistic RegressionSimple, interpretable, fastOnly linear relationshipsBaseline, small data
Random ForestHandles non-linearity, robustCan overfit, slowerMost use cases
XGBoostExcellent accuracy, fastBlack box, complex tuningLarge datasets, competition
Neural NetworkCaptures complex patternsNeeds more data, slowVery large datasets
python
from sklearn.model_selection import TimeSeriesSplit
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score

# Don't shuffle time series! Use TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5)

for train_idx, test_idx in tscv.split(X):
    X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
    y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]

    model = RandomForestClassifier(n_estimators=100)
    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    precision = precision_score(y_test, y_pred)
    print(f'Accuracy: {accuracy}, Precision: {precision}')

Key Metrics for Classification

  • Accuracy: (TP + TN) / Total - overall correctness
  • Precision: TP / (TP + FP) - when we predict UP, how often are we right?
  • Recall: TP / (TP + FN) - how many actual UP moves do we catch?
  • F1-Score: Harmonic mean of precision and recall
  • AUC-ROC: Area under the curve - overall model discrimination

Feature Importance: What Signals Matter?

python
# Random Forest tells us which features matter most
feature_importance = model.feature_importances_
feature_names = ['RSI', 'MACD', 'Volume_Ratio', 'Price_vs_SMA']

for name, importance in sorted(zip(feature_names, feature_importance),
                               key=lambda x: x[1], reverse=True):
    print(f'{name}: {importance:.3f}')

# Output:
# RSI: 0.342
# Volume_Ratio: 0.287
# MACD: 0.215
# Price_vs_SMA: 0.156

The Reality Check: 52% Accuracy = Profitable

You don't need 80% accuracy to profit. If your model is 52% accurate directing trades and you risk 1% to win 2%, you'll profit:

(0.52 × $2) - (0.48 × $1) = $1.04 - $0.48 = +$0.56 per dollar risked

Even modest accuracy works with good risk management

Calibration > Accuracy

A model that's 52% accurate and knows it's 52% accurate is more valuable than a model that's 80% accurate on training data but 50% on live data. Honest uncertainty beats overconfident accuracy.

Key Takeaways
  • Classification: predict discrete outcomes (up/down)
  • Logistic regression is a powerful baseline
  • Random forests and gradient boosting often outperform simpler models
  • Model performance on in-sample data means nothing—out-of-sample matters