fix(margin): floor restore-qty to lot step BEFORE the 4x over-liquidation (KI-31)#73
Merged
Merged
Conversation
…tion (KI-31)
process_margin_call computed the forced-liquidation lot as
qty_liq = floor(4 * (qty - equity/(adverse*pv*m)) / step) * step
i.e. it applied the 4x over-liquidation to the RAW minimum-restore qty and only
floored the product. TradingView floors the minimum-restore qty to the
instrument lot step FIRST, then multiplies by 4. Flooring after injects a
~step/4 error into the first nibble that compounds ~3x per step through the
margin-call cascade.
Two coupled corrections:
1. floor q_min to qty_step_ before `qty_liq = 4 * q_min`.
2. the (now redundant) second floor of 4*q_min uses the engine's standard
+1e-6 epsilon (as quantize_qty does): 4*(a lot multiple) is mathematically
a lot multiple, but binary float makes e.g. 4*5.7089 = 22.83559999… which a
bare std::floor drops a whole lot. Without the epsilon the tail nibbles
desync from ~step 14 on.
Row-diff vs the ETHUSDT.P export (alpha-wizard-channel percent_of_equity=100,
cascade-1): 19/19 margin-call nibbles now bit-exact (7.7232/30.3796/35.716/…);
the old floor-after matched 0/19 and desynced by step 7.
Blast radius: qty_step_ == 0 for the entire corpus (only verify-engine-local
passes 0.0001 for the ETHUSDT.P scraper feed); the explicit-leverage p2/5x
margin probes never set it. So corpus + those probes are BYTE-IDENTICAL — this
only sharpens the ETHUSDT.P percent_of_equity margin-cascade nibbles. Note: it
does NOT change any scraper strategy's tier (the ~40 margin-cascade members fail
on entry-coverage / are KI-28 indicator-rewrite provenance-gapped, not on
margin-exit fidelity) — this is a correctness/fidelity fix, score-neutral.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
pull Bot
pushed a commit
to Malumbo21/pineforge-engine
that referenced
this pull request
Jul 7, 2026
…(KI-31) test_short_margin_call_qty_step asserted the old floor-AFTER value (first forced lot 3.5 = floor(4*shortfall/step)*step). PR pineforge-4pass#73 corrected the engine to floor the restore qty to the lot step BEFORE the 4x over-liquidation (matching TradingView, 19/19 alpha-wizard nibbles bit-exact). The synthetic scenario's first lot is now 4*floor(0.95238095/0.5)*0.5 = 4*0.5 = 2.0 (still an exact step multiple, still <= the 3.80952381 unquantized cap). Test-only; ctest 79/79. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
process_margin_callcomputed the forced-liquidation lot asfloor(4 * q_min / step) * step— it applied the 4× over-liquidation to the raw minimum-restore qty and floored only the product. TradingView floors the restore-qty to the instrument lot step first, then multiplies by 4. Flooring after injects a ~step/4 error into the first margin-call nibble that compounds ~3×/step through the cascade.Fix (two coupled corrections)
q_mintoqty_step_beforeqty_liq = 4 * q_min.+1e-6) the now-redundant second floor of4*q_min(same guard asquantize_qty):4 × (a lot multiple)is mathematically a lot multiple, but binary float makes e.g.4*5.7089 = 22.83559999…which a barestd::floordrops a whole lot — desyncing the tail nibbles from ~step 14.Verification (fresh-context R7 = GO)
percent_of_equity=100cascade-1 now 19/19 bit-exact (7.7232 / 30.3796 / 35.716 / … / 22.8356 / …) vs the ETHUSDT.P export; old floor-after matched 0/19 and desynced by step 7.run_corpus.sh→ submodule completely clean, 0 changed outputs, tiers 239 exc / 12 strong / 1 anomaly unchanged.qty_step_ == 0for the entire corpus + the explicit-leverage p2/5x margin probes (onlyverify-engine-localpasses 0.0001), so both floor branches are guarded no-ops there.Scope note (honest)
This is a correctness/fidelity fix, score-neutral. It does not change any scraper strategy's tier: the margin nibbles are exit rows while the grader matches on entries, and the ~40 margin-cascade members fail on entry coverage / are KI-28 indicator-rewrite (provenance-gapped) — not on margin-exit fidelity. It sharpens the ETHUSDT.P
percent_of_equitymargin model to bit-exact TV parity for its own sake.🤖 Generated with Claude Code