Skip to main content
ML Stock Trading Engine

Personal Project

ML Stock Trading Engine

ML Stock Trading Engine - image 1
ML Stock Trading Engine - image 2
ML Stock Trading Engine - image 3
ML Stock Trading Engine - image 4
ML Stock Trading Engine - image 5

Production-grade algorithmic trading pipeline with a 4-model ensemble (Random Forest, XGBoost, Dense NN, LSTM), 25+ technical indicators, unsupervised K-Means regime detection, full backtesting engine with Kelly criterion sizing, Monte Carlo simulation, multi-ticker portfolio optimization, parameter sensitivity analysis, and an interactive Streamlit + Plotly dashboard — 12 Python modules, 52 tests, ~3,000 lines of production code.

Engineered a data pipeline that fetches historical OHLCV data via the Yahoo Finance API with exponential-backoff retry logic and Parquet-based caching to avoid redundant API calls. The pipeline computes a feature matrix of 25+ technical indicators using fully vectorized NumPy/pandas operations — no loops over candles. Indicators span five categories: trend (SMA-20/50/200, EMA-12/26), momentum (RSI-14, MACD 12/26/9, Stochastic %K/%D, Williams %R, CCI-20, ROC-10), volatility (Bollinger Bands upper/lower/width/%B, ATR-14), volume (OBV, VWAP-20, Dollar Volume, Volume Ratio), and trend strength (ADX-14). Additional features include 5/10/20-day lookback windows for momentum, volatility, high/low range, 1/2/3/5-day lagged returns, and candlestick body/shadow ratios. Built-in data quality checks detect gaps, zero-volume days, and extreme outliers (>20% single-day moves).

Implemented unsupervised market regime discovery using K-Means clustering on the StandardScaler-normalized feature matrix, with PCA for 2D visualization. The algorithm partitions trading days into 5 heuristically-labeled regimes — High-Momentum Uptrend, Volatile Consolidation, Oversold Reversal, Low-Volatility Drift, and Distribution/Breakdown — based on mean return, RSI, and Bollinger Band width profiles. Cluster assignments are appended as categorical features for downstream models. The regime system lets the ensemble learn separate decision boundaries per market state, improving prediction accuracy in trending vs. mean-reverting environments.

Built a 4-model ensemble system that trains and combines: Random Forest (300 trees, max_depth=10, sqrt features), XGBoost (300 boosting rounds with L1/L2 regularization), a Keras Dense Network (128→64→32 with BatchNormalization + Dropout + ReduceLROnPlateau), and an LSTM (64→32 recurrent units with 20-day lookback sequences). Each model is cross-validated with TimeSeriesSplit to prevent data leakage. The ensemble combines predictions via CV-score-weighted averaging — higher-scoring models contribute more to the final signal. Feature importance is extracted from tree-based models to identify which indicators drive predictions. A standalone single-model mode uses RandomizedSearchCV across 50 hyperparameter configurations with 3-fold time-series CV and early stopping.

Designed a comprehensive backtesting engine with institutional-grade risk management: stop-loss, take-profit, trailing stop, and max position caps. Position sizing uses the Kelly criterion scaled by prediction confidence — higher-confidence signals receive proportionally larger allocations capped at 25% of portfolio. The engine tracks extended metrics: Sharpe, Sortino, and Calmar ratios, max drawdown duration, market exposure percentage, consecutive win/loss streaks, average hold duration, and profit factor. Walk-forward backtesting with rolling train/test windows validates out-of-sample robustness. A Monte Carlo simulation module bootstraps 1,000 synthetic equity curves from the strategy's daily returns, reporting percentile confidence intervals (P5/P25/P50/P75/P95) for terminal return, max drawdown, and Sharpe.

Added multi-ticker portfolio analysis with mean-variance optimization (max Sharpe, min variance, equal weight) using scipy.optimize, efficient frontier computation, cross-ticker correlation/covariance analysis, relative strength ranking across 20/60/120-day lookback windows, and beta computation against a benchmark ticker. A parameter sensitivity module runs 2D sweeps across any two backtest parameters (threshold × stop-loss, for example), generating annotated heatmaps of Sharpe, return, drawdown, and win rate across the grid — identifying optimal parameter regions. An analytics module provides benchmark comparison (rolling alpha/beta, information ratio, up/down capture ratios), day-of-week return analysis, monthly seasonality heatmaps, and rolling 63-day performance metrics.

Built an interactive Streamlit + Plotly web dashboard with 10 tabs: Backtest (equity curve + trade markers), Price/Features (candlestick + indicator overlays), Regimes (PCA cluster scatter), Models (per-model accuracy + feature importance), Signal Analysis (confusion matrix + reliability diagram + precision/recall/F1), Sensitivity (interactive 2D heatmaps), Monte Carlo (simulated equity fan chart with percentile bands), Analytics (benchmark comparison + seasonality), Portfolio (efficient frontier + optimal weights + correlation matrix), and Trade Log (filterable P&L table). The entire pipeline is configurable via YAML config or CLI flags (argparse) and includes model persistence (pickle for sklearn, .keras for TensorFlow, JSON metadata) for saving and reloading trained ensembles. The project has 52 unit tests across all modules.

PythonTensorFlowscikit-learnXGBoostLSTMK-MeansFinanceNumPyStreamlitMachine Learning