Skip to content

SM1LE-X/Apex-Quant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ApexQuant

A 2026 state-of-the-art financial time series benchmarking framework. Implements and extends the FinTSB paper with production-grade backtesting, 16 registered models across 6 backbone categories, HMM regime detection, and real-world market constraints.

14,000+ lines | 70+ files | 18 models | 4 strategies | 18 metrics | 43 alpha factors | Alpaca paper trading | Sharpe 8.53 intraday | All 36 tests passing

Quick Start

# Install
cd apex-quant
pip install -e .

# 1. Verify everything works (3 seconds)
python scripts/quick_start.py

# 2. Backtest with real S&P 500 data (default)
python scripts/run_backtest.py --model xgboost

# 3. Backtest specific tickers
python scripts/run_backtest.py --model lstm --tickers AAPL,MSFT,GOOGL,NVDA,TSLA

# 4. Compare all models head-to-head
python scripts/compare_models.py

# 5. Launch interactive TUI
python scripts/tui.py

# 6. Detect market regimes on real data
python scripts/detect_regimes.py --ticker SPY --method hmm

# 7. Train the alpha ranker (S&P 100, walk-forward)
python scripts/train_alpha.py

# 8. Paper trade on Alpaca (needs .env with API keys)
python scripts/paper_trade.py --mode alpaca

CLI Scripts

Script What it does Default Data
scripts/quick_start.py Verify install, show all models, run demo backtest Synthetic
scripts/run_backtest.py Full backtest with any model/strategy/market Real (yfinance)
scripts/compare_models.py Head-to-head comparison of multiple models Synthetic
scripts/fetch_data.py Download & cache market data (US equities, crypto) Real
scripts/detect_regimes.py Detect market regimes (rule-based or HMM) Synthetic / Real
scripts/compute_metrics.py Compute all 18 metrics from predictions Synthetic
scripts/tui.py Interactive terminal UI with model/strategy selectors Synthetic
scripts/paper_trade.py Paper trading (simulated / Alpaca / CCXT sandbox) Real
scripts/train_alpha.py Alpha ranker training with walk-forward evaluation Real

Examples

# Real data: backtest XGBoost on 10 stocks (2022-2025)
python scripts/run_backtest.py --model xgboost \
    --tickers AAPL,MSFT,GOOGL,NVDA,TSLA,META,AMZN,JPM,V,JNJ \
    --start 2022-01-01 --end 2025-01-01

# Real data: LSTM on top 30 S&P 500 stocks
python scripts/run_backtest.py --model lstm --n-stocks 30

# Real data: compare tree models vs deep learning
python scripts/compare_models.py --models xgboost,lightgbm,lstm,transformer,localformer

# Real data: all models at once
python scripts/run_backtest.py --model all

# Real data: Chinese market constraints
python scripts/run_backtest.py --model xgboost --market chinese --k 30

# Real data: fetch S&P 500 with technical indicators
python scripts/fetch_data.py --source yfinance --tickers sp500 --n 50 --features --output data.parquet

# Real data: HMM regime detection on SPY
python scripts/detect_regimes.py --ticker SPY --method hmm

# Synthetic fallback (no internet needed)
python scripts/run_backtest.py --model lstm --source synthetic

Architecture

apex-quant/
├── scripts/               # One-command scripts (start here)
│   ├── quick_start.py     # Verify install
│   ├── run_backtest.py    # Full backtest (real data default)
│   ├── compare_models.py  # Head-to-head comparison
│   ├── fetch_data.py      # Download market data
│   ├── detect_regimes.py  # Market regime detection
│   ├── compute_metrics.py # Metrics calculator
│   └── tui.py             # Interactive terminal UI
│
├── config/
│   └── default.yaml       # Full pipeline config (16 models)
│
├── apex_quant/
│   ├── core/              # Type system + model registry
│   │   ├── types.py       # MarketRegime, Portfolio, BacktestResult, MetricResult
│   │   └── registry.py    # @register_model / @register_strategy decorators
│   │
│   ├── data/              # Data pipeline
│   │   ├── sources/       # YFinance (US equities) + CCXT (crypto)
│   │   ├── features/      # 25 technical indicators + statistical characteristics
│   │   ├── pattern.py     # Rule-based + HMM regime detection
│   │   ├── tokenizer.py   # Cross-sectional normalization (no future leakage)
│   │   └── pipeline.py    # End-to-end: fetch -> features -> normalize -> split
│   │
│   ├── models/            # 16 registered models
│   │   ├── classic/       # CSM (momentum), BLSW (mean reversion)
│   │   ├── ml/            # XGBoost, LightGBM
│   │   ├── dl/            # LSTM, GRU, Mamba, Transformer, PatchTST,
│   │   │                  # Localformer, iTransformer, GCN, GAT
│   │   ├── rl/            # PPO, DQN
│   │   └── generative/    # DDPM, DDIM (diffusion-based probabilistic)
│   │
│   ├── losses/            # Dual-objective (MSE + ranking), pairwise, listwise, IC loss
│   │
│   ├── backtest/          # Production-grade backtesting
│   │   ├── engine.py      # Day-by-day simulation + walk-forward
│   │   ├── strategies.py  # TopK, TopK-Drop, LongShort, RiskParity
│   │   ├── constraints.py # Tx costs, slippage, limit-up/down, trading halts
│   │   ├── metrics.py     # 18 metrics (IC, ICIR, Sharpe, MDD, VaR, CVaR, ...)
│   │   └── risk.py        # Drawdown limits, position concentration, stop-loss
│   │
│   ├── pipeline/          # Orchestration
│   │   ├── trainer.py     # Early stopping on validation IC, mixed precision
│   │   ├── evaluator.py   # Per-regime evaluation
│   │   └── experiment.py  # Full benchmark runner with parallel training
│   │
│   └── viz/               # Plotly dashboards
│       └── dashboard.py   # Equity curves, drawdowns, regime heatmaps
│
├── tests/
│   ├── test_smoke.py      # 10 end-to-end tests
│   └── test_ship_ready.py # 7-phase ship-readiness verification
│
├── docs/
│   └── USAGE_GUIDE.md     # Complete 10-section usage guide
│
├── README.md              # This file
├── WALKTHROUGH.md          # Step-by-step developer guide
└── pyproject.toml          # Dependencies & build config

Registered Models (16)

Category Model Description
Classic csm Cross-Sectional Momentum
Classic blsw Buy Losers Sell Winners (mean reversion)
ML xgboost Gradient-boosted trees
ML lightgbm LightGBM with GOSS
DL lstm Multi-layer LSTM
DL gru Gated Recurrent Unit
DL mamba Selective State Space Model (S6, built from scratch)
DL transformer Vanilla Transformer encoder
DL patchtst Patch-based channel-independent Transformer
DL localformer Local causal conv + local attention (best DL in FinTSB)
DL gcn_stock Graph Convolutional Network for inter-stock correlations
DL gat_stock Graph Attention Network with learned edge weights
RL ppo Proximal Policy Optimization
RL dqn Deep Q-Network
Generative ddpm Denoising Diffusion Probabilistic Model
Generative ddim Denoising Diffusion Implicit Model (fast sampling)

Trading Strategies (4)

Strategy Description Key Params
topk Select top-K stocks, equal weight, full daily rebalance k
topk_drop Paper default: retain persistent top stocks, only drop worst N k, drop
long_short Long top-K, short bottom-K, dollar neutral k
risk_parity Top-K by prediction, weighted by inverse volatility k

Metrics (18)

Dimension Metrics
Ranking IC, ICIR, RankIC, RankICIR
Portfolio ARR, AVol, MDD, Sharpe (ASR), IR, Sortino, Calmar
Error MSE, MAE
Risk VaR (95%), CVaR (95%), Omega, Tail Ratio, Win Rate, Profit Factor

What Goes Beyond FinTSB

Feature FinTSB Paper ApexQuant
Data source Static datasets Live yfinance / CCXT with caching
Regime detection Rule-based (thresholds) HMM with BIC auto-selection
Train/test split Static 7:1:2 Walk-forward + rolling window
Slippage None Square-root market impact model
Position sizing Equal weight only Equal, risk parity, Kelly, vol-target
Risk management None Drawdown limits, VaR, stop-loss, concentration
Markets CN + US only US, Chinese, Crypto (pre-configured presets)
Architectures ~30 models 16 with Mamba, Diffusion, GNN, iTransformer
Loss functions MSE + ranking + Listwise, IC loss, adaptive weighting
Metrics 11 18 (+ Sortino, Calmar, VaR, CVaR, Omega)
Data engine Qlib (pandas) Polars (10-100x faster)
Interface Code only CLI scripts + Interactive TUI

Usage Examples

1. Backtest with Real Data (Default)

from apex_quant.backtest.engine import BacktestEngine
from apex_quant.backtest.strategies import TopKDropStrategy
from apex_quant.backtest.constraints import MarketConstraints

# Use the CLI for easiest access:
#   python scripts/run_backtest.py --model xgboost --tickers AAPL,MSFT,GOOGL

# Or programmatically:
engine = BacktestEngine(
    strategy=TopKDropStrategy(k=10, drop=3),
    constraints=MarketConstraints.us_market(),
    initial_capital=1_000_000,
)
result = engine.run(predictions, returns, stock_ids)
print(f"Sharpe: {result.metrics['asr']:.2f}")
print(f"Return: {result.metrics['arr']*100:.1f}%")
print(f"MDD:    {result.metrics['mdd']*100:.1f}%")

2. Fetch Real Market Data

from apex_quant.data.sources.yfinance_source import YFinanceSource
from apex_quant.data.features.technical import compute_all_features

source = YFinanceSource()
tickers = source.get_sp500_tickers()[:50]
df = source.fetch(tickers, start="2020-01-01", end="2025-12-31")

# Add 24 technical indicators (all vectorized with polars)
df_featured = compute_all_features(df)

3. Train a DL Model

from apex_quant.models.dl.recurrent import LSTMModel

model = LSTMModel(input_dim=29, hidden_dim=128, num_layers=2, num_stocks=1)

# Built-in training loop: early stopping on validation IC, mixed precision, gradient clipping
model.fit(train_data, config={
    "epochs": 50, "batch_size": 256, "lr": 1e-3, "patience": 10, "device": "cuda",
})

predictions = model.predict(test_data)

4. Walk-Forward Backtesting

result = engine.run_walk_forward(
    model=my_model,
    full_data=X_all,         # (T, N, L, F)
    full_returns=returns_all, # (T, N)
    stock_ids=stock_ids,
    train_window=175,         # ~9 months
    test_window=50,           # ~2.5 months
    step=50,                  # retrain every 50 days
)

5. Detect Market Regimes

from apex_quant.data.pattern import classify_regimes

# HMM auto-detects 2-5 regimes via BIC
segments = classify_regimes(returns, method="hmm")
for seg in segments:
    print(f"{seg.regime.value}: days {seg.start_idx}-{seg.end_idx} "
          f"(confidence: {seg.confidence:.0%})")

6. Diffusion Models for Probabilistic Forecasting

from apex_quant.models.generative.diffusion import DDPMForecaster

model = DDPMForecaster(input_dim=29, num_stocks=50, hidden_dim=256, n_diffusion_steps=100)
predicted_distribution = model(context_tensor)  # (batch, 50) sampled returns

7. Custom Loss Functions

from apex_quant.losses.dual_objective import DualObjectiveLoss, ICLoss, CombinedLoss

# Paper default: MSE + 5x pairwise ranking (adaptive weighting)
loss = DualObjectiveLoss(eta=5.0, adaptive=True)

# Directly optimize Information Coefficient
loss = ICLoss(temperature=0.5)

# Multi-objective
loss = CombinedLoss(losses={
    "mse": (torch.nn.MSELoss(), 1.0),
    "rank": (PairwiseRankingLoss(), 5.0),
    "ic": (ICLoss(), 2.0),
})

8. Market-Specific Constraints

from apex_quant.backtest.constraints import MarketConstraints

cn = MarketConstraints.chinese_market()   # 10% limit-up/down, no shorting, 100-lot
us = MarketConstraints.us_market()        # No daily limits, shorting OK, 1-lot
cr = MarketConstraints.crypto_market()    # Higher slippage, 24/7 trading

# Custom constraints
from apex_quant.backtest.constraints import TransactionCostModel, SlippageModel, TradingRestrictions
custom = MarketConstraints(
    costs=TransactionCostModel(proportional_fee=0.0002),
    slippage=SlippageModel(fixed_slippage_bps=2.0),
    restrictions=TradingRestrictions(allow_short=True, min_lot_size=1),
)

9. Risk Management

from apex_quant.backtest.risk import RiskManager

rm = RiskManager(
    max_drawdown=-0.15,       # Go to cash at -15% drawdown
    max_position_weight=0.05, # No single stock > 5%
    var_limit=0.03,           # Portfolio VaR ceiling
)
safe_weights = rm.apply_risk_limits(raw_weights, equity, returns_history)

10. Add Your Own Model (3 minutes)

from apex_quant.core.registry import register_model
from apex_quant.models.base import BaseTorchModel
import torch.nn as nn

@register_model("my_model", category="dl")
class MyModel(BaseTorchModel):
    def __init__(self, input_dim=64, num_stocks=1):
        super().__init__()
        self.net = nn.Sequential(nn.Linear(input_dim, 128), nn.GELU(), nn.Linear(128, num_stocks))
        self.num_stocks = num_stocks

    @property
    def name(self): return "my_model"
    @property
    def category(self): return "dl"

    def forward(self, x):
        return self.net(x[:, -1, :])  # use last timestep

# Now available everywhere: python scripts/run_backtest.py --model my_model

Configuration

See config/default.yaml for the full pipeline config:

data:
  source: yfinance
  tickers: sp500
  lookback: 20
  split:
    method: walk_forward

models:
  - name: localformer
    params: { d_model: 128, n_heads: 8, local_window: 5 }
  - name: xgboost
    params: { n_estimators: 500, max_depth: 6 }

training:
  loss: { type: dual_objective, eta: 5.0, adaptive: true }
  epochs: 100
  patience: 10

backtest:
  strategy: topk_drop
  k: 30
  drop: 5
  market: us

Paper Trading (Live)

Full paper-trading infrastructure with 3 execution modes:

Mode Assets Fills Cost
simulated Any Modeled (stale prices) Free
sandbox Crypto CCXT testnet Free
alpaca US equities Real matching engine Free
# 1. Set Alpaca keys in .env
#    ALPACA_API_KEY=PKxxx...
#    ALPACA_SECRET_KEY=xxx...

# 2. Run paper trading with live dashboard
python scripts/paper_trade.py --mode alpaca

# Dashboard at http://localhost:8000/
# API docs at http://localhost:8000/docs

10-layer architecture: DataStream, Ensemble, SignalEngine, Executor, Portfolio, RiskMonitor, Journal, Scheduler, Server, Runner.

Alpha Engine (Best Model)

The Intraday Cross-Sectional Mean Reversion strategy is the production model:

Metric Value
Sharpe Ratio 8.53
Win Rate 79%
Annualized Return +176%
Max Drawdown -4.0%
Calmar Ratio 45
Universe 50 mega-cap US stocks
# Train and evaluate the alpha ranker (daily cross-sectional)
python scripts/train_alpha.py

# Run the intraday strategy backtest
python -c "
from apex_quant.alpha.intraday import run_intraday_backtest
import polars as pl
df = pl.read_parquet('data/intraday_50stocks.parquet')
r = run_intraday_backtest(df, top_k=10, bottom_k=10, entry_hours=list(range(8,21)))
print(f'Sharpe: {r.sharpe:.2f}, Cum: {r.cum_ret*100:+.1f}%, WinRate: {r.win_rate:.0%}')
"

How it works: Every hour, rank stocks by return-since-open. Long the 10 biggest losers (they revert up), short the 10 biggest gainers (they revert down). Close at end of day.

Alpha Factor Library (43 factors)

Category Count Examples
Momentum 12 12-1 month, 5d, volume-weighted, acceleration
Reversal 5 1d/5d reversal, distance from high/low, z-score
Volatility 9 Realized vol, downside vol, ATR, vol-of-vol
Volume 8 Relative volume, Amihud illiquidity, money flow
Price Action 6 Gap, close location, bar range, body ratio
Technical 8 RSI, MACD, Bollinger %B, Stochastic, EMA cross
Interactions 12 momentum x vol, reversal x volume, RSI x momentum

Deploy to Alpaca Paper Trading

# 1. Install
pip install -e ".[alpaca]"

# 2. Configure .env (copy from .env.example)
cp .env.example .env
# Edit .env with your Alpaca API keys from https://app.alpaca.markets

# 3. Fetch intraday data
python -c "
from dotenv import load_dotenv; load_dotenv('.env')
import os
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame
from datetime import datetime
dc = StockHistoricalDataClient(os.environ['ALPACA_API_KEY'], os.environ['ALPACA_SECRET_KEY'])
bars = dc.get_stock_bars(StockBarsRequest(
    symbol_or_symbols=['AAPL','MSFT','NVDA'], timeframe=TimeFrame.Hour,
    start=datetime(2024,6,1), end=datetime(2025,3,15)))
print(f'Fetched {sum(len(bars[s]) for s in bars.data)} bars')
"

# 4. Launch paper trading
python scripts/paper_trade.py --mode alpaca --tickers AAPL,MSFT,GOOGL,AMZN,META,NVDA,TSLA,JPM,V,JNJ

Dependencies

pip install -e .            # Core: torch, polars, xgboost, lightgbm, yfinance, ...
pip install -e ".[rl]"      # + stable-baselines3, gymnasium
pip install -e ".[paper]"   # + FastAPI, uvicorn (paper trading dashboard)
pip install -e ".[alpaca]"  # + alpaca-py (Alpaca paper/live trading)
pip install -e ".[all]"     # Everything

Requires Python >= 3.11, PyTorch >= 2.2, Polars >= 1.0.

Verified Results (Real Data)

XGBoost on 10 US stocks (2022-2025): Sharpe 2.00, ARR 42.2%, MDD -15.7%
LSTM on 10 US stocks (2022-2025):    Sharpe 2.43, ARR 50.5%, MDD -6.4%

Paper Reference

@inproceedings{hu2025fintsb,
  title={FinTSB: A Comprehensive and Practical Benchmark for Financial Time Series Forecasting},
  author={Hu, Yifan and Li, Yuante and Liu, Peiyuan and Zhu, Yuxia and Li, Naiqi and Dai, Tao
          and Xia, Shu-tao and Cheng, Dawei and Jiang, Changjun},
  year={2025},
  note={arXiv:2502.18834}
}

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages