Skip to content

Preserve frozen reversal quantities and trim underfunded opens#132

Merged
luisleo526 merged 1 commit into
mainfrom
codex/abhivish-affordability-production-20260723
Jul 22, 2026
Merged

Preserve frozen reversal quantities and trim underfunded opens#132
luisleo526 merged 1 commit into
mainfrom
codex/abhivish-affordability-production-20260723

Conversation

@luisleo526

Copy link
Copy Markdown
Collaborator

What changed

  • Preserve signal-time-quantized default quantities through ordinary MARKET reversals instead of flooring the same contracts value a second time.
  • Track a narrowly scoped omitted-quantity, 100%-of-equity short-to-long reversal event.
  • When that fill leaves a positive but sub-lot affordability deficit, apply TradingView's one-contract post-fill margin trim.
  • Keep explicit quantities, priced/RAW orders, other sizing modes, and unrelated margin paths unchanged.
  • Add boundary and negative-control regression tests for both behaviors.

Root cause

Default-sized MARKET orders are frozen and exchange-quantized at signal time. The ordinary reversal path sent that frozen binary64 value through the quantity-step floor again, which could reduce it by one lot. Correcting that exposed a second broker edge: TradingView closes one whole contract when an otherwise eligible 100%-equity reversal becomes microscopically underfunded at the next-open fill, even when the minimum restore amount is below one quantity step.

Validation

  • Engine suite: 119/119 passed
  • Focused quantity-freeze tests: 60/60 passed
  • Focused margin-call tests: 315/315 passed
  • Mutation proof: disabling the new reversal trim makes the natural-event assertions fail 7/7, while the explicit-quantity control remains 3/3 passed
  • Production target + six collateral strategies: 7/7 exactly matched the preregistered R+B tapes and metrics, with no experimental matrix flags
  • Forced-no-cache scraper sweep: 416/416 classified, 0 UP / 0 DOWN
    • board unchanged: 361 Excellent / 40 Strong / 4 Moderate / 2 Weak
  • Engine corpus: 252/252 ran successfully
    • 247 Excellent / 4 Strong / 1 documented anomaly
  • Independent implementation review found no correctness or public C ABI issue

This is intentionally score-neutral: it fixes two proven broker lifecycle defects without promoting broader experimental affordability rules that still have unresolved collateral interactions.

Copilot AI review requested due to automatic review settings July 22, 2026 17:07
@luisleo526
luisleo526 merged commit 779f14b into main Jul 22, 2026
8 checks passed
@luisleo526
luisleo526 deleted the codex/abhivish-affordability-production-20260723 branch July 22, 2026 17:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines TradingView-compatibility for default-sized MARKET reversals by (1) preserving signal-time frozen/quantized quantities through reversal execution (avoiding a second qty-step floor) and (2) modeling a narrowly scoped TradingView “one-contract trim” margin behavior for a specific underfunded 100%-equity SHORT→LONG reversal case, with regression coverage for both behaviors.

Changes:

  • Preserve pre-quantized frozen default quantities through ordinary MARKET reversal execution (avoid a second qty-step floor).
  • Add a tightly-scoped margin-call/affordability event flag to trigger TradingView’s “close exactly 1 contract” trim when a 100%-equity omitted-qty SHORT→LONG reversal becomes slightly underfunded post-fill.
  • Add boundary + negative-control tests for frozen-quantity reversals and for the underfunded reversal trim behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/engine_orders.cpp Threads explicit_qty_prequantized into the flip-reversal path so frozen MARKET reversal qty isn’t floored a second time.
src/engine_fills.cpp Adds a reversal-specific affordability event flag and uses it to enable the one-contract “floor-zero fallback” trim in margin-call processing.
include/pineforge/engine.hpp Introduces opening_affordability_default_long_reversal_ state and extends the flip helper signature to accept the prequantized-qty flag.
tests/test_margin_call.cpp Adds a pinned regression covering the one-contract post-fill trim for the underfunded omitted-qty 100%-equity reversal scenario (plus explicit-qty negative control).
tests/test_default_qty_signal_freeze.cpp Adds reversal boundary regressions to ensure frozen default qty survives a MARKET reversal without being refloored (plus explicit-qty control).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/engine_orders.cpp
Comment on lines +925 to +932
// Default-sized MARKET quantities are frozen and exchange-quantized at
// signal time. Every sibling dispatch path preserves that provenance;
// ordinary flips must not feed the frozen contracts through qty_step a
// second time (binary64 can turn 1.3410 into 1.3409 on the second floor).
// Explicit/FIXED quantities keep their normal single fill-side floor.
double new_qty = explicit_qty_prequantized
? explicit_qty
: calc_qty_for_type(fill_price, explicit_qty, explicit_qty_type);
Comment on lines +456 to +460
// J/K. The same binary boundary must survive an ordinary in-position MARKET
// reversal. The default-sized long is quantized when it is placed; the later
// short->long flip must consume that frozen contracts value directly. The
// adjacent explicit-qty reversal remains un-frozen and therefore still gets
// exactly one ordinary fill-side floor.
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.

2 participants