Skip to content

Close stale deferred flip entries without reopening#83

Merged
luisleo526 merged 1 commit into
mainfrom
fix/same-id-stop-reissue-cross-ordering
Jul 9, 2026
Merged

Close stale deferred flip entries without reopening#83
luisleo526 merged 1 commit into
mainfrom
fix/same-id-stop-reissue-cross-ordering

Conversation

@luisleo526

Copy link
Copy Markdown
Collaborator

Summary

  • track the position side present when a priced entry is created or replaced
  • make stale opposite entries close-only after the position cycle changes
  • add focused deferred-flip carry regression coverage

Validation

  • full configured build and ctest: 83/83
  • corpus: 252/252, 239 excellent / 12 strong / 1 anomaly; target cAbs 22 -> 2
  • combined full 412-strategy scraper sweep: stable error inventory and 0 tier moves
  • all 26 KI-62 dual-stop fixtures remain excellent

A pending stop/limit ENTRY that reached its trigger while an OPPOSITE
position it did NOT open was live performed a full reversal (close the
opposite + open its own direction) whenever created_position_side != FLAT.
The close-only path in apply_entry_order_fill fired only for the
created_position_side == FLAT bracket (probes 80-87), so a same-id stop
armed during a PRIOR position cycle — one that survives a flip to the
opposite side and later triggers against that new position — reopened a
fresh position at the stale stop level.

TradingView instead closes the flipped position and re-arms the entry: the
reduce leg fires at the touch, the open leg is superseded by the same-bar
same-id re-issue and re-arms at the modified level (filling later, or never).

Fix: gate close_only on `created_position_side != position_side_` — a
reduce-only flip for any priced entry not placed in the cycle of the
position it now reverses (covers both the flat bracket and the deferred-flip
carry). The created != FLAT case routes through
flip_market_position_to(close_only=true), which closes the whole opposite
position and stays flat instead of opening. A genuine same-cycle reverse
(created_position_side == the reversed side) still opens its new leg;
deferred-flip entries that fire from FLAT are untouched.

Validation:
- ctest 82/82 (adds test_deferred_flip_carry_close_only, fails pre-fix).
- Corpus 239 excellent / 12 strong / 1 anomaly (unchanged tally).
- Deferred-flip / same-id sibling probes byte-identical.
- Only 3 percent-of-equity / cash strategies change, in equity-sized qty at
  the ~1e-6 float level (identical entry/exit times + prices); all tiers,
  coverage, and gated deltas preserved.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@luisleo526 luisleo526 merged commit 2e9f39f into main Jul 9, 2026
5 checks passed
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