Conversation
Prevent ghost strategy positions after exchange-native TP/SL, liquidation, or ADL, and record matching close trades for the strategy ledger. Also recover OKX reduce-only / leverage setup failures caused by open algo orders (error 59669) via cancel-and-retry and deferred pending-order requeue. Co-authored-by: Cursor <cursoragent@cursor.com>
Move recoverable 59669 / algo-order leverage retry out of pending_order_worker.py so CI backend quality baselines stay green. Co-authored-by: Cursor <cursoragent@cursor.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.
Summary
This PR hardens live-trading reconciliation for two production failure modes observed while running OKX swap strategies on QuantDinger:
Ghost strategy positions after exchange-native closes
Exchange-side TP/SL (algo), liquidation, ADL, or manual flat closes often never create a
pending_ordersrow, so the private-stream / pending-order path cannot attribute them. Local L3 legs can remain open forever even though the venue is flat, and the strategy trade ledger stays missing aclose_*fill.OKX
59669blocking reduce-only closes / leverage setupOKX rejects
set leveragewhen algo orders exist on the instrument (59669). That cascaded into pending-order retries and, in practice, blocked reduce-only swap closes that first needed account configuration / leverage setup.Both changes keep upstream's L3 ownership model: strategy positions are still produced by strategy-owned fills. The only L3 mutation added here is deleting legs that a fresh exchange snapshot confirms are flat, optionally after writing a reconciled close trade.
Related issue
Changes
A) Exchange-native flat reconcile + flat-ledger purge
external_flat_close.pyto backfillclose_*trades when the venue is flat but the strategy ledger still has residual open size.trade_close_reasonwithEXCHANGE_NATIVE_CLOSE/EXCHANGE_FLAT_RECONCILE/EXCHANGE_LIQUIDATION/EXCHANGE_ADL.POSITION_SYNC_PURGE_GRACE_SEC, default 120s) so a transient flat snapshot cannot delete a leg that just had an open/add fill.record_trade(..., created_at=)so reconciled historical closes keep the venue close timestamp when available.POSITION_SYNC_PURGE_FLAT_LEDGER(defaulttrue).B) OKX leverage / algo interaction (
59669)59669.Test plan
tests/test_position_sync_flat_purge.pytests/test_external_flat_close.pytests/test_okx_set_leverage_59669.pytests/test_okx_cancel_algo_payload.pytests/test_pending_order_leverage_retry.pytests/test_derivatives_account_configuration.py59669with an open algo order, confirm leverage setup / reduce-only close recovers after cancel-and-retryAPI documentation (if routes/schemas changed)
Screenshots (if UI change)
Roadmap contribution (if applicable)
Notes / scope limits