IntraFormer is a lightweight, channel-first transformer architecture designed specifically for next-day closing price prediction on daily equity data (OHLCV).
Instead of treating time steps as the sequence (common in most time-series transformers), IntraFormer treats the OHLCV channels as the sequence and projects the time dimension — a design choice that has shown surprisingly strong empirical performance on individual stock prediction tasks.
- Channel-sequential Transformer encoder (no patching)
- Reversible Instance Normalization (RevIN) tailored for financial time series
- Optional positional embeddings on channels
- Ensemble of 3 independently trained models (different seeds)
- Ablation suite comparing RevIN, positional embeddings, layer count, dropout
- Realistic walk-forward backtesting with transaction costs (5 bps)
- Comparison scripts vs traditional deep models (LSTM/GRU/CNN-LSTM/CLAM)
- Carbon footprint estimation example (training + inference energy)
thequantscientist-intraformer/
├── README.md
├── LICENSE # MIT
└── src/
├── IntraFormer.py # Main single-stock training & evaluation script
├── ablation.py # Ablation study: RevIN / pos emb / layers / dropout
├── backtesting.py # Portfolio-level walk-forward backtest
├── computation.py # Energy & CO₂ footprint measurement example
├── traditional_benchmark.py # LSTM / GRU / CNN-LSTM / CLAM baselines
└── transformer_benchmark.py # Autoformer / FEDformer / Informer / iTransformer
└── ultimate_results.csv # (example output – not versioned)
pip install torch pandas numpy scikit-learn
# optional: for transformer benchmarks
pip install einopsPlace daily OHLCV CSV files in data/ with format:
Date,open,high,low,close,volume
2020-01-02,74.06,75.15,73.80,75.09,135480400
...Example filename: AAPL_1d_full.csv
Update paths in the scripts if needed.
cd src
python IntraFormer.py→ Produces predictions and metrics in ~/qa/IntraFormer_all_equity_*
python ablation.py→ Compares 5 configurations across all stocks
After generating predictions:
python backtesting.pyShows long-only, top-N, and long-short portfolio performance with realistic costs.
Input: (B, 60 days, 5 channels: OHLCV)
→ Reversible Instance Normalization (per sample)
→ Linear projection: 60 → d_model=256 (per channel)
→ Add learnable channel positional embedding (optional)
→ TransformerEncoder (3 layers, 8 heads, GELU FFN)
→ Take close-channel representation (index 3)
→ MLP head → scalar next-day close prediction
→ Denormalize (if RevIN used)
- Best ablation usually includes RevIN + positional embeddings + 3 layers
- IntraFormer frequently outperforms LSTM/GRU baselines by 10–30% in RMSE/MAE on many names
- Portfolio strategies show positive Sharpe even after transaction costs in several configurations
If you find this work useful in your research, feel free to cite:
@misc{intraformer2026,
author = {Nguyen Quoc Anh},
title = {IntraFormer: Channel-Sequential Transformer for Daily Equity Close Prediction},
year = {2026},
url = {https://github.com/thequantscientist/intraformer}
}