Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 2.07 KB

codebase.md

File metadata and controls

42 lines (30 loc) · 2.07 KB

Backtest Engine Codebase

README.md is the user-facing contract. This note is a compact map of the implementation.

engine/
  data_loader.py  validated UTC OHLCV loading and conservative resampling
  broker.py       one-position, next-open order simulation and trade ledger
  reporter.py     report, equity curve, and JSON export
  core.py         event loop and dynamic strategy loading
strategies/base.py  optional base class for Strategy modules
scripts/test_strategy.py  runnable example strategy
examples/            committed small CSV for the README quick start
tests/               standard-library regression suite
main.py              CLI entry point

Event order

For each source or resampled candle, the engine:

  1. Marks the broker to the candle open, settles any funding due since the previous open on the position held (when funding_rates is configured), and fills any order queued by the prior candle.
  2. Marks the broker to the candle close.
  3. Calls Strategy.update(candle), which may queue one order for the next open.
  4. Records marked-to-market equity at the close.

The engine cancels an order queued on the final candle, because there is no next open. It then liquidates an existing position at that final close so reports contain realized final equity.

Design boundaries

The broker intentionally supports a single net position and market orders only. It validates positive finite prices, quantities, balances, and leverage; slippage and commission are non-negative decimal rates. Position P&L is net of both entry and exit fees.

The broker checks entry affordability and supports optional equity-based sizing and maintenance liquidation on observed opens and closes. The README defines the timing and accounting rules. Each repeat run starts with a fresh broker, strategy instance and reporter.

Development

python -m pip install -e .
python -m unittest discover -v

Keep changes small and add a regression test whenever a simulation rule changes. The test suite is deliberately based on unittest, so no development-only test dependency is required.