Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(margin): floor restore-qty to lot step BEFORE the 4x over-liquidation (KI-31)#73

Merged
luisleo526 merged 1 commit into
mainfrom
fix/ki31-margin-nibble-floor-order
Jul 7, 2026
Merged

fix(margin): floor restore-qty to lot step BEFORE the 4x over-liquidation (KI-31)#73
luisleo526 merged 1 commit into
mainfrom
fix/ki31-margin-nibble-floor-order

Conversation

@luisleo526

Copy link
Copy Markdown
Collaborator

Bug

process_margin_call computed the forced-liquidation lot as floor(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)

  1. Floor q_min to qty_step_ before qty_liq = 4 * q_min.
  2. Epsilon (+1e-6) the now-redundant second floor of 4*q_min (same guard as quantize_qty): 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 — desyncing the tail nibbles from ~step 14.

Verification (fresh-context R7 = GO)

  • Nibble fidelity: alpha-wizard-channel percent_of_equity=100 cascade-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.
  • Corpus BYTE-IDENTICAL: full run_corpus.sh → submodule completely clean, 0 changed outputs, tiers 239 exc / 12 strong / 1 anomaly unchanged. qty_step_ == 0 for the entire corpus + the explicit-leverage p2/5x margin probes (only verify-engine-local passes 0.0001), so both floor branches are guarded no-ops there.
  • ctest 79/79.

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_equity margin model to bit-exact TV parity for its own sake.

🤖 Generated with Claude Code

…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]>
@luisleo526 luisleo526 merged commit 15b02e0 into main Jul 7, 2026
0 of 5 checks passed
@luisleo526 luisleo526 deleted the fix/ki31-margin-nibble-floor-order branch July 7, 2026 06:38
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant