Status: Validated and Extracted
Version: v0 (engine_validated_v0)
A production-grade backtesting engine for Binance USDⓈ-M Futures with realistic execution modeling, risk controls, and comprehensive validation gates.
This is the core backtesting engine extracted from a larger strategy project. It provides:
- ✅ Multi-symbol backtesting (15m base timeframe)
- ✅ Realistic fill modeling (slippage, fees, funding)
- ✅ Risk controls (ES guardrails, margin checks, loss halts, beta caps)
- ✅ Liquidity diagnostics utilities (generic thresholds; optional for strategies)
- ✅ Comprehensive validation harness (oracle module, baselines, parity checks)
- ✅ Full accounting and metrics generation
- ❌ Strategy logic (TREND, RANGE, SQUEEZE modules removed)
- ❌ Regime classification (strategy-specific logic removed)
- ❌ Master side logic (strategy-specific logic removed)
The engine is strategy-agnostic and only supports oracle_mode for validation/testing. For production strategies, provide signal generators externally.
# Clone repository
git clone <repo-url>
cd engine_core
# Install dependencies
pip install -r requirements.txt
# Run validation tests
pytest tests/ -vpython scripts/run_example_oracle.pyThis runs a minimal example using the Oracle module with always_long mode on toy UP market data.
python scripts/run_baselines.py --data-path ../data/ --start-date 2021-06-01 --end-date 2021-09-01python scripts/validate_data_integrity.py --data-path ../data/The engine includes a comprehensive validation harness:
- Phase 1: Data Integrity - Timestamp monotonicity, gaps, NaNs, OHLC sanity
- Phase 2: Accounting Invariants - Equity identity, position conservation, PnL conservation
- Phase 3: Toy Oracles - Deterministic signal validation on synthetic markets
- Phase 4: Baseline Benchmarks - Buy & Hold, Flat, Random strategies
- Phase 5: Cross-Backtester Parity - Independent PnL replay validation
See docs/AUDIT_REPORT.md for validation evidence.
engine_core/
├── src/ # Core engine components
│ ├── engine.py # Main BacktestEngine orchestrator
│ ├── portfolio/ # Portfolio state management
│ ├── risk/ # Risk controls (ES, margin, loss halts, beta)
│ ├── execution/ # Fill model, order management, sequencing
│ ├── data/ # Data loading and schema
│ ├── indicators/ # Technical indicators
│ ├── liquidity/ # Liquidity diagnostics utilities
│ ├── reporting.py # Metrics generation
│ └── modules/ # Oracle module (validation only)
├── config/ # Parameter system
│ ├── params_loader.py
│ ├── base_params.json
│ └── example_overrides/
├── scripts/ # Validation and utility scripts
├── tests/ # Validation tests
└── docs/ # Documentation
For detailed architecture and usage information, see docs/ENGINE_OVERVIEW.md.
The engine is strategy-agnostic. To add a strategy:
- Create a strategy module that generates signals (similar to
src/modules/oracle.py) - Integrate signal generation into your strategy runner
- Pass signals to the engine via a callback or signal queue
Note: The engine currently only supports oracle_mode for validation. Full strategy integration requires engine modifications to accept external signal generators.
Engine parameters are defined in config/base_params.json. Key sections:
general: Capital, fees, venue settingsuniverse: Symbol selection and refresh rulesrisk: ES guardrails, margin, loss halts, beta controlsslippage_costs: Fill model parametersliquidity_regimes: VACUUM/THIN detection thresholds
Use ParamsLoader to override parameters:
from engine_core.config.params_loader import ParamsLoader
params = ParamsLoader(overrides={
'general': {'oracle_mode': 'always_long'},
'es_guardrails': {'es_cap_of_equity': 1.0}
}, strict=False)# Run all validation tests
pytest tests/ -v
# Run specific validation phase
pytest tests/test_toy_oracles.py -v # Phase 3
pytest tests/test_baselines_smoke.py -v # Phase 4
pytest tests/test_accounting_invariants_toy.py -v # Phase 2
# Run validation scripts
python scripts/validate_data_integrity.py --data-path ../data/
python scripts/run_baselines.py --data-path ../data/ --start-date 2021-06-01 --end-date 2021-09-01The engine expects 15m OHLCV data in CSV format:
data/
├── BTCUSDT_15m.csv
├── ETHUSDT_15m.csv
└── ...
CSV format: timestamp,open,high,low,close,volume
MIT License (or as specified)
This is a standalone, strategy-agnostic backtesting engine. For strategy development, create your own strategy modules externally and integrate them with the engine.
✅ Data Integrity: PASS
✅ Accounting Invariants: PASS
✅ Toy Oracles: PASS (7/7 tests)
✅ Baseline Benchmarks: PASS
✅ Cross-Backtester Parity: PASS
See docs/AUDIT_REPORT.md for detailed evidence.