Skip to content

Backtesting

Running a backtest

bash
matchstick run --strategy src/strategy.json --data data/sample_data.csv --capital 100000

Flags

FlagRequiredDefaultDescription
--strategyYes-Path to strategy file
--dataNogeneratedPath to CSV market data
--modeNobacktestExecution mode
--capitalNo100000Initial capital ($)

Without data

If you omit --data, the engine generates 1440 bars of synthetic data (30 days of 1-minute bars) using deterministic sine/cosine price movement.

Strategy logic

v0.1.0 uses a hardcoded mean reversion strategy:

  1. Calculate SMA(20) on the most recent 20 bars
  2. Entry: Buy when price drops 1% below SMA
  3. Exit: Sell when price returns to SMA
  4. Position size determined by FixedRisk method (1% risk per trade)
  5. Risk manager validates each trade against limits

Custom strategy loading is planned for a future release.

Data format

CSV with header row:

timestamp,symbol,open,high,low,close,volume
2024-01-02 09:30:00,AAPL,185.50,186.20,185.10,185.80,1500000

Supported timestamp formats:

  • 2024-01-02T09:30:00Z (ISO 8601)
  • 2024-01-02 09:30:00
  • 2024-01-02

Output metrics

MetricDescription
Final P&LAbsolute dollar profit/loss
ReturnPercentage return on initial capital
Total TradesNumber of completed round trips
Win RatePercentage of profitable trades
Sharpe RatioAnnualized risk-adjusted return (252 trading days)
Max DrawdownLargest peak-to-trough decline

Components used

The backtest engine uses all 5 library crates:

  • core-types: OhlcvBar, Position, Timeframe
  • indicators: IndicatorEngine for SMA calculation
  • orders: OrderManager (initialized, used for tracking)
  • risk: RiskManager for position sizing and trade validation
  • strategy: StrategyManager for strategy lifecycle

Built for traders who value data provenance.