backtest-engine 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/.gitattributes +1 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/.github/workflows/ci.yml +16 -8
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/Cargo.lock +1 -1
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/Cargo.toml +1 -1
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/PKG-INFO +73 -18
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/README.md +72 -17
- backtest_engine-0.2.0/data/spy_tlt_daily.csv +5029 -0
- backtest_engine-0.2.0/data/spy_tlt_daily.meta.json +24 -0
- backtest_engine-0.2.0/data/spy_tlt_daily.sha256 +1 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/docs/case_study.html +1 -1
- backtest_engine-0.2.0/docs/case_study.sha256 +1 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/docs/index.html +10 -7
- backtest_engine-0.2.0/docs/multi_asset.html +169 -0
- backtest_engine-0.2.0/docs/multi_asset.sha256 +1 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/pyproject.toml +2 -1
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/__init__.py +4 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/cli.py +22 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/strategies.py +87 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/reports/example.html +1 -1
- backtest_engine-0.2.0/reports/expected.sha256 +1 -0
- backtest_engine-0.2.0/scripts/build_multi_asset_case.py +537 -0
- backtest_engine-0.2.0/scripts/fetch_multi_asset.py +179 -0
- backtest_engine-0.2.0/scripts/verify_multi_asset_case.py +42 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/engine.rs +108 -12
- backtest_engine-0.2.0/tests/test_multi_asset.py +139 -0
- backtest_engine-0.1.0/docs/case_study.sha256 +0 -1
- backtest_engine-0.1.0/reports/expected.sha256 +0 -1
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/.gitignore +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/LICENSE +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/data/sample.csv +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/data/spy_daily.csv +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/data/spy_daily.meta.json +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/data/spy_daily.sha256 +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/docs/benchmark.json +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/docs/benchmark.md +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/examples/demo_lookahead.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/examples/run_example.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/__main__.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/_backtest_engine.pyi +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/detector.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/py.typed +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/report.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/python/backtest_engine/walkforward.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/rust-toolchain.toml +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/benchmark_real_data.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/build_case_study.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/fetch_real_data.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/generate_data.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/verify_case_study.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/scripts/verify_report.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/costs.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/error.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/lib.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/metrics.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/queue.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/report.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/store.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/time.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/src/types.rs +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/__init__.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/helpers.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/test_detector.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/test_real_data.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/test_report.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/test_store.py +0 -0
- {backtest_engine-0.1.0 → backtest_engine-0.2.0}/tests/test_walkforward.py +0 -0
|
@@ -89,11 +89,11 @@ jobs:
|
|
|
89
89
|
path: reports/example.html
|
|
90
90
|
|
|
91
91
|
case-study:
|
|
92
|
-
name: Real-data case
|
|
92
|
+
name: Real-data case studies ${{ matrix.os }}
|
|
93
93
|
strategy:
|
|
94
94
|
fail-fast: false
|
|
95
95
|
matrix:
|
|
96
|
-
os: [ubuntu-latest, windows-latest]
|
|
96
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
97
97
|
runs-on: ${{ matrix.os }}
|
|
98
98
|
steps:
|
|
99
99
|
- uses: actions/checkout@v7
|
|
@@ -106,14 +106,22 @@ jobs:
|
|
|
106
106
|
run: python -m pip install --upgrade pip
|
|
107
107
|
- name: Build extension
|
|
108
108
|
run: python -m pip install .
|
|
109
|
-
- name: Rebuild
|
|
109
|
+
- name: Rebuild single-asset report
|
|
110
110
|
run: python scripts/build_case_study.py
|
|
111
|
-
- name: Verify
|
|
111
|
+
- name: Verify single-asset hash
|
|
112
112
|
run: python scripts/verify_case_study.py
|
|
113
|
-
- name:
|
|
113
|
+
- name: Rebuild multi-asset report
|
|
114
|
+
run: python scripts/build_multi_asset_case.py
|
|
115
|
+
- name: Verify multi-asset hash
|
|
116
|
+
run: python scripts/verify_multi_asset_case.py
|
|
117
|
+
- name: Prove the subtle single-asset leak is detected
|
|
114
118
|
run: python -m backtest_engine check --strategy full_sample_zscore --expect-detected
|
|
115
|
-
- name:
|
|
119
|
+
- name: Prove the multi-asset trap is detected
|
|
120
|
+
run: python -m backtest_engine check --strategy multi_asset_trap --data data/spy_tlt_daily.csv --expect-detected
|
|
121
|
+
- name: Upload case studies
|
|
116
122
|
uses: actions/upload-artifact@v7
|
|
117
123
|
with:
|
|
118
|
-
name: case-
|
|
119
|
-
path:
|
|
124
|
+
name: case-studies-${{ matrix.os }}
|
|
125
|
+
path: |
|
|
126
|
+
docs/case_study.html
|
|
127
|
+
docs/multi_asset.html
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: backtest-engine
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Classifier: Development Status :: 4 - Beta
|
|
5
5
|
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -36,6 +36,8 @@ is reproduced against a future-masked replay before an HTML report is written.
|
|
|
36
36
|
- Walk-forward folds with non-overlapping train and test windows.
|
|
37
37
|
- Real-data case study: 10 years of SPY, walk-forward, buy-and-hold comparison and a subtle
|
|
38
38
|
full-sample look-ahead strategy that the detector catches.
|
|
39
|
+
- Multi-asset case study: SPY and TLT, relative momentum, per-asset costs and a cross-sectional
|
|
40
|
+
future trap that the detector catches.
|
|
39
41
|
- Self-contained HTML reports whose SHA-256 is verified on Linux, Windows and macOS in CI.
|
|
40
42
|
|
|
41
43
|
## Why Rust with PyO3
|
|
@@ -267,7 +269,9 @@ window on 252-bar training blocks and reports only the following 63-bar blocks;
|
|
|
267
269
|
aggregate out-of-sample return is 16.3799% with a Sharpe of 0.7733 over 8 folds. That gap is the
|
|
268
270
|
point of walk-forward.
|
|
269
271
|
|
|
270
|
-
## Real-data case
|
|
272
|
+
## Real-data case studies
|
|
273
|
+
|
|
274
|
+
### Single asset: SPY
|
|
271
275
|
|
|
272
276
|
The committed report is [`docs/case_study.html`](docs/case_study.html) and is also served at
|
|
273
277
|
[ikerabad.github.io/backtest-engine/case_study.html](https://ikerabad.github.io/backtest-engine/case_study.html).
|
|
@@ -316,6 +320,52 @@ strategy stays flat. The detector reports the first decision where the traces di
|
|
|
316
320
|
rejects `research_series` before a raw report can be produced, and the pytest suite asserts the
|
|
317
321
|
failure on both the synthetic and the real dataset.
|
|
318
322
|
|
|
323
|
+
### Multi-asset: SPY and TLT
|
|
324
|
+
|
|
325
|
+
The two-asset report is [`docs/multi_asset.html`](docs/multi_asset.html) and is served at
|
|
326
|
+
[ikerabad.github.io/backtest-engine/multi_asset.html](https://ikerabad.github.io/backtest-engine/multi_asset.html).
|
|
327
|
+
It uses `data/spy_tlt_daily.csv`: 5,028 rows, 2,514 timestamps, two symbols, same 2016-2026 range.
|
|
328
|
+
|
|
329
|
+
The strategy is `RelativeMomentum`: on the first bar of each month it computes each asset's return
|
|
330
|
+
over a lookback window and puts 100% of equity in the winner. Walk-forward uses 504-bar training,
|
|
331
|
+
252-bar test, 252-bar step and a lookback grid of 63/126/252 over 7 folds.
|
|
332
|
+
|
|
333
|
+
| Out-of-sample metric | Relative momentum | Static 50/50 |
|
|
334
|
+
|---|---:|---:|
|
|
335
|
+
| Total return | 34.2857% | 91.2796% |
|
|
336
|
+
| Annualized volatility | 17.8669% | 12.2823% |
|
|
337
|
+
| Sharpe ratio | 0.3258 | 0.8189 |
|
|
338
|
+
| Max drawdown | -46.6757% | -27.8624% |
|
|
339
|
+
|
|
340
|
+
The strategy **underperforms the static 50/50 reference** and has a much deeper drawdown. It made 93
|
|
341
|
+
fills and selected SPY in 69 rebalances and TLT in 22. Total costs were 8,182.70. The result is
|
|
342
|
+
published as measured.
|
|
343
|
+
|
|
344
|
+
Capital allocation and per-asset costs:
|
|
345
|
+
|
|
346
|
+
- Target weights are fractions of total equity. This strategy uses `w_SPY + w_TLT = 1`, so gross
|
|
347
|
+
exposure is 100% and cash is only the residual after fills. The engine does not enforce a budget;
|
|
348
|
+
the strategy is responsible for the sum of absolute weights.
|
|
349
|
+
- Each asset's fill is charged on its own notional and volume:
|
|
350
|
+
`commission_i = fixed + bps * |qty_i| * open_i / 10_000`. A switch from SPY to TLT produces two
|
|
351
|
+
fills, so the fixed commission is paid twice. Spread and slippage use that asset's own bar.
|
|
352
|
+
|
|
353
|
+
| Asset | Fills | Notional | Commission | Spread | Slippage | Total |
|
|
354
|
+
|---|---:|---:|---:|---:|---:|---:|
|
|
355
|
+
| SPY | 62 | 22,633,022.92 | 2,263.30 | 2,263.30 | 9.00 | 4,535.61 |
|
|
356
|
+
| TLT | 31 | 17,556,560.78 | 1,755.66 | 1,755.66 | 135.78 | 3,647.09 |
|
|
357
|
+
| Total | 93 | 40,189,583.70 | 4,018.96 | 4,018.96 | 144.78 | 8,182.70 |
|
|
358
|
+
|
|
359
|
+
The multi-asset detector cases:
|
|
360
|
+
|
|
361
|
+
- The honest `RelativeMomentum` replay passes with 0 future reads and 0 divergent decisions.
|
|
362
|
+
- `MultiAssetFutureTrap` peeks at both assets' next bar on every rebalance. The detector logs 242
|
|
363
|
+
future reads and 121 divergent decisions. The first is decision #0 on 2016-11-21: normal
|
|
364
|
+
`SPY +1.000000, TLT +0.000000`; masked `SPY +0.000000, TLT +0.000000`. Normal final equity is
|
|
365
|
+
2,025,864.26 against 1,000,000.00 masked.
|
|
366
|
+
- `tests/test_multi_asset.py` asserts the detection, the allocation split and the per-asset
|
|
367
|
+
commissions. Strict mode rejects the trap before a raw report can be produced.
|
|
368
|
+
|
|
319
369
|
## Measured performance
|
|
320
370
|
|
|
321
371
|
One 10-year daily backtest, release build, measured on 2026-11-23:
|
|
@@ -351,8 +401,10 @@ Ubuntu, Windows and macOS:
|
|
|
351
401
|
module, no `pow`.
|
|
352
402
|
- The committed engine report hash lives in `reports/expected.sha256` and is checked by
|
|
353
403
|
`scripts/verify_report.py` on Ubuntu, Windows and macOS.
|
|
354
|
-
- The
|
|
355
|
-
`scripts/verify_case_study.py` on
|
|
404
|
+
- The single-asset case study hash lives in `docs/case_study.sha256` and is checked by
|
|
405
|
+
`scripts/verify_case_study.py` on all three platforms.
|
|
406
|
+
- The multi-asset case study hash lives in `docs/multi_asset.sha256` and is checked by
|
|
407
|
+
`scripts/verify_multi_asset_case.py` on all three platforms.
|
|
356
408
|
|
|
357
409
|
`tests/test_report.py` also checks that two in-process runs produce byte-identical HTML.
|
|
358
410
|
|
|
@@ -397,18 +449,20 @@ src/
|
|
|
397
449
|
types.rs Bar, Order, Trade, Decision, DataAccess, EngineConfig, result
|
|
398
450
|
lib.rs PyO3 module and Python strategy adapter
|
|
399
451
|
python/backtest_engine/
|
|
400
|
-
strategies.py moving average, mean reversion, look-ahead trap, full-sample z-score
|
|
452
|
+
strategies.py moving average, mean reversion, look-ahead trap, full-sample z-score,
|
|
453
|
+
relative momentum and the multi-asset future trap
|
|
401
454
|
detector.py normal vs masked replay and divergence report
|
|
402
455
|
walkforward.py non-overlapping folds and aggregate out-of-sample metrics
|
|
403
456
|
report.py byte-exact report writer
|
|
404
457
|
cli.py backtest / check / walkforward commands
|
|
405
458
|
_backtest_engine.pyi complete type stubs
|
|
406
459
|
data/sample.csv committed synthetic point-in-time dataset
|
|
407
|
-
data/spy_daily.csv committed real SPY
|
|
408
|
-
|
|
460
|
+
data/spy_daily.csv committed real SPY history and metadata
|
|
461
|
+
data/spy_tlt_daily.csv committed real SPY/TLT history and metadata
|
|
462
|
+
docs/ case studies, index page and benchmark results
|
|
409
463
|
reports/ committed engine report and expected SHA-256
|
|
410
|
-
scripts/ data fetch, case study
|
|
411
|
-
tests/ pytest acceptance, real-data and determinism tests
|
|
464
|
+
scripts/ data fetch, case study builders, verifiers and benchmark
|
|
465
|
+
tests/ pytest acceptance, real-data, multi-asset and determinism tests
|
|
412
466
|
```
|
|
413
467
|
|
|
414
468
|
The Rust core also has unit tests for queue ordering, revision visibility, cost arithmetic, metric
|
|
@@ -424,9 +478,10 @@ math, next-open execution and strict/masked modes (`cargo test`).
|
|
|
424
478
|
`ruff format --check` and `mypy --strict`.
|
|
425
479
|
3. **Reproducible engine report**: on all three OSes, run `examples/run_example.py`, verify
|
|
426
480
|
`reports/expected.sha256`, and confirm that the deliberate trap is detected.
|
|
427
|
-
4. **Real-data case
|
|
428
|
-
`docs/case_study.sha256`, and confirm that the subtle
|
|
429
|
-
|
|
481
|
+
4. **Real-data case studies**: on all three OSes, rebuild the single-asset and multi-asset reports,
|
|
482
|
+
verify `docs/case_study.sha256` and `docs/multi_asset.sha256`, and confirm that both the subtle
|
|
483
|
+
full-sample strategy and the multi-asset future trap are detected. The reports are uploaded as
|
|
484
|
+
artifacts.
|
|
430
485
|
|
|
431
486
|
## Scope and limitations
|
|
432
487
|
|
|
@@ -435,12 +490,12 @@ HTML reporting, deterministic hashing.
|
|
|
435
490
|
|
|
436
491
|
Out of scope: broker connections, paid data, portfolio optimisation, margin/borrow costs, dividends,
|
|
437
492
|
corporate actions, taxes, intraday calendars and survivorship-bias-free universe construction. The
|
|
438
|
-
sample has one symbol; the
|
|
439
|
-
|
|
440
|
-
The real-data
|
|
441
|
-
terms before commercial redistribution. A strategy that loads its own
|
|
442
|
-
full-sample indicators outside `ctx` is outside the detector's reach. This
|
|
443
|
-
not investment advice.
|
|
493
|
+
synthetic sample has one symbol; the real-data multi-asset case covers two. The engine supports
|
|
494
|
+
arbitrary symbols and the portfolio accounting is per asset. Slippage is linear in participation, not
|
|
495
|
+
a square-root impact model. The real-data CSVs are snapshots from Yahoo Finance and are used for
|
|
496
|
+
research; check the provider's terms before commercial redistribution. A strategy that loads its own
|
|
497
|
+
DataFrame and precomputes full-sample indicators outside `ctx` is outside the detector's reach. This
|
|
498
|
+
is research software, not investment advice.
|
|
444
499
|
|
|
445
500
|
## License
|
|
446
501
|
|
|
@@ -15,6 +15,8 @@ is reproduced against a future-masked replay before an HTML report is written.
|
|
|
15
15
|
- Walk-forward folds with non-overlapping train and test windows.
|
|
16
16
|
- Real-data case study: 10 years of SPY, walk-forward, buy-and-hold comparison and a subtle
|
|
17
17
|
full-sample look-ahead strategy that the detector catches.
|
|
18
|
+
- Multi-asset case study: SPY and TLT, relative momentum, per-asset costs and a cross-sectional
|
|
19
|
+
future trap that the detector catches.
|
|
18
20
|
- Self-contained HTML reports whose SHA-256 is verified on Linux, Windows and macOS in CI.
|
|
19
21
|
|
|
20
22
|
## Why Rust with PyO3
|
|
@@ -246,7 +248,9 @@ window on 252-bar training blocks and reports only the following 63-bar blocks;
|
|
|
246
248
|
aggregate out-of-sample return is 16.3799% with a Sharpe of 0.7733 over 8 folds. That gap is the
|
|
247
249
|
point of walk-forward.
|
|
248
250
|
|
|
249
|
-
## Real-data case
|
|
251
|
+
## Real-data case studies
|
|
252
|
+
|
|
253
|
+
### Single asset: SPY
|
|
250
254
|
|
|
251
255
|
The committed report is [`docs/case_study.html`](docs/case_study.html) and is also served at
|
|
252
256
|
[ikerabad.github.io/backtest-engine/case_study.html](https://ikerabad.github.io/backtest-engine/case_study.html).
|
|
@@ -295,6 +299,52 @@ strategy stays flat. The detector reports the first decision where the traces di
|
|
|
295
299
|
rejects `research_series` before a raw report can be produced, and the pytest suite asserts the
|
|
296
300
|
failure on both the synthetic and the real dataset.
|
|
297
301
|
|
|
302
|
+
### Multi-asset: SPY and TLT
|
|
303
|
+
|
|
304
|
+
The two-asset report is [`docs/multi_asset.html`](docs/multi_asset.html) and is served at
|
|
305
|
+
[ikerabad.github.io/backtest-engine/multi_asset.html](https://ikerabad.github.io/backtest-engine/multi_asset.html).
|
|
306
|
+
It uses `data/spy_tlt_daily.csv`: 5,028 rows, 2,514 timestamps, two symbols, same 2016-2026 range.
|
|
307
|
+
|
|
308
|
+
The strategy is `RelativeMomentum`: on the first bar of each month it computes each asset's return
|
|
309
|
+
over a lookback window and puts 100% of equity in the winner. Walk-forward uses 504-bar training,
|
|
310
|
+
252-bar test, 252-bar step and a lookback grid of 63/126/252 over 7 folds.
|
|
311
|
+
|
|
312
|
+
| Out-of-sample metric | Relative momentum | Static 50/50 |
|
|
313
|
+
|---|---:|---:|
|
|
314
|
+
| Total return | 34.2857% | 91.2796% |
|
|
315
|
+
| Annualized volatility | 17.8669% | 12.2823% |
|
|
316
|
+
| Sharpe ratio | 0.3258 | 0.8189 |
|
|
317
|
+
| Max drawdown | -46.6757% | -27.8624% |
|
|
318
|
+
|
|
319
|
+
The strategy **underperforms the static 50/50 reference** and has a much deeper drawdown. It made 93
|
|
320
|
+
fills and selected SPY in 69 rebalances and TLT in 22. Total costs were 8,182.70. The result is
|
|
321
|
+
published as measured.
|
|
322
|
+
|
|
323
|
+
Capital allocation and per-asset costs:
|
|
324
|
+
|
|
325
|
+
- Target weights are fractions of total equity. This strategy uses `w_SPY + w_TLT = 1`, so gross
|
|
326
|
+
exposure is 100% and cash is only the residual after fills. The engine does not enforce a budget;
|
|
327
|
+
the strategy is responsible for the sum of absolute weights.
|
|
328
|
+
- Each asset's fill is charged on its own notional and volume:
|
|
329
|
+
`commission_i = fixed + bps * |qty_i| * open_i / 10_000`. A switch from SPY to TLT produces two
|
|
330
|
+
fills, so the fixed commission is paid twice. Spread and slippage use that asset's own bar.
|
|
331
|
+
|
|
332
|
+
| Asset | Fills | Notional | Commission | Spread | Slippage | Total |
|
|
333
|
+
|---|---:|---:|---:|---:|---:|---:|
|
|
334
|
+
| SPY | 62 | 22,633,022.92 | 2,263.30 | 2,263.30 | 9.00 | 4,535.61 |
|
|
335
|
+
| TLT | 31 | 17,556,560.78 | 1,755.66 | 1,755.66 | 135.78 | 3,647.09 |
|
|
336
|
+
| Total | 93 | 40,189,583.70 | 4,018.96 | 4,018.96 | 144.78 | 8,182.70 |
|
|
337
|
+
|
|
338
|
+
The multi-asset detector cases:
|
|
339
|
+
|
|
340
|
+
- The honest `RelativeMomentum` replay passes with 0 future reads and 0 divergent decisions.
|
|
341
|
+
- `MultiAssetFutureTrap` peeks at both assets' next bar on every rebalance. The detector logs 242
|
|
342
|
+
future reads and 121 divergent decisions. The first is decision #0 on 2016-11-21: normal
|
|
343
|
+
`SPY +1.000000, TLT +0.000000`; masked `SPY +0.000000, TLT +0.000000`. Normal final equity is
|
|
344
|
+
2,025,864.26 against 1,000,000.00 masked.
|
|
345
|
+
- `tests/test_multi_asset.py` asserts the detection, the allocation split and the per-asset
|
|
346
|
+
commissions. Strict mode rejects the trap before a raw report can be produced.
|
|
347
|
+
|
|
298
348
|
## Measured performance
|
|
299
349
|
|
|
300
350
|
One 10-year daily backtest, release build, measured on 2026-11-23:
|
|
@@ -330,8 +380,10 @@ Ubuntu, Windows and macOS:
|
|
|
330
380
|
module, no `pow`.
|
|
331
381
|
- The committed engine report hash lives in `reports/expected.sha256` and is checked by
|
|
332
382
|
`scripts/verify_report.py` on Ubuntu, Windows and macOS.
|
|
333
|
-
- The
|
|
334
|
-
`scripts/verify_case_study.py` on
|
|
383
|
+
- The single-asset case study hash lives in `docs/case_study.sha256` and is checked by
|
|
384
|
+
`scripts/verify_case_study.py` on all three platforms.
|
|
385
|
+
- The multi-asset case study hash lives in `docs/multi_asset.sha256` and is checked by
|
|
386
|
+
`scripts/verify_multi_asset_case.py` on all three platforms.
|
|
335
387
|
|
|
336
388
|
`tests/test_report.py` also checks that two in-process runs produce byte-identical HTML.
|
|
337
389
|
|
|
@@ -376,18 +428,20 @@ src/
|
|
|
376
428
|
types.rs Bar, Order, Trade, Decision, DataAccess, EngineConfig, result
|
|
377
429
|
lib.rs PyO3 module and Python strategy adapter
|
|
378
430
|
python/backtest_engine/
|
|
379
|
-
strategies.py moving average, mean reversion, look-ahead trap, full-sample z-score
|
|
431
|
+
strategies.py moving average, mean reversion, look-ahead trap, full-sample z-score,
|
|
432
|
+
relative momentum and the multi-asset future trap
|
|
380
433
|
detector.py normal vs masked replay and divergence report
|
|
381
434
|
walkforward.py non-overlapping folds and aggregate out-of-sample metrics
|
|
382
435
|
report.py byte-exact report writer
|
|
383
436
|
cli.py backtest / check / walkforward commands
|
|
384
437
|
_backtest_engine.pyi complete type stubs
|
|
385
438
|
data/sample.csv committed synthetic point-in-time dataset
|
|
386
|
-
data/spy_daily.csv committed real SPY
|
|
387
|
-
|
|
439
|
+
data/spy_daily.csv committed real SPY history and metadata
|
|
440
|
+
data/spy_tlt_daily.csv committed real SPY/TLT history and metadata
|
|
441
|
+
docs/ case studies, index page and benchmark results
|
|
388
442
|
reports/ committed engine report and expected SHA-256
|
|
389
|
-
scripts/ data fetch, case study
|
|
390
|
-
tests/ pytest acceptance, real-data and determinism tests
|
|
443
|
+
scripts/ data fetch, case study builders, verifiers and benchmark
|
|
444
|
+
tests/ pytest acceptance, real-data, multi-asset and determinism tests
|
|
391
445
|
```
|
|
392
446
|
|
|
393
447
|
The Rust core also has unit tests for queue ordering, revision visibility, cost arithmetic, metric
|
|
@@ -403,9 +457,10 @@ math, next-open execution and strict/masked modes (`cargo test`).
|
|
|
403
457
|
`ruff format --check` and `mypy --strict`.
|
|
404
458
|
3. **Reproducible engine report**: on all three OSes, run `examples/run_example.py`, verify
|
|
405
459
|
`reports/expected.sha256`, and confirm that the deliberate trap is detected.
|
|
406
|
-
4. **Real-data case
|
|
407
|
-
`docs/case_study.sha256`, and confirm that the subtle
|
|
408
|
-
|
|
460
|
+
4. **Real-data case studies**: on all three OSes, rebuild the single-asset and multi-asset reports,
|
|
461
|
+
verify `docs/case_study.sha256` and `docs/multi_asset.sha256`, and confirm that both the subtle
|
|
462
|
+
full-sample strategy and the multi-asset future trap are detected. The reports are uploaded as
|
|
463
|
+
artifacts.
|
|
409
464
|
|
|
410
465
|
## Scope and limitations
|
|
411
466
|
|
|
@@ -414,12 +469,12 @@ HTML reporting, deterministic hashing.
|
|
|
414
469
|
|
|
415
470
|
Out of scope: broker connections, paid data, portfolio optimisation, margin/borrow costs, dividends,
|
|
416
471
|
corporate actions, taxes, intraday calendars and survivorship-bias-free universe construction. The
|
|
417
|
-
sample has one symbol; the
|
|
418
|
-
|
|
419
|
-
The real-data
|
|
420
|
-
terms before commercial redistribution. A strategy that loads its own
|
|
421
|
-
full-sample indicators outside `ctx` is outside the detector's reach. This
|
|
422
|
-
not investment advice.
|
|
472
|
+
synthetic sample has one symbol; the real-data multi-asset case covers two. The engine supports
|
|
473
|
+
arbitrary symbols and the portfolio accounting is per asset. Slippage is linear in participation, not
|
|
474
|
+
a square-root impact model. The real-data CSVs are snapshots from Yahoo Finance and are used for
|
|
475
|
+
research; check the provider's terms before commercial redistribution. A strategy that loads its own
|
|
476
|
+
DataFrame and precomputes full-sample indicators outside `ctx` is outside the detector's reach. This
|
|
477
|
+
is research software, not investment advice.
|
|
423
478
|
|
|
424
479
|
## License
|
|
425
480
|
|