Skip to content

An entry sized against pending entries that did not fill uses its own quantity - #436

Merged
alexgrover merged 1 commit into
devfrom
fix/entry-pending-reversal-qty
Oct 15, 2026
Merged

alexgrover merged 1 commit into
devfrom
fix/entry-pending-reversal-qty

Conversation

@alexgrover

@alexgrover alexgrover commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

A strategy.entry queued after an opposite market entry on the same bar is sized as a reversal of the position that entry would open. When that entry doesn't fill (for example, rejected for margin), the reversal size is wrong.

Example: a short of 2 rejected for margin, then a long of 1. PineTS sized the long as 1 + 2 = 3, which was rejected too, so nothing traded. TradingView fills the long with its own quantity of 1 (probes o07 / o09, BINANCE:BTCUSDT 1h).

Change

The entry records that its reversal size came from pending orders rather than the actual position (_reversesPending). If the position is flat when it fills, it uses its own quantity. A reversal sized against an actual position keeps its frozen quantity, as before (TradingView behavior established in #316).

Not included: two opposite entries that both fill (case 29)

This PR started as a fix for controlled case 29 (a market short then a market long, from flat, on one bar), where TradingView fills only the short. A rule that dropped the second entry fixed case 29 and four probes, but broke three premium backtester runs: there, TradingView does reverse on the same bar.

More probes show the outcome depends on capital and margin:

  • with 100,000 of capital at 100% margin, the second entry doesn't fill;
  • with 1,000,000 of capital or margin 0, the long fills first (even when placed second) and the short then closes part of it.

That needs a proper model of TradingView's execution order and margin check for same-bar orders, so it's left for a separate investigation.

Tests

tests/namespaces/strategy/opposite-entries.test.ts (2 tests): the long-after-rejected-short case and its mirror. Both fail on dev.

Results against TradingView (compared with dev)

  • Probes o07 and o09: 0 trades → identical.
  • Controlled cases and premium backtesters: no change.
  • Public strategies: ict-daily-liquidity-sweep (already diverging at trade 0) no longer opens the spurious same-bar long that dev opened on bar 2377. It now has TradingView's short there; the trade count changes from 53 to 55 on the already-diverged ledger.
  • Unit suite: all committed tests pass. Oracle suite: 0 regressions.

Note

Medium Risk
Changes market entry fill sizing in the strategy engine; incorrect logic would skew backtests, but the change is narrow and covered by new tests.

Overview
Fixes same-bar opposite strategy.entry orders when the first order never opens a position (e.g. margin rejection) but the second was queued as a reversal of the first’s projected size.

strategy.entry now tags those orders with _reversesPending when reversal sizing came only from earlier market entries on the bar, not from an actual open position. fillEntryOrder resets quantity to _base_qty at fill time if the account is still flat—so a long of 1 after a rejected short of 2 fills 1, not 3. Reversals against a real position keep the existing frozen reversal qty (#316).

Adds opposite-entries.test.ts (margin-too-tight mirror cases). Does not change behavior when both opposite entries fill on one bar (left for follow-up).

Reviewed by Cursor Bugbot for commit 40f3f8b. Bugbot is set up for automated code reviews on this repo. Configure here.

… quantity

A strategy.entry queued after an opposite market entry on the same bar is
sized as a reversal of the position that entry would open. When that
entry does not fill (rejected for margin), the reversal size is wrong:
a short of 2 rejected for margin then a long of 1 gave a long of 3,
rejected too, so nothing traded. TradingView fills the long with its own
quantity (probes o07 / o09). Such an entry now falls back to its own
quantity when the position is flat at its fill.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor

cursor Bot commented Oct 15, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: ff01fcac-ad0d-4d01-a264-9f3eaf55bd22)

@alexgrover
alexgrover merged commit 896431e into dev Oct 15, 2026
2 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant