Repository navigation
Stop-limit entries after an exit on the path; exits for another entry survive a flat position - #438
Merged
Merged
Conversation
… survive a flat position - A stop-limit entry against the open position, reached on the bar's path after an exit of that position, now waits for the exit like limit and stop entries do (it fills at the price computed for it, which a later pass would otherwise recompute from its triggered state). - When an exit leaves the position flat, only the exits for the closed trades are cancelled (from_entry empty or one of those entries); an exit for another entry, placed while the position was open, stays. TradingView (controlled case 22): on a bar rising through a short's stop (64,614) and a pending stop-limit long's stop (64,664), the short's exit fills first, then the long, then the long's take-profit (its exit placed while the short was open). Case 22 becomes identical. 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: 8c4a2926-9652-42ec-9f7c-d8b03d4a76af) |
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.
Controlled case 22 (stop-limit entries with exits), measured on TradingView (BINANCE:BTCUSDT 1h).
The bar rises through a short's stop-loss (64,614) and through a pending stop-limit long's stop (64,664). On TradingView:
strategy.exitwas placed while the short was still open.PineTS filled the long first, as a reversal that closed the short, and the take-profit only on the next bar.
Changes
from_entry, or whosefrom_entryis one of the closed entries. An exit for another entry, placed while the position was open, stays. The rule exists to stop exits of a closed entry from attaching later with stale levels, and still does.Tests
tests/namespaces/strategy/stop-limit-after-exit.test.ts: the short's stop, then the stop-limit long, then the long's take-profit, all on one bar. It fails ondev.Results against TradingView (compared with
dev)calc_on_order_fills, sizing, opposite orders), public strategies, premium backtesters: no change.Also checked: EURUSD 1h (5 controlled runs)
The five EURUSD runs all diverge on one signal: the 10- and 30-bar SMAs are mathematically equal on bar 496 (1.171326). TradingView's floating-point sums leave the fast one 2e-16 above, so it counts a crossover. PineTS rounds TA outputs to 10 decimals, so they're equal and the crossover comes one bar later. This is a numerical tie, not a broker difference, and isn't changed.
Note
Medium Risk
Changes core order-fill sequencing and exit cancellation in
processStrategyOrders/processExitOrders; behavior is targeted at TV parity but affects edge cases with opposing entries and bracket survival across flat periods.Overview
Aligns intrabar fill ordering with TradingView when a bar hits both an opposing exit and a pending stop-limit entry (controlled case 22).
Stop-limit entries now use the same defer-until-after-exits behavior as limit/stop reversals: if an exit leg is reached earlier on the bar path, the entry is deferred and its computed fill price is stored (
_deferredFillPrice) so the second pass does not re-trigger at the open. Path-position logic for deferral also treats stop-limit like stop (not limit) for rising/falling along the path.When a bar’s exits flatten the position, pending exit orders are cancelled only for the entries that actually closed on that bar (
from_entryin the closed-trades set), not every exit that was placed while any position was open—so a long’sstrategy.exitqueued during an open short can still fill the long entered later on the same bar.Adds
stop-limit-after-exit.test.ts: short stop → stop-limit long → long take-profit on one bar.Reviewed by Cursor Bugbot for commit 51e2bf9. Bugbot is set up for automated code reviews on this repo. Configure here.