Skip to content

Latest commit

Β 

History

65 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Multifactor Portfolio Quantitative Trading Strategy

Python Version License: MIT Code Style: Black

An advanced, production-grade Two-Layered Machine Learning & Quantitative Portfolio Strategy designed for cryptocurrency futures markets. The strategy combines cross-sectional alpha return prediction with a global macro regime risk control overlay to generate market-neutral alpha while protecting capital during market stress.


🌟 Architecture Overview

The strategy operates using a modular Two-Layered Architecture:

flowchart TD
    subgraph Layer 1: Alpha Return Predictor
        A[25 Asset-Specific Features: RSI, WMA, Flow, Carry, OI] --> B[Cross-Sectional Percentile Rank & Target Demeanization]
        B --> C[Machine Learning Model: XGBoost / LightGBM]
        C --> D[Cross-Sectional Long-Short Quantile Ranking]
    end

    subgraph Layer 2: Macro Regime Risk Overlay
        E[85 Global Macro & Volatility Features: DVOL, VIX, DXY, FNG, SPY] --> F[Stress Regime Classifier: VIX > 22, FNG < 30, DVOL > 65]
    end

    D --> G[Inverse Volatility Weighting & Stress Deleveraging]
    F -->|High Stress: Multiply Exposure by 0.5| G
    G --> H[Final Execution Portfolio Weights]
Loading

1. Layer 1: Alpha Return Prediction (Asset-Level ML Model)

  • Universe: Top 40 liquid Binance Futures cryptocurrencies.
  • Features: 25 coin-level momentum, liquidity, funding carry, and margin risk indicators computed across 5 lookback windows ${7, 14, 30, 60, 90}$ days.
  • Normalization: Daily Cross-Sectional Percentile Ranking $[0, 1]$ and Market Beta Target Demeanization ($y'{i,t} = y{i,t} - \bar{y}_t$).
  • Models: XGBoost & LightGBM Regressors.

2. Layer 2: Global Macro Regime Risk Control (Macro Overlay)

  • Macro Inputs: Deribit Implied Volatility (DVOL BTC/ETH), CBOE VIX, DXY Index, SPY, Crypto Fear & Greed Index, and Total Stablecoin Market Cap.
  • Dynamic Exposure Control: Automatically scales portfolio position sizes by stress_multiplier = 0.5 during market-wide panic regimes (VIX > 22, FNG < 30, DVOL > 65).

πŸ“ Repository Structure

multifactor_portfo/
β”œβ”€β”€ .github/
β”‚   └── workflows/ci.yml           # Automated CI (canonical tests/ suite)
β”œβ”€β”€ data/                          # Cached macro + funding CSVs (gitignored)
β”œβ”€β”€ src/multifactor_mlops/         # CANONICAL pipeline (v4)
β”‚   β”œβ”€β”€ config/                    # Strict 1:1 Pydantic schema + loader
β”‚   β”œβ”€β”€ data/                      # OHLCV/macro/funding loaders
β”‚   β”œβ”€β”€ features/                  # Asset features, macro overlay, panel, preprocessor
β”‚   β”œβ”€β”€ labels/                    # Open-to-open labels + purge
β”‚   β”œβ”€β”€ optimization/              # Stage 1 ML tuning, Stage 2 strategy tuning, folds
β”‚   β”œβ”€β”€ pipelines/                 # OOF generation, single-touch OOS evaluation, fit_final
β”‚   β”œβ”€β”€ portfolio/                 # Weights constructor + shared stress overlay
β”‚   β”œβ”€β”€ backtest/                  # QuantBT native walk-forward adapter + runner
β”‚   β”œβ”€β”€ tracking/                  # MLflow/run manifests
β”‚   └── register/                  # Production bundle export
β”œβ”€β”€ tests/                         # Canonical test suite (phase1-3 + v4)
β”œβ”€β”€ scratch/                       # Isolated verification experiments
β”œβ”€β”€ artifacts/                     # Locked tuned configs + final OOS results
β”œβ”€β”€ parameters.json                # Master config (immutable, tuned params live in artifacts/)
β”œβ”€β”€ .env.example                   # Sample environment variable file
β”œβ”€β”€ .gitignore                     # Git ignore rules
β”œβ”€β”€ .pre-commit-config.yaml        # Pre-commit hooks for linting & formatting
β”œβ”€β”€ CONTRIBUTING.md                # Contribution guidelines
β”œβ”€β”€ Dockerfile                     # Bundle verification container
β”œβ”€β”€ LICENSE                        # MIT License
└── README.md                      # Strategy documentation

βš™οΈ Quick Start & CLI Usage

Prerequisites

  • Python 3.11 or 3.12
  • Poetry dependency manager

Installation

git clone https://github.com/your-org/multifactor_portfo.git
cd multifactor_portfo
poetry install

1. Stage 1 β€” ML Hyperparameter Tuning (dev window only, rank IC objective)

poetry run python -m src.multifactor_mlops.optimization.stage1_ml_tuning --trials 30
# -> artifacts/model_config.json (locked)

2. Generate OOF Predictions (dev window)

poetry run python -m src.multifactor_mlops.pipelines.generate_oof_predictions
# -> artifacts/oof_predictions.csv + fold_metrics.json

3. Stage 2 β€” Strategy & Risk Overlay Tuning (dev window only, QuantBT backtest)

poetry run python -m src.multifactor_mlops.optimization.stage2_strategy_tuning --trials 15
# -> artifacts/strategy_config.json (locked)

4. Single-Touch Outer OOS Evaluation (2024-01-01 β†’)

poetry run python -m src.multifactor_mlops.pipelines.evaluate_final --oos-start 2024-01-01
# -> artifacts/final_oos_metrics.json + final_oos_report.md

5. Export Production Bundle (model + preprocessor + overlay)

poetry run python -m src.multifactor_mlops.register.export_bundle --training-cutoff 2023-12-31
# -> artifacts/model_bundle/

6. Run Unit Tests

poetry run pytest tests/ -q

πŸ› οΈ Configuration (parameters.json)

Key strategy parameters are dynamically loaded from parameters.json:

{
  "features": {
    "model_type": "xgboost",
    "learning_rate": 0.02,
    "max_depth": 3,
    "num_boost_round": 80,
    "colsample_bytree": 0.3
  },
  "dataset": {
    "asset_class": "crypto",
    "universe_name": "binance_daily",
    "top_n_symbols": 40,
    "quantbt_repo_path": "/root/bobby/pool_alpha/quantbt"
  },
  "training": {
    "split_mode": "walk_forward_2024",
    "fee": 0.0005,
    "allocation_cap": 0.1,
    "lag": 1,
    "quantiles": 40,
    "inverse_vol_period": 120,
    "stress_vix_threshold": 22.0,
    "stress_fng_threshold": 30.0,
    "stress_dvol_threshold": 65.0,
    "stress_multiplier": 0.5
  }
}

🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details on code style, branch naming, and pull request procedures.


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

An advanced, production-grade Two-Layered Machine Learning & Quantitative Portfolio Strategy designed for cryptocurrency futures markets. The strategy combines cross-sectional alpha return prediction with a global macro regime risk control overlay to generate market-neutral alpha while protecting capital during market stress.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages