PSX Stock Predictor: Walk-Forward Machine Learning for Pakistan Equity Returns
The complete research arc: from a rejected 15-stock price prototype, through BiLSTM return forecasters (V10-V16), to LightGBM cross-sectional rankers (V17 family) and a final net-of-costs portfolio study with significance testing, bootstrap robustness, regime-overlay sensitivity, and capacity analysis.
Arham Mirkar
DataLayer - Enterprise Data Infrastructure
Primary evidence: saved JSON/CSV artifacts in the PSX Stock Price Predictor repository.
Technical Summary
This study asks whether machine learning can produce deployable excess returns on the Pakistan Stock Exchange. Two model families were built on a 77-78 symbol panel with expanding walk-forward validation (each test year predicted by models trained only on prior data): BiLSTM-attention return forecasters, of which V14 is the strongest (61.4% mean 20-day directional accuracy across seven folds), and LightGBM cross-sectional rankers (V17 family) trained on percentile-rank targets computed from corporate-action-adjusted prices.
The central finding is negative for single models and positive for the ensemble: no individual model carries statistically significant portfolio alpha, but a 50/50 blend of the three-model rank ensemble with a 60/120-day momentum sleeve, wrapped in a 200-day moving-average regime overlay, earns 44.2% annualized net of 0.5% round-trip costs with Sharpe 1.47 versus 21.3% (Sharpe 0.86) for equal-weight buy-and-hold over 2020-2026. The monthly active return is +1.47% (t = 3.10, p = 0.0027) and survives block-bootstrap, parameter-grid, rolling-window, and liquidity-filter robustness checks — with returns compressing to 34.8% annualized on a realistically tradable universe.
The target is return ranking, not price memorization.
The research moved from next-close price regression to 20-day return forecasting because allocation depends on relative expected return, direction, and rank, not whether the model can reproduce split-adjusted price levels.
Observation grain
One symbol-date sample with a 60-trading-day lookback window and close-to-close forward return labels for 5d, 10d, and 20d horizons.
Primary target
20-trading-day return: (close[t+20] - close[t]) / close[t]. Direction is correct when predicted and realized returns have the same sign.
Validation design
Expanding walk-forward folds. Each test year is evaluated after training only on prior data, with a 30-calendar-day embargo around the validation boundary.
Feature families
Technical momentum, volatility, candlestick, market-index context, FIPI flow, USD/PKR, Brent crude, SBP policy-rate, sector-relative, regime, fundamental, and sentiment-derived inputs.
Leakage controls
Training-only scalers, temporal split by date, lagged macro/flow context, label thinning to reduce overlapping-target autocorrelation, and explicit corporate-action adjustment work.
BiLSTM attention with explicit directional pressure.
The dominant architecture projects the feature window, encodes sequence dependence with bidirectional LSTMs, uses multi-head self-attention for temporal weighting, and predicts multi-horizon returns. Later versions add loss terms for sign agreement, downside recall, magnitude matching, variance floor, and bias correction.
Input window: (batch, 60 trading days, N features) -> feature dropout + linear projection -> sinusoidal position context -> 2-layer bidirectional LSTM, hidden=128 -> 4-head self-attention + residual normalization -> feed-forward block + residual normalization -> last-step latent state -> return heads: 5d, 10d, 20d -> loss: Huber + sign penalty + asymmetric downside + variance + bias
Lookback
60 trading days
Primary horizon
20 trading days
V14 features
97 filtered inputs
V16 features
130 filtered inputs
Training split
Expanding walk-forward
Embargo
30 calendar days
Every major result changes the interpretation.
The research is incomplete if it only reports the best number. The archive shows why objectives, features, and validation definitions matter.
Direction survives most regimes; rank quality does not.
V14 is directionally stable across most folds, but Spearman IC turns negative in 2022 and varies sharply by regime. The result is real enough to study, not stable enough to ship without a risk layer.
| Fold | Direction | IC | MAPE | Down Recall | Up Precision | Interpretation |
|---|---|---|---|---|---|---|
| 2020 | 64.2% | +0.313 | 13.56% | 53.1% | 64.8% | Strong broad-market signal |
| 2021 | 60.2% | +0.060 | 14.85% | 90.3% | 51.5% | Defensive bias helped downside calls |
| 2022 | 60.4% | -0.072 | 15.91% | 92.7% | 28.3% | Direction survived, rank IC failed |
| 2023 | 57.3% | +0.093 | 15.93% | 32.2% | 59.9% | Rally regime challenged down bias |
| 2024 | 57.8% | +0.204 | 17.62% | 60.3% | 66.9% | Positive ranking recovery |
| 2025 | 54.7% | +0.100 | 16.30% | 41.3% | 58.1% | Weakest completed calendar fold |
| 2026 | 75.3% | +0.503 | 12.97% | 84.5% | 71.6% | Partial-year stress-regime outlier |
Common-fold comparison prevents cherry-picking.
The table below is freshly recomputed from raw prediction CSVs on shared 2020-2023 folds. It is not generated from the old comparison report. IC is Spearman rank correlation between predicted and realized 20-day returns.
| Model | Folds | N | Dir | IC | Ret MAE | Bias | Top-10 Alpha | Beat Rate |
|---|---|---|---|---|---|---|---|---|
| V10.5 | 2020-2023 | 13,838 | 60.65% | +0.257 | 12.21% | -4.98 pp | +0.85 pp | 59.9% |
| V14 | 2020-2023 | 22,868 | 60.47% | +0.234 | 15.44% | -4.44 pp | +0.48 pp | 48.9% |
| V16-old | 2020-2023 | 22,868 | 60.34% | +0.203 | 15.44% | -5.84 pp | -0.07 pp | 50.2% |
| V16-fixed | 2020-2023 | 22,868 | 57.40% | +0.143 | 10.86% | -2.90 pp | +0.43 pp | 50.5% |
Top-10 alpha is the average realized 20-day return of the top-10 predicted stocks minus the same-date universe average. It is a period statistic, not a compounded strategy return.
The signal is stock-specific.
Per-stock performance shows that the model is not learning a single universal PSX rule. Banks and defensives often rank better than high-volatility technology names in the common-fold audit.
Ranking alpha exists, but compounding claims are fragile.
The local files include very high compounded top-N results when overlapping 20-day predictions are chained. Those figures are not suitable as a research claim; the conservative evidence here is average per-period top-basket return over the same-date universe. The proper daily net-of-costs backtest that resolves this question appears in the strategy study further below.
A separate non-overlapping V10.5 final audit reported combined 20-day rebalanced returns of +65.1% for Top-5 over eight periods, with -15.2% max drawdown. It is promising but too sparse to substitute for the broader walk-forward classification evidence.
Corporate actions were a first-order error source.
PSX bonuses, splits, and rights issues create mechanical price discontinuities. If those are not adjusted, the model is rewarded or punished for predicting accounting mechanics rather than investor returns.
| Symbol | Event | Period | Research impact | Status |
|---|---|---|---|---|
| SYS | 5:1 stock split | March 2025 | Raw price discontinuity caused the model to interpret a mechanical share split as a crash. | Adjusted |
| LUCK | 1:1 bonus issue | July 2024 | Unadjusted labels inflated price-error metrics and distorted return targets around the ex-date. | Adjusted |
| UBL | Bonus issue | 2024 | Incorrect raw close continuity made bank-sector forecasts look worse than the adjusted series. | Adjusted |
| MARI | Right issue | 2023 | Return series required ratio-style adjustment to separate entitlement mechanics from market repricing. | Adjusted |
| ENGRO | 1:1 bonus | 2021 | Historical levels needed back-adjustment so the model did not learn a false structural break. | Adjusted |
The V17 family predicts ranks, not returns.
The corporate-action findings above motivated a redesign: LightGBM models trained on the daily cross-sectional percentile rank of split-adjusted 20-day forward returns. Predicting which stocks beat the cross-section is exactly what a long-only top-N portfolio needs. The table shows out-of-sample daily rank IC (Spearman) per walk-forward fold.
| Fold | V17 (early stop) | V17b (3-seed, 400 rounds) | V17c (+momentum block) |
|---|---|---|---|
| 2020 | +0.013 | -0.024 | -0.010 |
| 2021 | +0.085 | +0.037 | +0.034 |
| 2022 | +0.059 | +0.078 | +0.099 |
| 2023 | +0.012 | +0.013 | +0.032 |
| 2024 | +0.058 | +0.109 | +0.106 |
| 2025 | +0.070 | +0.068 | +0.071 |
| 2026 | +0.147 | +0.107 | +0.093 |
V17's early-stopping window collapsed to a single tree on half the folds because the last 10% of training dates rarely resembled the test year. V17b fixes this with a fixed 400 boosting rounds, stronger regularization, and 3-seed averaging: its IC is positive in six of seven folds. V17c adds explicit momentum features but did not improve portfolios — momentum proved more valuable as a separate sleeve.
No single model is significant. The ensemble is.
Every configuration below is a daily net-of-costs backtest over 2020-2026: top-N equal weight, rebalanced every 20 trading days with 1-day execution lag, 0.5% round-trip costs, corporate-action adjusted prices, and idle cash earning the SBP policy rate. Alpha t is a monthly regression of active returns against equal-weight buy-and-hold.
| Configuration | Ann. Return | Sharpe | Max DD | Alpha t | Beat B&H | Reading |
|---|---|---|---|---|---|---|
| KSE-100 index | 24.4% | 1.21 | -37.0% | — | — | Market benchmark |
| Equal-weight buy & hold | 21.3% | 0.86 | -46.0% | — | — | Universe benchmark all alpha is measured against |
| V14 top-10 (best single model) | 25.8% | 0.92 | -41.9% | 0.83 | 6/7 | Positive but not significant |
| V17b top-3 (best single ranker) | 36.7% | 1.03 | -50.7% | 1.47 | 4/7 | Higher return, concentrated and unstable |
| 3-way ML rank blend, top-10 | 33.9% | 1.12 | -43.6% | 1.70 | 6/7 | Diversifying model errors beats any single model |
| Momentum 60/120d, top-10 | 39.9% | 1.22 | -41.5% | 2.77 | 6/7 | The strongest standalone factor in PSX 2020-2026 |
| 50% ML blend + 50% momentum, top-10 | 44.1% | 1.34 | -44.0% | 3.21 | 7/7 | Only configuration to beat B&H every year |
| Final: combo + 200d-MA regime overlay | 44.2% | 1.47 | -34.8% | 3.10 | 6/7 | Production configuration (V17_PROD) |
The blend's value is error diversification: V14 (sequence model, raw-price era), V17 (GBM on V14 features), and V17b (GBM on expanded features) make different mistakes, so averaging their cross-sectional ranks is more stable than any member. Adding the momentum sleeve at the portfolio level — rather than as model features, which V17c showed dilutes it — produces the only configuration that beat buy-and-hold in all seven years.
Year by year, the overlay earns its keep in bears.
V17_PROD is the production configuration: the 50/50 ML-momentum combo with gross exposure halved whenever the KSE-100 closes below its 200-day moving average, cash earning the SBP policy rate (13-22% during the 2022-23 tightening cycle).
| Year | V17_PROD | Combo, no overlay | EW Buy & Hold | KSE-100 |
|---|---|---|---|---|
| 2020 | +51.7% | +58.8% | +41.4% | +4.6% |
| 2021 | +19.7% | +17.1% | -4.9% | +0.4% |
| 2022 | -5.9% | -15.5% | -24.2% | -9.4% |
| 2023 | +51.5% | +56.1% | +33.1% | +54.5% |
| 2024 | +140.3% | +140.3% | +88.2% | +87.4% |
| 2025 | +76.8% | +76.8% | +40.0% | +50.7% |
| 2026* | -6.4% | -1.6% | -4.4% | -3.0% |
2026 is a partial year through June. The overlay's value concentrates in 2022: -5.9% against -24.2% for buy-and-hold. Its cost shows in choppy 2026, where MA whipsaws left the strategy behind the no-overlay combo.
The strategy is deployable at PKR 100-400M, not unlimited.
Re-running the production strategy with average-daily-value floors on the selection universe, assuming 15% volume participation, quantifies how much of the edge lives in small caps.
| Universe filter | Ann. Return | Sharpe | Alpha t | Deployable AUM |
|---|---|---|---|---|
| No filter | 44.2% | 1.47 | 3.10 | ~PKR 31M median |
| ADV >= 10M | 34.5% | 1.22 | 1.83 | ~PKR 150M |
| ADV >= 25M | 34.8% | 1.22 | 1.93 | ~PKR 209M median |
| ADV >= 50M | 32.2% | 1.14 | 1.65 | ~PKR 300M |
| ADV >= 100M | 24.6% | 0.92 | 0.54 | PKR 400M+ |
The unconstrained 44.2% headline is honest only for personal-account size. On an ADV >= PKR 25M universe the result is 34.8% annualized, Sharpe 1.22, with median deployable AUM around PKR 209M (2026 median ~PKR 485M).
What the paper does not prove.
A research paper becomes stronger when its negative evidence is visible. These caveats are not footnotes; they define the boundary between a promising signal and an investable system.
Roughly half the final edge is medium-term momentum, and PSX 2020-2026 was a momentum-friendly market. The pure ML sleeve alone is t = 1.70 — suggestive, not conclusive.
2026 year-to-date the production strategy is -6.4% versus -4.4% for buy-and-hold: the regime overlay whipsawed in the current choppy market. It earns its keep in sustained bears (2022: -5.9% vs -24.2%) and costs a little in chop.
The backtest assumes fills at the close with 15% volume participation and 0.5% round-trip costs; long-only, no leverage, no borrow. Tick size, circuit breakers, and intraday impact are not simulated.
Unconstrained headline numbers rely on small-cap picks with ~PKR 31M median deployable capacity. On an ADV >= PKR 25M universe the annualized return falls to 34.8% and alpha significance weakens to t = 1.93.
V14 LSTM predictions refresh only with walk-forward retrains; only the LightGBM models have a live inference path. A live blend today leans more on the GBM sleeve than the backtest did.
The 2026 fold is a partial year. All seven-fold aggregates and 7/7 beat counts include it, and conclusions could shift when the year completes.
Final verdict: the best model is the ensemble.
Asked "which model was best," the honest answer has two levels. Among single models, V14 remains the strongest complete deep-learning evidence set (61.4% mean direction) and V17b is the most reliable ranker (positive rank IC in six of seven folds) — but neither carries significant portfolio alpha alone. The research-grade result is the production strategy: a 50/50 blend of the V14 + V17 + V17b rank ensemble with 60/120-day momentum, under a 200-day-MA regime overlay, delivering 44.2% annualized net (Sharpe 1.47, alpha t = 3.10) against 21.3% for buy-and-hold — compressing to a still-attractive 34.8% on a realistically tradable universe. What remains is not modeling but evidence: a live, auditable track record.
Sources and Reproducibility
Primary local artifacts reviewed: results/all_results.json, results/baseline_comparison.json, results_v10.1_monthly/all_results.json, results_v10.5_quick_test/walk_forward_results.json, results_v14/walk_forward_results.json, results_v16/walk_forward_results.json, raw prediction CSVs under results_v10.5_quick_test, results_v14, results_v16, results_v16_fixed, results_v17/ v17b/ v17c, and the full strategy evaluation suite in results_v17_eval (top-N comparisons, significance tests, overlay robustness grid, block bootstrap, rolling 12-month actives, capacity analysis) plus V17_PRODUCTION_REPORT.txt.
Data sources include Pakistan Stock Exchange OHLCV histories, KSE index histories, NCCPL/FIPI flow files, SBP policy-rate data, USD/PKR, Brent crude, corporate announcements, and parsed company financials where available.
This research is educational and methodological. It is not investment advice, a solicitation, or a representation of live trading performance.