Preserve frozen reversal quantities and trim underfunded opens#132
Merged
luisleo526 merged 1 commit intoJul 22, 2026
Merged
Conversation
There was a problem hiding this comment.
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 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. |
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.
What changed
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
This is intentionally score-neutral: it fixes two proven broker lifecycle defects without promoting broader experimental affordability rules that still have unresolved collateral interactions.