Skip to main content

State-of-the-art event-driven backtesting engine for quantitative trading

Project description

ml4t-backtest

Python 3.11+ PyPI License: MIT

Event-driven backtesting engine for quantitative trading strategies with realistic execution modeling.

Part of the ML4T Library Ecosystem

This library is one of five interconnected libraries supporting the machine learning for trading workflow described in Machine Learning for Trading:

ML4T Library Ecosystem

Each library addresses a distinct stage: data infrastructure, feature engineering, signal evaluation, strategy backtesting, and live deployment.

What This Library Does

Backtesting requires accurate simulation of order execution, position tracking, and risk management. ml4t-backtest provides:

  • Event-driven architecture with point-in-time correctness (no look-ahead bias)
  • Exit-first order processing matching real broker behavior
  • Configurable execution modes (same-bar or next-bar fills)
  • Position-level risk rules (stop-loss, take-profit, trailing stops)
  • Portfolio-level constraints (max positions, drawdown limits)
  • Cash and margin account policies

The same Strategy class used in backtesting works unchanged in ml4t-live for production deployment.

ml4t-backtest Architecture

Installation

pip install ml4t-backtest

Quick Start

from ml4t.backtest import Engine, Strategy, BacktestConfig, DataFeed
from ml4t.backtest.risk import StopLoss, TakeProfit, RuleChain

class TrendFollowing(Strategy):
    def __init__(self, fast=10, slow=30):
        self.fast = fast
        self.slow = slow

    def on_data(self, timestamp, data, context, broker):
        close = data["close"]
        fast_ma = close.rolling(self.fast).mean().iloc[-1]
        slow_ma = close.rolling(self.slow).mean().iloc[-1]

        position = broker.get_position("SPY")

        if fast_ma > slow_ma and position is None:
            broker.submit_order("SPY", quantity=100, side="BUY")
        elif fast_ma < slow_ma and position is not None:
            broker.close_position("SPY")

config = BacktestConfig(
    initial_cash=100_000,
    commission_rate=0.001,
)

feed = DataFeed(price_data)
engine = Engine(feed, TrendFollowing(), config)
result = engine.run()

print(f"Total Return: {result.total_return:.2%}")
print(f"Sharpe Ratio: {result.metrics['sharpe_ratio']:.2f}")

Risk Management

Position-level exit rules:

from ml4t.backtest.risk import StopLoss, TakeProfit, TrailingStop, RuleChain

class MyStrategy(Strategy):
    def on_start(self, broker):
        broker.set_position_rules(RuleChain([
            StopLoss(pct=0.05),
            TakeProfit(pct=0.15),
            TrailingStop(pct=0.03),
        ]))

Portfolio-level controls:

from ml4t.backtest.risk import MaxPositions, MaxDrawdown, DailyLossLimit

Execution Modes

from ml4t.backtest import ExecutionMode, StopFillMode

# Same-bar fills (VectorBT style)
config = BacktestConfig(
    execution_mode=ExecutionMode.SAME_BAR,
    stop_fill_mode=StopFillMode.STOP_PRICE,
)

# Next-bar fills (Backtrader style)
config = BacktestConfig(
    execution_mode=ExecutionMode.NEXT_BAR,
    stop_fill_mode=StopFillMode.STOP_PRICE,
)

Commission and Slippage

from ml4t.backtest import PercentCommission, PercentSlippage

config = BacktestConfig(
    commission_model=PercentCommission(rate=0.001),
    slippage_model=PercentSlippage(rate=0.0005),
)

Multi-Asset Support

class RankingStrategy(Strategy):
    def on_data(self, timestamp, data, context, broker):
        returns = data["close"].pct_change(20)
        ranked = returns.iloc[-1].sort_values(ascending=False)

        # Long top 10
        for asset in ranked.head(10).index:
            if broker.get_position(asset) is None:
                broker.submit_order(asset, quantity=100, side="BUY")

Validation

The library is validated against VectorBT Pro, Backtrader, and Zipline:

  • 119,000+ trades verified trade-by-trade across frameworks
  • 500 assets x 10 years (2,520 bars) stress testing
  • 100% PnL match on common execution scenarios

See validation/README.md for test methodology.

Release-gate commands:

# Fast parity contract gate (scenario 01 across vectorbt/backtrader/zipline)
ML4T_COMPARISON_INPROC=1 uv run pytest tests/contracts/test_cross_engine_contracts.py -q

# Full correctness runner (selected scenarios)
python validation/run_all_correctness.py --framework vectorbt_oss --scenarios 01,03,05,09
python validation/run_all_correctness.py --framework backtrader --scenarios 01,03,05,09
python validation/run_all_correctness.py --framework zipline --scenarios 01,03,05,09

Technical Characteristics

  • Event-driven: Each bar processes sequentially with exit-first logic
  • Point-in-time: No access to future data within strategy callbacks
  • Configurable fills: Match behavior of different backtesting frameworks
  • Parquet export: Results serializable for analysis with ml4t-diagnostic

Related Libraries

  • ml4t-data: Market data acquisition and storage
  • ml4t-engineer: Feature engineering and technical indicators
  • ml4t-diagnostic: Signal evaluation and statistical validation
  • ml4t-live: Live trading with broker integration

Development

git clone https://github.com/applied-ai/ml4t-backtest.git
cd ml4t-backtest
uv sync
uv run pytest tests/ -q
uv run ty check

Known Limitations

See LIMITATIONS.md for documented assumptions:

  • Partial fills not supported (all-or-nothing)
  • No intrabar stop simulation (uses bar OHLC)
  • Calendar overnight sessions require configuration

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ml4t_backtest-0.1.0a8.tar.gz (221.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ml4t_backtest-0.1.0a8-py3-none-any.whl (135.2 kB view details)

Uploaded Python 3

File details

Details for the file ml4t_backtest-0.1.0a8.tar.gz.

File metadata

  • Download URL: ml4t_backtest-0.1.0a8.tar.gz
  • Upload date:
  • Size: 221.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ml4t_backtest-0.1.0a8.tar.gz
Algorithm Hash digest
SHA256 194b2ab2a217dbf7c4e2c0f921775b8e30cbae83489173a2c6262d7e05d09051
MD5 93d3af06a06ad4d0337f38ed3f9611ff
BLAKE2b-256 207884e259ac662697396eb6e1cde3161f8eb97097b51d20fcb7a91c4ebf77d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for ml4t_backtest-0.1.0a8.tar.gz:

Publisher: release.yml on ml4t/backtest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ml4t_backtest-0.1.0a8-py3-none-any.whl.

File metadata

  • Download URL: ml4t_backtest-0.1.0a8-py3-none-any.whl
  • Upload date:
  • Size: 135.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ml4t_backtest-0.1.0a8-py3-none-any.whl
Algorithm Hash digest
SHA256 474c87ca0a9d4737516380fa731b045e38b62b0172397dfc264a88fabe30670e
MD5 58928e24285c6b1018415b0010aaec0e
BLAKE2b-256 2c4c1106cb91fecf66554c2e67621371ebd678f93d3860f884fdc5596696fffd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ml4t_backtest-0.1.0a8-py3-none-any.whl:

Publisher: release.yml on ml4t/backtest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page