This project implements a machine learning pipeline for predicting stock price direction movements using technical indicators and gradient boosting. The system uses XGBoost with time-series cross-validation to avoid look-ahead bias and provide realistic performance estimates.
- Predict direction, not exact price: Focus on classifying whether tomorrow's close will be higher/lower rather than predicting exact values
- Avoid look-ahead bias: Never use future information to predict the past
- Probabilistic approach: Aim for consistent 52-60% accuracy rather than perfect prediction
- Efficient Market Hypothesis: Markets quickly incorporate all available information
- Time Series Properties: Financial data is non-stationary and noisy
- Statistical edge: Small consistent advantages can be profitable through many trades
- OHLCV data (Open, High, Low, Close, Volume)
- Daily Returns
- Moving Averages: MA_5, MA_20, MA_50 (trend identification)
- Bollinger Bands: BB_Upper, BB_Middle, BB_Lower (mean reversion)
- Momentum: Price change over 5 days
- Volatility: 20-day rolling standard deviation of returns
- RSI: Relative Strength Index (overbought/oversold conditions)
- Price vs Moving Average ratios
- Bollinger Band position normalization
- Volume confirmation indicators
- Lagged features (previous day values)
- XGBoost Classifier: Gradient boosting optimized for performance and accuracy
- Objective: Binary classification (up/down prediction)
- Hyperparameters: Default settings with
random_state=42for reproducibility
- TimeSeriesSplit: 5-fold chronological cross-validation
- Training Window: Expands with each fold (rolling forward validation)
- Test Window: Fixed size (337 periods) that moves forward
- Performance Metrics: Accuracy, Precision, Recall, F1-Score, ROC-AUC
- Data Download: Yahoo Finance API (
yfinance) - Feature Calculation: Technical indicators from OHLCV data
- NaN Handling: Drop rows with missing values after indicator calculation
- Target Definition:
Target = 1 if tomorrow_close > today_close else 0 - Alignment: Ensure feature and target indices match exactly
# Core validation loop
tscv = TimeSeriesSplit(n_splits=5)
for fold, (train_index, test_index) in enumerate(tscv.split(X)):
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
# Fresh model for each fold to prevent leakage
model = XGBClassifier(random_state=42)
model.fit(X_train, y_train)
# Evaluate and store results- Accuracy: 52-60% range (slightly better than random)
- Consistency: Low standard deviation across folds indicates robustness
- Market Regime Dependence: Performance varies across bull/bear markets
- Primary: Accuracy and ROC-AUC
- Secondary: Precision, Recall, F1-Score
- Risk Management: Maximum drawdown, Sharpe ratio in backtesting
# Test model on Apple stock
results = test_stock_model("AAPL", "2017-01-01", "2024-01-01")# Compare across multiple stocks
tickers = ["AAPL", "MSFT", "GOOGL", "TSLA", "JPM"]
results = [test_stock_model(ticker) for ticker in tickers]- Always use
ilocfor position-based indexing in time series - Ensure perfect alignment between features and targets
- Handle NaN values before model training
- Use chronological train-test splits only
- Set
random_statefor reproducible results - Initialize fresh model for each cross-validation fold
- Monitor feature importance for insight
- Validate across multiple market regimes
- Consider transaction costs in target definition
- Implement stop-loss mechanisms
- Use position sizing based on model confidence
- Monitor performance consistency across time periods
- MultiIndex problems: Download one ticker at a time
- NaN values: Calculate indicators first, then drop missing values
- Alignment errors: Use
.loc[X.index]for target alignment
- Overfitting: Use simpler models or regularization
- Underfitting: Add more relevant features
- Look-ahead bias: Use TimeSeriesSplit exclusively
- Alternative data sources (news sentiment, macro indicators)
- Deep learning features (LSTM autoencoders)
- Market regime detection features
- Hyperparameter optimization
- Ensemble methods
- Bayesian optimization for parameter tuning
- Dynamic position sizing
- Correlation-aware portfolio construction
- Drawdown control mechanisms
This framework provides a robust foundation for stock prediction using machine learning. The emphasis on proper time-series validation and realistic performance expectations distinguishes it from naive prediction approaches. The system is designed for incremental improvement through careful feature engineering and model refinement rather than seeking mythical perfect predictions.