Repository navigation
An entry sized against pending entries that did not fill uses its own quantity - #436
Merged
Merged
Conversation
… 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>
Bugbot couldn't run - usage limit reachedBugbot 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) |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
A
strategy.entryqueued 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:
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 ondev.Results against TradingView (compared with
dev)devopened on bar 2377. It now has TradingView's short there; the trade count changes from 53 to 55 on the already-diverged ledger.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.entryorders 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.entrynow tags those orders with_reversesPendingwhen reversal sizing came only from earlier market entries on the bar, not from an actual open position.fillEntryOrderresets quantity to_base_qtyat 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.