The reference backtest¶
One strategy, one data file, one configuration, and the numbers the engine produces for them. It is the check to run against any other engine, and the fixed point every change to the execution model has to move on purpose: a C++ test and a Python test pin every value on this page, and the runnable example below fails when they drift.
What is fixed¶
| Data | docs/examples/data/btcusdt_1m.csv: 3000 one-minute bars, millisecond timestamps, timestamp,open,high,low,close,volume |
| Strategy | SMA(10) and SMA(40) over the close; long 0.01 while the fast average is above the slow one, flat otherwise; no stops |
| Entry | market_buy(0.01) from the bar callback when flat and fast is above slow |
| Exit | close_position() from the bar callback when long and fast is below slow |
| Fees | taker 0.0004 of notional on every fill (fee_rate=0.0004) |
| Capital | 10 000 |
| Replay | run_bars over the arrays, nanosecond timestamps, one symbol |
How a bar run executes¶
These are the rules the numbers below follow. They are the behaviour of BacktestRunner.run_bars and SimulatedExecutor; the tests hold them.
- A bar is walked before the strategy sees it. For each bar the simulator takes the open, then the low, then the high, then the close into its market state. Resting stops and take-profits match against the intrabar low and high, not only the close.
- The strategy is called once per bar, after the walk.
on_barruns with the bar fully in the past, and every price it can react to is a price that has already printed. - An order from the callback fills at the next bar's open. While the callback runs, every order it submits is held; the next bar for that symbol releases it at its open step, where a market order takes the open price. Nothing fills on the bar that produced the signal, and an order submitted from the last bar's callback never fills.
- Fill timestamps are bar end times. The runner moves its clock to a bar's end time before walking it, so a fill in bar N carries bar N's end time, not the signal bar's.
- Order prices are checked against the symbol's tick. A limit price, trigger or trailing distance off the tick is rejected at submit, as a venue rejects it. This run sends market orders only, so the check never fires here.
- Fees are charged per fill on notional. A round trip pays the rate twice: once on the entry notional and once on the exit notional.
- A trade is an entry fill matched with the exit fill that flattens it.
pnlis price difference times quantity, before fees;feeis the two fills' fees;net_pnlacross the run istotal_pnlminustotal_fees, andfinal_capitalis the initial capital plusnet_pnl. - A position open when the data ends is not a trade. It is not in
trades()or the equity curve, andnet_pnlleaves out its unrealized P&L but carries its entry fee throughtotal_fees. The stats value it separately:unrealized_pnlmarks it at the last bar's close, andfinal_equityisinitial_capital + net_pnl + unrealized_pnl. This run ends flat, so herenet_pnlequals the sum of the trades'pnl - fee,unrealized_pnlis 0 andfinal_equityequalsfinal_capital. See A position open at the end.
In this run SMA(40) is ready on the 40th close (bar index 39). The buy that close produces fills at the open of bar 40, 75822.85, and the trade's entry_time_ns is bar 40's end time.
The numbers¶
| Statistic | Value |
|---|---|
total_trades |
47 |
winning_trades / losing_trades |
16 / 31 |
gross_profit / gross_loss |
28.0608 / 42.7754 |
total_pnl |
-14.7146 |
total_fees |
28.37314883 |
net_pnl |
-43.08774883 |
final_capital |
9956.91225117 |
return_pct |
-0.4308774883 |
| Trade | Entry time (ns) | Entry | Exit time (ns) | Exit | pnl |
fee |
|---|---|---|---|---|---|---|
| first | 1776609420000000000 | 75822.85 | 1776611280000000000 | 75909.76 | 0.8691 | 0.60693044 |
| last | 1776781140000000000 | 76185.83 | 1776783420000000000 | 76005.21 | -1.8062 | 0.60876416 |
The strategy loses money on this file. That is not what the page is for: the figures are a fixed point, not a result.
The run¶
The example loads the file, runs the strategy and compares what it gets with the table above; it exits non-zero on any difference.
"""
The reference backtest: one strategy, one data file, pinned numbers.
Everything here is fixed so the output is a number to check an engine
against, not a result to interpret: SMA(10) over SMA(40) on the close of
one-minute bars, long 0.01 when the fast average is above the slow one, flat
otherwise, taker fee 0.0004 on every fill, initial capital 10 000. The
expected values below are the ones the engine produces today; the script
fails if they move.
Usage:
cd /path/to/lrvx
PYTHONPATH=build/python python3 docs/examples/python_reference_backtest.py
"""
import os
import numpy as np
import lrvx
DATA = os.path.join(os.path.dirname(__file__), "data", "btcusdt_1m.csv")
EXPECTED = {
"total_trades": 47,
"winning_trades": 16,
"losing_trades": 31,
"gross_profit": 28.0608,
"gross_loss": 42.7754,
"total_pnl": -14.7146,
"total_fees": 28.37314883,
"net_pnl": -43.08774883,
"final_capital": 9956.91225117,
"return_pct": -0.4308774883,
}
# The first and the last closed trade. entry_time_ns is the end of the bar the
# entry filled in, which is the bar after the one whose close produced the
# signal; the entry price is that bar's open.
FIRST_TRADE = {"entry_time_ns": 1776609420000000000, "entry_price": 75822.85,
"exit_time_ns": 1776611280000000000, "exit_price": 75909.76,
"pnl": 0.8691, "fee": 0.60693044}
LAST_TRADE = {"entry_time_ns": 1776781140000000000, "entry_price": 76185.83,
"exit_time_ns": 1776783420000000000, "exit_price": 76005.21,
"pnl": -1.8062, "fee": 0.60876416}
class SmaCross(lrvx.Strategy):
def __init__(self, symbols, fast=10, slow=40, qty=0.01):
super().__init__(symbols)
self.fast = lrvx.SMA(fast)
self.slow = lrvx.SMA(slow)
self.qty = qty
def on_bar(self, ctx, bar):
f = self.fast.update(bar.close)
s = self.slow.update(bar.close)
if f is None or s is None:
return
if f > s and ctx.is_flat():
self.market_buy(self.qty)
elif f < s and ctx.is_long():
self.close_position()
def load_bars(path):
raw = np.loadtxt(path, delimiter=",", skiprows=1)
start_ns = raw[:, 0].astype(np.int64) * 1_000_000
return {
"start_time_ns": start_ns,
"end_time_ns": start_ns + 60_000_000_000,
"open": raw[:, 1],
"high": raw[:, 2],
"low": raw[:, 3],
"close": raw[:, 4],
"volume": raw[:, 5],
}
def run():
bars = load_bars(DATA)
registry = lrvx.SymbolRegistry()
btc = registry.add_symbol("exchange", "BTCUSDT", tick_size=0.01)
bt = lrvx.BacktestRunner(registry, fee_rate=0.0004, initial_capital=10_000.0)
bt.set_strategy(SmaCross([btc]))
stats = bt.run_bars(symbol="BTCUSDT", **bars)
return bars, stats, bt.trades()
def main():
bars, stats, trades = run()
mismatches = []
for key, want in EXPECTED.items():
got = stats[key]
if abs(got - want) > 1e-9:
mismatches.append(f"{key}: expected {want}, got {got}")
first = {k: trades[k][0].item() for k in FIRST_TRADE}
last = {k: trades[k][-1].item() for k in LAST_TRADE}
for name, got, want in (("first trade", first, FIRST_TRADE), ("last trade", last, LAST_TRADE)):
for k in want:
if abs(float(got[k]) - float(want[k])) > 1e-9:
mismatches.append(f"{name} {k}: expected {want[k]}, got {got[k]}")
# The entry fills at the open of the bar after the signal bar: the slow
# average is ready on the 40th bar, so the first buy fills on the 41st.
if abs(float(bars["open"][40]) - FIRST_TRADE["entry_price"]) > 1e-9:
mismatches.append("first entry price is not the open of bar 40")
if int(bars["end_time_ns"][40]) != FIRST_TRADE["entry_time_ns"]:
mismatches.append("first entry time is not the end of bar 40")
print(f"bars: {len(bars['close'])}")
for key in EXPECTED:
print(f" {key:16s} {stats[key]}")
print(f" first trade {first}")
print(f" last trade {last}")
if mismatches:
raise SystemExit("reference backtest moved:\n " + "\n ".join(mismatches))
print("reference backtest matches")
if __name__ == "__main__":
main()
The same strategy in C++ runs through BacktestRunner::runBars and is pinned to the same values in tests/test_reference_backtest.cpp:
#include "lrvx/indicator/sma.h"
#include "lrvx/strategy/strategy.h"
class SmaCross : public lrvx::Strategy
{
public:
using Strategy::Strategy;
protected:
void onSymbolBar(SymbolContext& ctx, const BarEvent& ev) override
{
const double close = ev.bar.close.toDouble();
_fast.update(close);
_slow.update(close);
if (!_fast.ready() || !_slow.ready())
{
return;
}
const double f = _fast.value();
const double s = _slow.value();
if (f > s && ctx.position.raw() == 0)
{
emitMarketBuy(ev.symbol, Quantity::fromDouble(0.01));
}
else if (f < s && ctx.position.raw() > 0)
{
emitClosePosition(ev.symbol);
}
}
private:
lrvx::indicator::SMA _fast{10};
lrvx::indicator::SMA _slow{40};
};
Checking another engine against it¶
Run the same rules on the same file: fill at the next bar's open, fee on every fill's notional, trades closed by the fill that flattens. Compare the trade list before the statistics: a mismatch in total_trades or in the first entry's time and price locates the convention that differs, where a mismatch in return_pct alone does not.
The same strategy on a tape¶
These rules are the rules of bar data, where a bar is all there is. On a recorded tape the same bar strategy can be run with the fills of the tape instead: run_tape_bars_on_ticks delivers the bars run_tape_bars builds, and matches every order on the tape's prints. The two runs differ in exactly the places the bar hides:
- Entry. An order from bar n's callback fills at the first trade stamped at or after the end of bar n, instead of at bar n+1's open. Both are the first price after the decision; on bars the open stands in for it.
- Stops. A stop that triggers inside a bar fills at the print that crossed it, instead of at the bar's low (high for a buy stop). Never better than its trigger, in either mode.
- Fill timestamps. A fill carries the time of the trade it matched, not a bar's end time.
- Latency. A submit latency moves a fill to the first print at or after the bar's end plus the latency, milliseconds later, instead of to a later bar's open.
The bars, and so every decision the strategy makes, are the same in both. The difference in the results is the cost of filling at bar prices; Decide on bars, fill on ticks has the full set of rules.