Skip to content

Commit bbc3cb5

Browse files
luisleo526claude
andcommitted
replay: record the default-off byte golden from main; add the decimal-price vector
The default JSON and primary sink stream of the three fixed scripts now carry the sha256 and byte count measured on main 95f4fd1 (record run rj-20261028t103035-87ee0b, six FAIL lines against the empty golden, digests taken from them). The row test_replay_off_golden is red if any default-path byte changes. New fixture shape nondyadic: three separate unamended closes at decimal tenths (100.7, 101.7, 102.9 entries) with a 0.07 per-contract commission and one held lot. The oracle follows the engine operation order in plain binary64. The test compares bits of each presented pnl, the left fold from +0.0 against the replay net profit sum and every Q4, and shows that a 10-significant-digit rounded fold differs, so the vector can tell rounded operands from exact ones. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent c374365 commit bbc3cb5

5 files changed

Lines changed: 168 additions & 14 deletions

File tree

‎docker/replay_off_golden.json‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
"source": "pineforge-engine main 95f4fd1f408bf0ded941ecc4144b278a026a677f, default (no replay switch), unjailed, wall clock fixed at 1.0, run id replay-off-golden",
33
"scenarios": {
44
"fifo": {
5-
"default_json": {"sha256": "", "bytes": 0},
6-
"primary_sink": {"sha256": "", "bytes": 0}
5+
"default_json": {"sha256": "f78a872b6251a8a7a9ba95caa2074a33b519f2c654ad6bf876b341ce7572b2da", "bytes": 9701},
6+
"primary_sink": {"sha256": "dfa2679110fbbf9f24f43c902e2e3a2fc33a1d804a202b8554632206d790686e", "bytes": 1554}
77
},
88
"any": {
9-
"default_json": {"sha256": "", "bytes": 0},
10-
"primary_sink": {"sha256": "", "bytes": 0}
9+
"default_json": {"sha256": "e1de7974b44ece51cdf2577a14a334e5b60da3782b8aa022adadd4ebfbad6ff1", "bytes": 9353},
10+
"primary_sink": {"sha256": "64852f4bb3b132efdd085a9a9c092de7b89224d7bf69bbefe4b779a851e81b14", "bytes": 1440}
1111
},
1212
"ordinary": {
13-
"default_json": {"sha256": "", "bytes": 0},
14-
"primary_sink": {"sha256": "", "bytes": 0}
13+
"default_json": {"sha256": "afbfc63879d67c30f9f19251545e5643b2ce94e6e100fa7e66a889aa090b521c", "bytes": 9363},
14+
"primary_sink": {"sha256": "7c7f414f108b930471026d66174c219304454ffb19232f5d77ea962a7c1455ce", "bytes": 1453}
1515
}
1616
}
1717
}

‎docker/replay_rq_engine_test.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,68 @@ def test_unamended_and_terminal_rows_have_neutral_markers_and_terminal_equity_op
220220
assert rq.bits(last["equity"]) != rq.bits(10000.0 + result.document["replay"]["net_profit_sum"])
221221

222222

223+
@pytest.mark.parametrize("route", ["array", "sink"])
224+
def test_nondyadic_unamended_pnl_is_the_exact_settlement_operand(
225+
tmp_path, monkeypatch, route):
226+
# Closes: 3 @ 100.7 -> 100.9, 5 @ 101.7 -> 101.9, 13 @ 102.9 -> 102.7.
227+
# These decimal tenths survive tick snapping unchanged but are not binary
228+
# exact; 13 * 0.07 also rounds in binary64. Sig10 changes all three P&Ls
229+
# and their final fold, so rounded report operands cannot pass this vector.
230+
result = rq.invoke(tmp_path, monkeypatch, "nondyadic", route=route)
231+
observation = rq.successful(result)
232+
assert observation["groups"] == 0 and observation["physical"] == []
233+
document = result.document
234+
rows = document["trades"]
235+
assert len(rows) == 4
236+
assert [row["settlement_index"] for row in rows] == [0, 1, 2, None]
237+
assert [row["open_at_end"] for row in rows] == [False, False, False, True]
238+
assert all(row["recut"] is None for row in rows)
239+
expected = rq.sources("nondyadic")
240+
terminal = rq.nondyadic_source(1, 103.7, 104.9)
241+
for row, wanted in zip(rows, expected + [terminal]):
242+
for field in ("entry_price", "exit_price", "qty", "commission", "pnl"):
243+
rq.same(row[field], wanted[field])
244+
assert [row["entry_bar_index"] for row in rows] == [1, 3, 5, 7]
245+
assert [row["exit_bar_index"] for row in rows] == [2, 4, 6, 8]
246+
rq.assert_exact(document, "nondyadic")
247+
rq.assert_q_schedule(document, "nondyadic")
248+
249+
presented = [+0.0]
250+
for row in rows[:3]:
251+
presented.append(presented[-1] + row["pnl"])
252+
total = document["replay"]["net_profit_sum"]
253+
rq.same(presented[-1], total)
254+
assert rq.bits(presented[-1] + rows[3]["pnl"]) != rq.bits(total)
255+
256+
records, _ = rq.replay_records(result.companion)
257+
scalars = rq.rows_with_tag(records, "S")
258+
assert len(scalars) == 1
259+
rq.same(scalars[0]["net_profit_sum"], presented[-1])
260+
replay_rows = rq.rows_with_tag(records, "R")
261+
assert len(replay_rows) == 4
262+
for trade, row in zip(rows, replay_rows):
263+
assert row["n"] == trade["n"]
264+
assert row["settlement_index"] == trade["settlement_index"]
265+
assert all(row[field] is None for field in rq.MARKER_FIELDS)
266+
points = rq.rows_with_tag(records, "Q")
267+
assert len(points) == len(document["equity_curve"])
268+
for point in document["equity_curve"] + points:
269+
if point["net_profit_sum"] is not None:
270+
cutoff = point["settlement_count"]
271+
assert type(cutoff) is int and 0 <= cutoff <= 3
272+
rq.same(point["net_profit_sum"], presented[cutoff])
273+
274+
def sig10(value):
275+
return float("%.10g" % value)
276+
277+
# Negative control: precision is deliberately lost only in this fold.
278+
rounded = [+0.0]
279+
for row in rows[:3]:
280+
rounded.append(rounded[-1] + sig10(row["pnl"]))
281+
assert any(rq.bits(sig10(row["pnl"])) != rq.bits(row["pnl"]) for row in rows[:3])
282+
assert rq.bits(rounded[-1]) != rq.bits(total)
283+
284+
223285
@pytest.mark.parametrize("route", ["array", "sink"])
224286
def test_terminal_quote_basis_uses_pre_capital_report_operand_not_raw_n(tmp_path, monkeypatch, route):
225287
"""ANSWER:200-201,239,248: distinct terminal_quote formula and retimed Q2."""

‎docker/replay_rq_support.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test support for the replay R/Q records. Nothing here is a production replay provider.
22
33
ANSWER.md:169-177,197-218,232-246; DELTA.md:60-68,104-110.
4-
Expected operands below come from the fixed integer-price order schedule,
4+
Expected operands below come from the fixed price and order schedule,
55
never from a report's scalar/marker. All success captures run a real module.
66
"""
77
from __future__ import annotations
@@ -59,7 +59,8 @@ def load_line(value):
5959

6060

6161
def csv_tape(path, shape):
62-
count = 46 if shape == "sealed" else 60 if shape == "quote" else 8
62+
count = 46 if shape == "sealed" else 60 if shape == "quote" else (
63+
9 if shape == "nondyadic" else 8)
6364
lines = ["timestamp,open,high,low,close,volume\n"]
6465
for index in range(count):
6566
opening = high = low = close = 128
@@ -70,6 +71,9 @@ def csv_tape(path, shape):
7071
opening = high = low = close = 136
7172
if shape == "ordinary" and index == count - 1:
7273
opening = high = low = close = 144
74+
if shape == "nondyadic":
75+
prices = (100.7, 100.7, 100.9, 101.7, 101.9, 102.9, 102.7, 103.7, 104.9)
76+
opening = high = low = close = prices[index]
7377
if shape == "cancellation":
7478
large = 1 << 54
7579
opening = high = low = close = 1 if index < 2 else (
@@ -270,11 +274,34 @@ def source(qty, entry=128, exit_price=136, fee=1):
270274
"pnl_pct": ((gross - commission) / float(entry * qty)) * 100.0}
271275

272276

277+
def nondyadic_source(qty, entry, exit_price):
278+
"""Independent binary64 oracle; no report value or rounding seeds it."""
279+
qty = float(qty)
280+
pointvalue, fx, fee = 1.0, 1.0, 0.07
281+
entry_commission = fee * qty
282+
exit_commission = fee * qty
283+
# engine_orders.cpp:102-104: subtract, multiply left to right, then
284+
# subtract the sum of the two fees. A full close retains its entry fee.
285+
gross = (exit_price - entry) * qty * pointvalue * fx
286+
pnl = gross
287+
pnl -= entry_commission + exit_commission
288+
entry_cost = entry * qty * pointvalue * fx
289+
return {"qty": qty, "entry_price": entry, "exit_price": exit_price,
290+
"pnl": pnl, "commission": entry_commission + exit_commission,
291+
"max_runup": max(0.0, max(0.0, gross) - entry_commission),
292+
"max_drawdown": max(0.0, max(0.0, -gross) + entry_commission),
293+
"pnl_pct": (pnl / entry_cost) * 100.0}
294+
295+
273296
def sources(shape):
274297
if shape == "zero":
275298
return []
276299
if shape == "ordinary":
277300
return [source(1)]
301+
if shape == "nondyadic":
302+
return [nondyadic_source(3, 100.7, 100.9),
303+
nondyadic_source(5, 101.7, 101.9),
304+
nondyadic_source(13, 102.9, 102.7)]
278305
if shape == "quote":
279306
return [source(1, exit_price=128)]
280307
if shape == "sealed":
@@ -376,12 +403,13 @@ def assert_q_schedule(document, shape, *, selected_anchor=False):
376403
close_bars = {
377404
"zero": [], "any": [3, 3], "fifo": [3, 3], "fifo_no_fees": [3, 3],
378405
"sort": [3, 3], "ordinary": [2], "quote": [30],
406+
"nondyadic": [2, 4, 6],
379407
"two_groups": [3, 3, 3, 3], "cancellation": [4, 4, 4],
380408
"sealed": [2, 6, 10, 14, 18, 22, 27, 27, 34, 38, 42],
381409
}[shape]
382410
points = document["equity_curve"]
383411
expected_minutes = list(range(0, 60, 15)) if shape == "quote" else list(
384-
range(46 if shape == "sealed" else 8))
412+
range(46 if shape == "sealed" else 9 if shape == "nondyadic" else 8))
385413
if shape == "quote":
386414
expected_minutes[-1] = 60 # Quote retimes the minute-45 completed mark.
387415
if selected_anchor:
@@ -395,11 +423,13 @@ def assert_q_schedule(document, shape, *, selected_anchor=False):
395423
continue
396424
bar = (point["time_ms"] - START) // MINUTE
397425
assert point["settlement_count"] == len([at for at in close_bars if at <= bar])
398-
terminal = index == len(points) - 1 and shape in ("ordinary", "quote")
426+
terminal = index == len(points) - 1 and shape in ("ordinary", "quote", "nondyadic")
399427
assert point["equity_basis"] == (
400428
"terminal_quote" if terminal and shape == "quote"
401429
else "range_end" if terminal else "kernel_open")
402430
term = ((144.0 - 128.0) - (1.0 + 1.0)) if terminal else 0.0
431+
if terminal and shape == "nondyadic":
432+
term = nondyadic_source(1, 103.7, 104.9)["pnl"]
403433
if terminal and shape == "quote":
404434
term = ((128.0 - 128.0) - (1.0 + 1.0)) + term
405435
if shape == "cancellation":

‎tests/replay_rq_fixture.hpp‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Host final : public HostBase {
6262
config.pyramiding = 4;
6363
config.commission_type = static_cast<int>(CommissionType::CASH_PER_CONTRACT);
6464
config.commission_value = shape == "fifo_no_fees" ? 0.0
65+
: shape == "nondyadic" ? 0.07
6566
: shape == "cancellation" ? 0.5 : 1.0;
6667
config.close_entries_rule_any = shape == "any" || shape == "cancellation";
6768
config.calc_on_order_fills = false;
@@ -76,7 +77,7 @@ class Host final : public HostBase {
7677
configure_pine_strategy(config);
7778
set_syminfo_timezone("UTC");
7879
set_syminfo_session("24x7");
79-
set_syminfo_mintick(1.0);
80+
set_syminfo_mintick(shape == "nondyadic" ? 0.1 : 1.0);
8081
set_syminfo_pointvalue(1.0);
8182
set_syminfo_metadata("qty_step", 1.0);
8283
set_trade_history_streamable(shape == "sealed");
@@ -100,6 +101,15 @@ class Host final : public HostBase {
100101
strategy_entry("held", true, absent, absent, 1.0);
101102
return;
102103
}
104+
if (shape == "nondyadic") {
105+
// Separate market closes: no global-exit replacement or re-cut.
106+
if (bar == 0) strategy_entry("decimal", true, absent, absent, 3.0);
107+
if (bar == 2) strategy_entry("decimal", true, absent, absent, 5.0);
108+
if (bar == 4) strategy_entry("decimal", true, absent, absent, 13.0);
109+
if (bar == 1 || bar == 3 || bar == 5) strategy_close("decimal");
110+
if (bar == 6) strategy_entry("held", true, absent, absent, 1.0);
111+
return;
112+
}
103113
const int offset = shape == "sealed" ? 24 : 0;
104114
// Six earlier and three later distinct-time closes force the recut
105115
// group past the newest exit-time block AND its retained predecessor.
@@ -152,7 +162,8 @@ class Host final : public HostBase {
152162
};
153163

154164
inline std::vector<Bar> tape(const std::string& shape) {
155-
const int count = shape == "sealed" ? 46 : shape == "quote" ? 60 : 8;
165+
const int count = shape == "sealed" ? 46 : shape == "quote" ? 60
166+
: shape == "nondyadic" ? 9 : 8;
156167
std::vector<Bar> result;
157168
for (int i = 0; i < count; ++i) {
158169
Bar bar{128.0, 128.0, 128.0, 128.0, 1.0, start + i * minute};
@@ -164,6 +175,11 @@ inline std::vector<Bar> tape(const std::string& shape) {
164175
bar.open = bar.high = bar.low = bar.close = 136.0;
165176
if (shape == "ordinary" && i == count - 1)
166177
bar.open = bar.high = bar.low = bar.close = 144.0;
178+
if (shape == "nondyadic") {
179+
constexpr std::array<double, 9> prices{
180+
100.7, 100.7, 100.9, 101.7, 101.9, 102.9, 102.7, 103.7, 104.9};
181+
bar.open = bar.high = bar.low = bar.close = prices[static_cast<std::size_t>(i)];
182+
}
167183
if (shape == "cancellation") {
168184
const double large = static_cast<double>(UINT64_C(1) << 54);
169185
const double price = i < 2 ? 1.0 : i == 3 ? large * 2.0 : large;

‎tests/test_replay_rq_fixture.cpp‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,50 @@ void check_terminal_shape(const std::string& shape, const replay_rq::Host& host,
145145
}
146146
}
147147

148+
void check_nondyadic_shape(const replay_rq::Host& host, const pf_report_t& report) {
149+
replay_rq::require(host.last_run_status() == 0 && host.last_error().empty(),
150+
"decimal-price fixture did not complete");
151+
replay_rq::require(host.groups == 0 && report.trades_len == 4,
152+
"decimal-price fixture needs three separate closes and one held lot");
153+
const std::array<double, 4> entries{100.7, 101.7, 102.9, 103.7};
154+
const std::array<double, 4> exits{100.9, 101.9, 102.7, 104.9};
155+
const std::array<double, 4> quantities{3.0, 5.0, 13.0, 1.0};
156+
const std::array<int, 4> entry_bars{1, 3, 5, 7}, exit_bars{2, 4, 6, 8};
157+
double expected_net = +0.0, presented_net = +0.0;
158+
for (std::size_t i = 0; i < entries.size(); ++i) {
159+
const auto& row = report.trades[i];
160+
replay_rq::require(row.is_long != 0 && (row.open_at_end != 0) == (i == 3),
161+
"decimal-price fixture has the wrong close or terminal population");
162+
replay_rq::require(row.entry_bar_index == entry_bars[i]
163+
&& row.exit_bar_index == exit_bars[i]
164+
&& row.entry_time == replay_rq::start + entry_bars[i] * replay_rq::minute
165+
&& row.exit_time == replay_rq::start + exit_bars[i] * replay_rq::minute,
166+
"decimal-price orders did not fill on the scheduled bars");
167+
replay_rq::require(std::memcmp(&row.entry_price, &entries[i], sizeof(double)) == 0
168+
&& std::memcmp(&row.exit_price, &exits[i], sizeof(double)) == 0
169+
&& std::memcmp(&row.qty, &quantities[i], sizeof(double)) == 0,
170+
"decimal-price fills differ from the independent schedule");
171+
const double entry_fee = 0.07 * quantities[i];
172+
const double exit_fee = 0.07 * quantities[i];
173+
const double commission = entry_fee + exit_fee;
174+
double pnl = (exits[i] - entries[i]) * quantities[i] * 1.0 * 1.0;
175+
pnl -= entry_fee + exit_fee;
176+
replay_rq::require(std::memcmp(&row.pnl, &pnl, sizeof(double)) == 0
177+
&& std::memcmp(&row.commission, &commission, sizeof(double)) == 0,
178+
"decimal-price row changed its binary64 close operands");
179+
if (i < 3) {
180+
expected_net = expected_net + pnl;
181+
presented_net = presented_net + row.pnl;
182+
}
183+
}
184+
const double actual_net = host.raw_sum_before_extension();
185+
replay_rq::require(std::memcmp(&actual_net, &expected_net, sizeof(double)) == 0
186+
&& std::memcmp(&actual_net, &presented_net, sizeof(double)) == 0,
187+
"decimal-price presented close fold differs from the booked total");
188+
replay_rq::require(report.equity_curve_len == 9,
189+
"decimal-price fixture has the wrong equity-point population");
190+
}
191+
148192
std::string single_line(std::string text) {
149193
for (char& value : text)
150194
if (value == '\n' || value == '\r') value = ' ';
@@ -158,7 +202,7 @@ int main() {
158202
// while the later guards still execute and print their own observations.
159203
for (const std::string shape : {
160204
"any", "fifo", "fifo_no_fees", "sort", "two_groups", "cancellation",
161-
"ordinary", "quote"}) {
205+
"ordinary", "quote", "nondyadic"}) {
162206
pf_strategy_t state = nullptr;
163207
pf_report_t report{};
164208
std::string failure;
@@ -172,7 +216,9 @@ int main() {
172216
"1", shape == "quote" ? "15" : "1", shape == "quote" ? 1 : 0,
173217
4, PF_MAGNIFIER_ENDPOINTS, &report);
174218
const auto& host = fixture(state);
175-
if (shape == "ordinary" || shape == "quote")
219+
if (shape == "nondyadic")
220+
check_nondyadic_shape(host, report);
221+
else if (shape == "ordinary" || shape == "quote")
176222
check_terminal_shape(shape, host, report);
177223
else
178224
check_global_shape(shape, host, report);

0 commit comments

Comments
 (0)