Repository navigation
Partial exits: exits share each trade's quantity in placement order - #432
Merged
Merged
Conversation
Several strategy.exit orders on one entry each closed up to their own qty / qty_percent of the position, so a full-size stop placed after a 50% target closed all of it. On TradingView exits share each trade's quantity in the order they were first placed: each takes its qty_percent of the trade's entry quantity, its absolute qty (oldest trades first) or the rest, out of what the exits placed before it and still working have not taken (a re-call keeps the exit's place). On whole contracts 50% of 1 contract takes the contract. Exit orders are also taken in the order the bar's path reaches their first leg. strategy.close with qty / qty_percent no longer cancels the exits of the entry (only a full close does), and a reversal takes the exits without from_entry placed for the reversed position with it. 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: daa806eb-38c9-4774-83ff-0ecc77197fbc) |
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.
Builds on #431 (merged): it uses its path helpers and per-trade trailing state.
Several
strategy.exitorders on one entry, measured on TradingView (BINANCE:BTCUSDT 1h, CME ES, Pine v6) with 11 new partial-exit probes.Changes
Exits share each trade's quantity in placement order (
exitCapacity). Each exit used to close up to its ownqty/qty_percentof the position, so a full-size stop placed after a 50% target closed everything. On TradingView, each exit takes its share out of what the exits placed before it, and still working, have not taken. The share is itsqty_percentof the trade's entry quantity, its absoluteqty(oldest trades first), or all the rest.qty = 3against 1 open takes all (pe05);Exit orders are taken in path order. When two exit orders trigger on one bar, the one whose first leg the path reaches first fills first (pe01/03/04).
strategy.closewithqty/qty_percentkeeps the exits working. Only a full close cancels the entry's pending exits. After a 50% close, the 50% target still fills on that bar (pe07).A reversal takes its exits with it. Exits without
from_entrythat were placed while a position was open die when a reversal closes that position, as they already did when it went flat. The new position gets the exits placed for it (pe11).Tests
tests/namespaces/strategy/partial-exits.test.ts(8 tests): the placement-order cases, over-allocation,qtyover the position, whole contracts, a re-called exit after a fill on an older trade, a partialstrategy.close, and the reversal. 7 fail on the base branch; "stop placed first takes all" guards existing behavior.Results against TradingView (compared with
dev, with #431 included)devdiverged from trade 0–3); pe11 first divergence trade 0 → 43.