Skip to content

fix: reconcile exchange-native flat closes and OKX leverage 59669 - #274

Open
Yhc-777 wants to merge 2 commits into
OpenByteInc:mainfrom
Yhc-777:contrib/live-trading-reliability-fixes
Open

Yhc-777 wants to merge 2 commits into
OpenByteInc:mainfrom
Yhc-777:contrib/live-trading-reliability-fixes

Conversation

@Yhc-777

@Yhc-777 Yhc-777 commented Oct 2, 2026 •

Copy link
Copy Markdown

Summary

This PR hardens live-trading reconciliation for two production failure modes observed while running OKX swap strategies on QuantDinger:

  1. Ghost strategy positions after exchange-native closes
    Exchange-side TP/SL (algo), liquidation, ADL, or manual flat closes often never create a pending_orders row, 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 a close_* fill.

  2. OKX 59669 blocking reduce-only closes / leverage setup
    OKX rejects set leverage when 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

  • N/A (bugfix from production/demo OKX runs; happy to file a tracking issue if maintainers prefer)

Changes

A) Exchange-native flat reconcile + flat-ledger purge

  • Add external_flat_close.py to backfill close_* trades when the venue is flat but the strategy ledger still has residual open size.
  • Extend trade_close_reason with EXCHANGE_NATIVE_CLOSE / EXCHANGE_FLAT_RECONCILE / EXCHANGE_LIQUIDATION / EXCHANGE_ADL.
  • Teach position sync to purge confirmed-flat local legs, with a grace window (POSITION_SYNC_PURGE_GRACE_SEC, default 120s) so a transient flat snapshot cannot delete a leg that just had an open/add fill.
  • Allow record_trade(..., created_at=) so reconciled historical closes keep the venue close timestamp when available.
  • Feature flag: POSITION_SYNC_PURGE_FLAT_LEDGER (default true).

B) OKX leverage / algo interaction (59669)

  • Cancel conflicting algo orders and retry leverage setup on OKX 59669.
  • Pre-clear algo orders during derivatives account configuration where needed.
  • Defer / requeue pending orders that fail on leverage setup instead of leaving them stuck in a dead processing state.
  • Support list JSON bodies for OKX cancel-algo payloads used by the recovery path.

Test plan

  • Relevant pytest tests pass:
    • tests/test_position_sync_flat_purge.py
    • tests/test_external_flat_close.py
    • tests/test_okx_set_leverage_59669.py
    • tests/test_okx_cancel_algo_payload.py
    • tests/test_pending_order_leverage_retry.py
    • tests/test_derivatives_account_configuration.py
  • Staging: live OKX swap — open position, trigger exchange-native TP/SL, confirm L3 clears and a close trade is recorded
  • Staging: reproduce 59669 with an open algo order, confirm leverage setup / reduce-only close recovers after cancel-and-retry

API documentation (if routes/schemas changed)

  • N/A — no HTTP route / OpenAPI schema changes in this PR.

Screenshots (if UI change)

  • N/A — backend-only.

Roadmap contribution (if applicable)

  • N/A — targeted live-trading reliability bugfix, not a roadmap epic slice.

Notes / scope limits

  • External close price discovery is OKX-first in this PR. Non-OKX venues still get flat L3 purge; close-trade backfill may defer until a price/fill source is available.
  • This does not change the distributed Kafka bar runtime, CCXT removal path, billing, or frontend assets.
  • Happy to split into two PRs if maintainers prefer a narrower review surface.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant