backtest2live 0.2.0__py3-none-any.whl
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.
- backtest2live/__init__.py +12 -0
- backtest2live/__main__.py +5 -0
- backtest2live/cli.py +68 -0
- backtest2live/convergence.py +567 -0
- backtest2live/fillgap/__init__.py +57 -0
- backtest2live/fillgap/data.py +289 -0
- backtest2live/fillgap/engine.py +169 -0
- backtest2live/fillgap/report.py +101 -0
- backtest2live/fillgap/sim.py +192 -0
- backtest2live-0.2.0.dist-info/METADATA +218 -0
- backtest2live-0.2.0.dist-info/RECORD +14 -0
- backtest2live-0.2.0.dist-info/WHEEL +4 -0
- backtest2live-0.2.0.dist-info/entry_points.txt +2 -0
- backtest2live-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Queue-proxy fill simulation for resting limit orders on L1 book + trade prints.
|
|
2
|
+
|
|
3
|
+
Deterministic, numpy only. All times are int64 microseconds; all prices are
|
|
4
|
+
int64 ticks. Buy logic is written once: sell orders are mirrored by negating
|
|
5
|
+
prices, so "the bid" is the order's own side and "below P" means through it.
|
|
6
|
+
|
|
7
|
+
Per order, in the window (active, end]:
|
|
8
|
+
- marketable: the limit crosses the opposite touch at arrival. A taker
|
|
9
|
+
order, out of scope for the maker comparison.
|
|
10
|
+
- queue ahead: 0 if the order improves the touch; the displayed size if it
|
|
11
|
+
joins the touch; if it rests behind the touch, the displayed size when the
|
|
12
|
+
touch first reaches its price (0 if the touch jumps past it).
|
|
13
|
+
- proxy fill: passive-side volume printed at exactly its price reaches
|
|
14
|
+
queue ahead + own size, or a passive-side trade prints through its price.
|
|
15
|
+
- touch fill (the naive backtest rule): any passive-side print at or
|
|
16
|
+
through its price after arrival.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
# status codes
|
|
22
|
+
NO_DATA, MARKETABLE, NEVER_REACHED, ACTIVE = 0, 1, 2, 3
|
|
23
|
+
STATUS_NAMES = {NO_DATA: "no_data", MARKETABLE: "marketable",
|
|
24
|
+
NEVER_REACHED: "never_reached", ACTIVE: "active"}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _side_arrays(book, trades, buy):
|
|
28
|
+
s = 1 if buy else -1
|
|
29
|
+
own_px = book["bid_px"] if buy else -book["ask_px"]
|
|
30
|
+
own_qty = book["bid_qty"] if buy else book["ask_qty"]
|
|
31
|
+
opp_px = book["ask_px"] if buy else -book["bid_px"]
|
|
32
|
+
# A resting buy is hit by aggressive sells, a resting sell by aggressive buys.
|
|
33
|
+
hit = trades["sell_aggr"] if buy else ~trades["sell_aggr"]
|
|
34
|
+
return own_px, own_qty, opp_px, trades["t"][hit], s * trades["px"][hit], trades["qty"][hit]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _first_through(ht, hp, t_from, t_to, P, idx):
|
|
38
|
+
"""For orders idx: time of the first hit trade with price < P in (t_from, t_to], else -1."""
|
|
39
|
+
out = np.full(len(P), -1, dtype=np.int64)
|
|
40
|
+
a = np.searchsorted(ht, t_from, "right")
|
|
41
|
+
b = np.searchsorted(ht, t_to, "right")
|
|
42
|
+
for j in idx:
|
|
43
|
+
if b[j] <= a[j]:
|
|
44
|
+
continue
|
|
45
|
+
seg = hp[a[j]:b[j]]
|
|
46
|
+
m = seg < P[j]
|
|
47
|
+
if m.any():
|
|
48
|
+
out[j] = ht[a[j] + int(m.argmax())]
|
|
49
|
+
return out
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def simulate_side(book, trades, t_arr, t_end, P_raw, size, buy):
|
|
53
|
+
"""Simulate orders of one side. Returns dict of per-order arrays."""
|
|
54
|
+
n = len(t_arr)
|
|
55
|
+
own_px, own_qty, opp_px, ht, hp, hq = _side_arrays(book, trades, buy)
|
|
56
|
+
P = P_raw if buy else -P_raw
|
|
57
|
+
bt = book["t"]
|
|
58
|
+
|
|
59
|
+
status = np.full(n, NO_DATA, dtype=np.int8)
|
|
60
|
+
queue = np.zeros(n, dtype=np.float64)
|
|
61
|
+
t_act = np.full(n, -1, dtype=np.int64)
|
|
62
|
+
|
|
63
|
+
i = np.searchsorted(bt, t_arr, "right") - 1
|
|
64
|
+
covered = (i >= 0) & (t_arr <= bt[-1]) & (t_end >= t_arr) if len(bt) else np.zeros(n, bool)
|
|
65
|
+
ic = np.clip(i, 0, max(len(bt) - 1, 0))
|
|
66
|
+
|
|
67
|
+
marketable = covered & (P >= opp_px[ic])
|
|
68
|
+
improve = covered & ~marketable & (P > own_px[ic])
|
|
69
|
+
at_touch = covered & ~marketable & (P == own_px[ic])
|
|
70
|
+
behind = covered & ~marketable & (P < own_px[ic])
|
|
71
|
+
|
|
72
|
+
status[marketable] = MARKETABLE
|
|
73
|
+
status[improve | at_touch] = ACTIVE
|
|
74
|
+
t_act[improve | at_touch] = t_arr[improve | at_touch]
|
|
75
|
+
queue[at_touch] = own_qty[ic[at_touch]]
|
|
76
|
+
|
|
77
|
+
# Behind the touch: wait until the touch comes to the order's price.
|
|
78
|
+
i_end = np.searchsorted(bt, t_end, "right") - 1
|
|
79
|
+
status[behind] = NEVER_REACHED
|
|
80
|
+
for j in np.flatnonzero(behind):
|
|
81
|
+
seg = own_px[ic[j] + 1:i_end[j] + 1]
|
|
82
|
+
m = seg <= P[j]
|
|
83
|
+
if m.any():
|
|
84
|
+
k = ic[j] + 1 + int(m.argmax())
|
|
85
|
+
status[j] = ACTIVE
|
|
86
|
+
t_act[j] = bt[k]
|
|
87
|
+
queue[j] = own_qty[k] if own_px[k] == P[j] else 0.0
|
|
88
|
+
|
|
89
|
+
active = status == ACTIVE
|
|
90
|
+
live = covered & ~marketable
|
|
91
|
+
idx_live = np.flatnonzero(live)
|
|
92
|
+
|
|
93
|
+
# --- volume printed at exactly P, sorted by (price, time) ---
|
|
94
|
+
t_fill_at = np.full(n, -1, dtype=np.int64)
|
|
95
|
+
t_touch_at = np.full(n, -1, dtype=np.int64)
|
|
96
|
+
if len(ht):
|
|
97
|
+
t0 = min(int(ht.min()), int(t_arr.min())) - 1
|
|
98
|
+
span = max(int(ht.max()), int(t_end.max())) - t0 + 2
|
|
99
|
+
B = 1 << int(span).bit_length()
|
|
100
|
+
pmin = min(int(hp.min()), int(P.min()))
|
|
101
|
+
pmax = max(int(hp.max()), int(P.max()))
|
|
102
|
+
if (pmax - pmin + 1) * B >= (1 << 62):
|
|
103
|
+
raise SystemExit("error: price range × time span too large for one pass; split the input by day")
|
|
104
|
+
key = (hp - pmin) * B + (ht - t0)
|
|
105
|
+
o = np.argsort(key, kind="stable")
|
|
106
|
+
key, ts, cs = key[o], ht[o], np.r_[0.0, np.cumsum(hq[o])]
|
|
107
|
+
base = (P - pmin) * B - t0
|
|
108
|
+
|
|
109
|
+
# touch rule: any print at P after arrival
|
|
110
|
+
lo_n = np.searchsorted(key, base + t_arr, "right")
|
|
111
|
+
hi = np.searchsorted(key, base + t_end, "right")
|
|
112
|
+
has = live & (hi > lo_n)
|
|
113
|
+
t_touch_at[has] = ts[lo_n[has]]
|
|
114
|
+
|
|
115
|
+
# proxy: cumulative volume at P after activation covers queue + own size
|
|
116
|
+
ta = np.where(active, t_act, t_end)
|
|
117
|
+
lo = np.searchsorted(key, base + ta, "right")
|
|
118
|
+
k = np.searchsorted(cs, cs[lo] + queue + size - 1e-12, "left")
|
|
119
|
+
ok = active & (k > lo) & (k <= hi)
|
|
120
|
+
t_fill_at[ok] = ts[k[ok] - 1]
|
|
121
|
+
|
|
122
|
+
# --- prints through P (the level was cleared) ---
|
|
123
|
+
t_through = _first_through(ht, hp, t_arr, t_end, P, idx_live)
|
|
124
|
+
|
|
125
|
+
def first(a, b):
|
|
126
|
+
both = (a >= 0) & (b >= 0)
|
|
127
|
+
return np.where(both, np.minimum(a, b), np.maximum(a, b))
|
|
128
|
+
|
|
129
|
+
t_touch = first(t_touch_at, t_through)
|
|
130
|
+
# A print through P clears every order resting at P since arrival, so the
|
|
131
|
+
# through leg applies from t_arr, also to orders the book never showed at
|
|
132
|
+
# the touch (the sweep and the book row can share a ms, or the print can
|
|
133
|
+
# lead the row).
|
|
134
|
+
t_through_proxy = np.where(live, t_through, -1)
|
|
135
|
+
t_proxy = first(t_fill_at, t_through_proxy)
|
|
136
|
+
status[(status == NEVER_REACHED) & (t_proxy >= 0)] = ACTIVE
|
|
137
|
+
|
|
138
|
+
return {"status": status, "queue": queue, "t_act": t_act,
|
|
139
|
+
"touch": t_touch >= 0, "t_touch": t_touch,
|
|
140
|
+
"proxy": t_proxy >= 0, "t_proxy": t_proxy}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def simulate(book, trades, orders, latency_us=0):
|
|
144
|
+
"""orders: dict t, t_end, buy (bool), px (ticks), size. Returns per-order arrays in input order."""
|
|
145
|
+
n = len(orders["t"])
|
|
146
|
+
out = {
|
|
147
|
+
"status": np.zeros(n, np.int8), "queue": np.zeros(n), "t_act": np.full(n, -1, np.int64),
|
|
148
|
+
"touch": np.zeros(n, bool), "t_touch": np.full(n, -1, np.int64),
|
|
149
|
+
"proxy": np.zeros(n, bool), "t_proxy": np.full(n, -1, np.int64),
|
|
150
|
+
}
|
|
151
|
+
t_arr = orders["t"] + int(latency_us)
|
|
152
|
+
for buy in (True, False):
|
|
153
|
+
m = orders["buy"] == buy
|
|
154
|
+
if not m.any():
|
|
155
|
+
continue
|
|
156
|
+
r = simulate_side(book, trades, t_arr[m], orders["t_end"][m], orders["px"][m],
|
|
157
|
+
orders["size"][m], buy)
|
|
158
|
+
for k, v in r.items():
|
|
159
|
+
out[k][m] = v
|
|
160
|
+
out["t_arr"] = t_arr
|
|
161
|
+
return out
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def trailing_range_bps(book, t_query, window_us=300_000_000, bin_us=60_000_000):
|
|
165
|
+
"""Trailing realized range of the mid (max-min over the previous 5 one-minute bins, bps of the min)."""
|
|
166
|
+
bt = book["t"]
|
|
167
|
+
mid = (book["bid_px"] + book["ask_px"]) / 2.0
|
|
168
|
+
t0 = bt[0]
|
|
169
|
+
b = (bt - t0) // bin_us
|
|
170
|
+
nb = int(b[-1]) + 1
|
|
171
|
+
mx = np.full(nb, np.nan)
|
|
172
|
+
mn = np.full(nb, np.nan)
|
|
173
|
+
np.fmax.at(mx, b, mid)
|
|
174
|
+
np.fmin.at(mn, b, mid)
|
|
175
|
+
# carry the last value through empty bins
|
|
176
|
+
for arr in (mx, mn):
|
|
177
|
+
idx = np.where(np.isnan(arr), 0, np.arange(nb))
|
|
178
|
+
np.maximum.accumulate(idx, out=idx)
|
|
179
|
+
arr[:] = arr[idx]
|
|
180
|
+
w = int(window_us // bin_us)
|
|
181
|
+
rmx, rmn = _rolling(mx, w, np.fmax), _rolling(mn, w, np.fmin)
|
|
182
|
+
rng = (rmx - rmn) / rmn * 1e4
|
|
183
|
+
q = np.clip((t_query - t0) // bin_us - 1, 0, nb - 1) # the window before the order
|
|
184
|
+
return np.where(t_query >= t0, rng[q], np.nan)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _rolling(a, w, f):
|
|
188
|
+
"""Trailing w-bin reduction: out[i] = f over a[i-w+1..i]."""
|
|
189
|
+
out = a.copy()
|
|
190
|
+
for s in range(1, w):
|
|
191
|
+
out[s:] = f(out[s:], a[:-s])
|
|
192
|
+
return out
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: backtest2live
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Check a backtest against what live execution would do: convergence, fill-gap, and more, run locally.
|
|
5
|
+
Project-URL: Homepage, https://martianmobile.com
|
|
6
|
+
Project-URL: Source, https://github.com/martianmobile/backtest2live
|
|
7
|
+
Project-URL: Issues, https://github.com/martianmobile/backtest2live/issues
|
|
8
|
+
Author-email: Martian Mobile <martin@martianmobile.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: backtest,execution,fill-rate,overfitting,quant,trading
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Provides-Extra: data
|
|
20
|
+
Requires-Dist: numpy>=1.23; extra == 'data'
|
|
21
|
+
Requires-Dist: pandas>=1.5; extra == 'data'
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# backtest2live
|
|
27
|
+
|
|
28
|
+
[](https://github.com/martianmobile/backtest2live/actions/workflows/ci.yml)
|
|
29
|
+
|
|
30
|
+
**Check a backtest against what live execution would do.** Evaluators run locally on your own files and public data, and each one returns a verdict with the numbers behind it.
|
|
31
|
+
|
|
32
|
+
Use it three ways: as a Python CLI (`bt2live`), as a library, or as a Claude Code plugin where the agent runs the CLI and interprets the verdict.
|
|
33
|
+
|
|
34
|
+
This is the public, MIT-licensed version of checks used in live crypto trading research at [Martian Mobile](https://martianmobile.com).
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Evaluators
|
|
39
|
+
|
|
40
|
+
| Command | Status | What it does |
|
|
41
|
+
|---------|--------|--------------|
|
|
42
|
+
| **`bt2live convergence`** | shipped | Read a variant parameter sweep → measure dispersion, IS/OOS rank stability, and parameter-plateau structure → verdict: **CONVERGED / ITERATE / KILL**. |
|
|
43
|
+
| **`bt2live fill-gap`** | shipped | Re-run your backtest's resting orders under a queue proxy on top-of-book and trades → your fill rate vs the proxy's: **CONSISTENT / OVERSTATED**. |
|
|
44
|
+
|
|
45
|
+
Roadmap: [ROADMAP.md](ROADMAP.md) and the [milestones](https://github.com/martianmobile/backtest2live/milestones).
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install backtest2live
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The core has no runtime dependencies (Python 3.9+). `fill-gap` needs numpy and pandas:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install 'backtest2live[data]'
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Claude Code plugin.** This repo is also a plugin marketplace. From inside Claude Code:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
/plugin marketplace add martianmobile/backtest2live
|
|
65
|
+
/plugin install backtest2live@martianmobile
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Then ask *"are these variants converged?"* or run `/convergence results.csv --metric sharpe_oos`. The skill installs the CLI if it is missing.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## `fill-gap`: do your backtest's passive fills survive a queue?
|
|
73
|
+
|
|
74
|
+
A backtest that fills a resting order whenever price touches it counts fills a live order would never get: the order sits behind the queue already displayed at that price. `fill-gap` re-runs each resting order in your order log against top-of-book and trade prints, and grants a fill only when:
|
|
75
|
+
|
|
76
|
+
- the volume printed at exactly your price, after you joined, covers the queue ahead of you plus your own size, or
|
|
77
|
+
- a trade prints through your price, which means the level was cleared.
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
bt2live fill-gap examples/orders_btcusdt_2024-03-30.csv --venue binance-um --pair BTCUSDT
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
## Verdict: OVERSTATED
|
|
85
|
+
|
|
86
|
+
**Your backtest fills 95.2% of its resting orders; the queue proxy fills 66.2%.
|
|
87
|
+
30.4% of the counted fills (834 of 2,740) are not granted on this data.**
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The report continues with fill rates by rule, median time to fill, misses by volatility tercile and by UTC hour, and P&L on missed fills if your log has a `pnl` column. `--json` gives the same result machine-readable. Exit code: `0` CONSISTENT, `1` OVERSTATED (more than `--tolerance`, default 10%, of your fills not granted), `3` error.
|
|
91
|
+
|
|
92
|
+
### Your order log
|
|
93
|
+
|
|
94
|
+
CSV, one row per order. Column names are matched case-insensitively:
|
|
95
|
+
|
|
96
|
+
| Role | Accepted names | Required |
|
|
97
|
+
|------|----------------|----------|
|
|
98
|
+
| time | `ts`, `timestamp`, `time`, `datetime`, `created_at` | yes (epoch s/ms/µs/ns or ISO-8601, UTC) |
|
|
99
|
+
| side | `side` (`buy`/`sell`) | yes |
|
|
100
|
+
| price | `price`, `px`, `limit_price` | yes |
|
|
101
|
+
| size | `size`, `qty`, `quantity`, `amount` | yes (base units) |
|
|
102
|
+
| backtest's fill | `filled` (true/false, 1/0) or `fill_qty` | no: without it, the touch rule is the baseline |
|
|
103
|
+
| end of life | `end_ts`, `cancel_ts`, `expire_ts` | no: default `--max-rest 60` seconds |
|
|
104
|
+
| P&L | `pnl` | no |
|
|
105
|
+
| id | `order_id` | no |
|
|
106
|
+
|
|
107
|
+
Orders marketable at arrival are taker orders and are skipped. `--latency-ms` delays every order's arrival at the book.
|
|
108
|
+
|
|
109
|
+
### Market data
|
|
110
|
+
|
|
111
|
+
- **`--venue binance-um --pair BTCUSDT`** downloads Binance's public USDT-M futures archive (`bookTicker` + `aggTrades`) for the days your orders cover, checks the published SHA-256, and caches it under `~/.cache/backtest2live` (`BT2LIVE_CACHE` overrides). About 100 MB per day for BTCUSDT. Binance stopped publishing `bookTicker` after **2024-03-30**, so this source covers orders up to that day.
|
|
112
|
+
- **`--book l1.csv --trades trades.csv`** uses your own recorded data, any venue and any date. Book: `ts, bid_px, bid_qty, ask_px, ask_qty` (every change of the best bid/ask). Trades: `ts, price, qty` plus `is_buyer_maker` (true when a seller hit the bid) or the taker's `side`.
|
|
113
|
+
|
|
114
|
+
### What the proxy cannot see
|
|
115
|
+
|
|
116
|
+
The touch rule is an upper bound: a resting order fills only if something trades at or through its price. The proxy is an estimate below it. Hidden size, your latency and your own impact make it too generous. Cancels ahead of you, and a queue position read from the size shown when the price reached you, make it too strict. How that nets out depends on venue, size and regime. Only your own live fills settle it; [Martian Mobile](https://martianmobile.com/fill-autopsy?utm_source=github&utm_medium=readme&utm_campaign=fill-gap) runs that check against live fills.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## `convergence`: has your variant sweep converged?
|
|
121
|
+
|
|
122
|
+
Given one row per variant (parameters + metrics), it measures:
|
|
123
|
+
|
|
124
|
+
- **Top-K dispersion** — are the best variants clustered, or is one a lucky outlier?
|
|
125
|
+
- **IS↔OOS rank stability** — does the in-sample ordering survive out-of-sample? (Spearman)
|
|
126
|
+
- **Parameter-space structure** — do winners sit on a plateau, or are they isolated points (overfit)? Is the best variant pinned to the edge of the swept range?
|
|
127
|
+
- **Sample sufficiency** — are any top variants below a trade-count floor?
|
|
128
|
+
|
|
129
|
+
…then returns a verdict:
|
|
130
|
+
|
|
131
|
+
| Verdict | Meaning |
|
|
132
|
+
|---------|---------|
|
|
133
|
+
| **CONVERGED** | Tight, OOS-stable, plateau region. Ship the representative variant. |
|
|
134
|
+
| **ITERATE** | Some criteria unmet (edge peak, weak stability, high dispersion). Suggested next sweep included. |
|
|
135
|
+
| **KILL** | No stable edge — IS doesn't survive OOS, or best ≈ median. Abandon or rethink. |
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### Usage
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
bt2live convergence examples/results_converged.csv --metric sharpe_oos
|
|
143
|
+
bt2live convergence examples/results_converged.csv --json # machine-readable verdict
|
|
144
|
+
python -m backtest2live convergence results.csv # same, without the script shim
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Exit code encodes the verdict: `0` CONVERGED · `1` ITERATE · `2` KILL · `3` error (bad input or usage), so a CI job can gate on it.
|
|
148
|
+
|
|
149
|
+
### Input format
|
|
150
|
+
|
|
151
|
+
A CSV with one row per variant:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
variant_id, lookback, threshold, sharpe_is, sharpe_oos, n_trades
|
|
155
|
+
v001, 20, 0.5, 1.82, 1.61, 412
|
|
156
|
+
v002, 25, 0.5, 1.85, 1.58, 401
|
|
157
|
+
...
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Column roles are inferred from names/types. IS/OOS pairs are detected by suffix (`_is`/`_oos`, `_in`/`_out`, `_train`/`_test`). For Parquet/SQLite, export to CSV first.
|
|
161
|
+
|
|
162
|
+
### Flags
|
|
163
|
+
|
|
164
|
+
| Flag | Default | Purpose |
|
|
165
|
+
|------|---------|---------|
|
|
166
|
+
| `--metric` | auto | Ranking metric column |
|
|
167
|
+
| `--top-k` | 5 | Top variants to assess |
|
|
168
|
+
| `--dispersion-threshold` | 0.05 | Max relative dispersion for CONVERGED |
|
|
169
|
+
| `--min-samples` | 200 | Sample floor per variant |
|
|
170
|
+
| `--rank-threshold` | 0.6 | Min IS/OOS Spearman for CONVERGED |
|
|
171
|
+
| `--lower-is-better` / `--higher-is-better` | auto | Metric direction override |
|
|
172
|
+
| `--id-column` / `--sample-column` | auto | Column-role overrides |
|
|
173
|
+
| `--save` | off | Also write `iteration_check_<timestamp>.md` in the current directory |
|
|
174
|
+
| `--json` | off | Print the verdict and numbers as JSON instead of Markdown |
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
### Example output
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
# Iteration Check | results_converged.csv | 2026-06-06
|
|
182
|
+
|
|
183
|
+
## Verdict: CONVERGED
|
|
184
|
+
|
|
185
|
+
**Top-5 cluster tightly (1.7% dispersion) on a parameter plateau, OOS-stable (ρ=0.99) — ship `v10`.**
|
|
186
|
+
|
|
187
|
+
## Convergence Analysis
|
|
188
|
+
| Metric | Top-K Mean | Top-K Std | Dispersion | Threshold |
|
|
189
|
+
|--------------------|-----------|-----------|------------|-----------|
|
|
190
|
+
| sharpe_oos (rank) | 1.58 | 0.03 | 1.7% | < 5% ✓ |
|
|
191
|
+
| sharpe_is | 1.82 | 0.02 | 1.3% | < 5% ✓ |
|
|
192
|
+
|
|
193
|
+
IS→OOS rank stability (Spearman): 0.99 (threshold > 0.60 ✓)
|
|
194
|
+
|
|
195
|
+
## Parameter-Space Check
|
|
196
|
+
- lookback: top-K at [20, 25, 30] — contiguous
|
|
197
|
+
- threshold: top-K at [0.5, 0.6] — contiguous
|
|
198
|
+
[x] Plateau region — robust, safe to deploy
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Limitations
|
|
204
|
+
|
|
205
|
+
- **Not a backtester.** It analyzes results you already have; it does not run strategies.
|
|
206
|
+
- **Not a live monitor.** Research-phase tooling — live trading needs different rails.
|
|
207
|
+
- **Metric-agnostic, not metric-smart.** It checks dispersion of whatever metric you point at; it doesn't know whether that metric is the right one.
|
|
208
|
+
- **Convergence ≠ profit.** Convergent variants can converge on losing strategies. The verdict reports convergence, not edge.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Why this exists
|
|
213
|
+
|
|
214
|
+
In real quant research, the failure mode isn't running too few backtests — it's calling one lucky variant a "winner" when its neighbors perform completely differently. Convergence-across-variants is the only honest signal that the parameter region has structure. This tool enforces the discipline — and gives the same treatment to the other ways a strategy can look better than it is.
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
Built by [Martian Mobile](https://martianmobile.com). MIT licensed — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
backtest2live/__init__.py,sha256=GpgnZ13hfLQ0ucK96cdrUkA2qEMhYMMGCd5Csu5l-no,408
|
|
2
|
+
backtest2live/__main__.py,sha256=YE-HyD3F1x67BzDT-6eyjmLYEXdxApdz5Tn2TsGsqr0,65
|
|
3
|
+
backtest2live/cli.py,sha256=y2vMzHu_1atKrbGZd9ySTxlIyyd-9hX1QKpP6tWZuQ0,2166
|
|
4
|
+
backtest2live/convergence.py,sha256=D2d-FX2hvUM0rBMIKo46cknwvHJKt4oZory1vbOsCIk,22555
|
|
5
|
+
backtest2live/fillgap/__init__.py,sha256=EvESvauiip1wA0l4QH-we-hffgymTl--YnHiYmdSopw,2923
|
|
6
|
+
backtest2live/fillgap/data.py,sha256=YAZU7RDK83OmApNPaCS1tmuGh1ztmkEOOeBqzR_rRYA,11721
|
|
7
|
+
backtest2live/fillgap/engine.py,sha256=vjQWNliqOYOvne3QCUlNqy01Jpwop9wqBkevqeJa2Nc,6916
|
|
8
|
+
backtest2live/fillgap/report.py,sha256=wid11xK63RYVMB9rWsrqrFIg83w6HPDBvLpHKojs8aI,5126
|
|
9
|
+
backtest2live/fillgap/sim.py,sha256=VRthq_LV-NJaGk2lwahL3dgim3zVghn2Wjsk-uiTBfg,7842
|
|
10
|
+
backtest2live-0.2.0.dist-info/METADATA,sha256=2cw9n9-LtTdE3s_iUQHmdaNyTPX4DIHd9tRFD7KokBA,10250
|
|
11
|
+
backtest2live-0.2.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
12
|
+
backtest2live-0.2.0.dist-info/entry_points.txt,sha256=IEZ2Cn3k11iTejY8CAaxBtKWjKpKRDJA8dkk8UDWpdU,51
|
|
13
|
+
backtest2live-0.2.0.dist-info/licenses/LICENSE,sha256=ESmBtKQy0a1E-dcresmJrP7DJEr-0eRHTfcmgWd1zjo,1071
|
|
14
|
+
backtest2live-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Martian Mobile
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|