fix(orders): close(id) closes logical unclosed qty, not physical lots (grid bots)#49
Merged
Merged
Conversation
… (grid bots)
strategy.close(id) under the default FIFO close-entries rule summed every
physical open lot carrying `id` to decide the close quantity. TradingView
instead closes the quantity entered under `id` that a prior close(id) has not
already targeted — it does not re-sum physical lots. The two agree when each
id maps to one open lot, but diverge for grid bots that re-use a single entry
id across sequential buy/sell cycles: the FIFO trade-record drain removes the
oldest lot (often a different id), leaving the id-tagged lot physically open,
so the next close(id) double-counts it (over-close) — and a TP whose id-lot
was already drained finds no physical match and is skipped (under-close).
Add a per-id `id_unclosed_qty_` ledger ("entered qty for id minus
already-closed-by-close(id) qty") maintained at every entry site and consumed
by compute_close_target_qty under the FIFO rule only. The ANY rule keeps the
physical id-matched path untouched.
3commas grid corpus: xrp-grid 26.7%->99.3%, pol-grid 21.5%->99.1% trade-for-
trade match vs TradingView (price-exact 100%). Corpus held 251 excellent / 1
anomaly; ctest 76/76 incl. new test_strategy_close_reused_id_closes_one_logical_slot
(fails pre-fix). No new C-ABI export.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
strategy.close(id)under the default FIFO close-entries rule summed every physical open lot carryingidto decide the close quantity. TradingView instead closes the quantity entered underidthat a priorclose(id)has not already targeted — it does not re-sum physical lots.The two agree when each id maps to one open lot (the common case), but diverge for grid bots that re-use a single entry id across sequential buy/sell cycles:
close(id)double-counts it → over-close;Net effect on the 3commas grid corpus: wrong trade count + PnL (xrp-grid 143 vs TV 153; exit-price p90 ~18×).
Fix
Add a per-id
id_unclosed_qty_ledger — "entered qty for id minus already-closed-by-close(id)qty for id" — maintained at every entry site (fresh open, pyramid add, raw fill) and consumed bycompute_close_target_qtyunder the FIFO rule only. The ANY rule keeps the physical id-matched path untouched. No new C-ABI export (in-engine state only).Validation
test_strategy_close_reused_id_closes_one_logical_slot(fails pre-fix)python3 scripts/check_c_abi_runtime.pyexits 0 (no symbol change).🤖 Generated with Claude Code