Skip to content

fix(trading): cancel an order by venue id when the in-flight tracker lost it - #252

Closed
carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/cancel-by-exchange-order-id
Closed

carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/cancel-by-exchange-order-id

Conversation

@carlito-hummingbot

Copy link
Copy Markdown

Problem

cancel_order gates on the in-flight tracker:

if client_order_id not in connector.in_flight_orders:
    raise HTTPException(404, f"Order '{client_order_id}' not found in active orders")

in_flight_orders is an in-memory map owned by the running process. When a bot is stopped, crashes, or is archived while an order is resting, the order is still live at the venue — but the API has no way to reach it. There is no cancel route that accepts a venue-side id, so the order sits on the book until a human clears it by hand.

Reproduction (XRPL, live account)

deploy a bot                      -> offer 106553241 resting
docker kill <bot>                 -> process gone, offer still on the ledger
POST .../orders/106553241-1/cancel -> 404 "Order '106553241-1' not found in active orders"
ledger account_offers              -> 1 offer, still resting

Fix

Add the missing path, without changing behaviour for orders the process still tracks:

  • services/accounts_service.py — cancel_order_by_exchange_id(account_name, connector_name, exchange_order_id, trading_pair=None). If the tracker holds the order, it uses the existing connector.cancel(...) path, so tracked orders behave exactly as before. Otherwise it calls connector.cancel_by_exchange_order_id(...), and reports 501 when the connector has no such capability, 500 with the venue's cause when cancellation is rejected.
  • routers/trading.py — POST /{account_name}/{connector_name}/orders/by-exchange-id/{exchange_order_id}/cancel.

For XRPL the exchange order id starts with the offer sequence (<sequence>-<pair>). The connector side is a companion PR against hummingbot.

Tests

test/test_cancel_by_exchange_id.py — 4 passed:

Case Expectation
untracked order cancelled at the venue
tracked order existing path used, unchanged
connector without capability 501
venue rejection 500, cause surfaced
4 passed, 7 warnings in 3.96s

Verified end to end on a live XRPL account

Same orphan that the old route 404'd on:

OLD route: 404 {"detail":"Order '106553241-1' not found in active orders"}
NEW route: 200 {"success":true,"message":"Order '106553241' cancelled by exchange id"}
ledger:    0 offers

Who benefits

Every agent that can be stopped while orders are resting — which is all of them after a crash — with two Condor agents on XRPL (aegis on RLUSD-XRP / XRP-USDC, and an XRPL maker on RLUSD-XRP) able to use it today through their startup reconcile pass.

@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds a route to cancel orders by venue ID when they are no longer tracked.

  • Callers can cancel orders by venue ID, even when the process no longer tracks them.

Reviews (5) · Last reviewed commit: "style: restore the trailing newline thes..." · Reviewed by Greptile

Comment thread routers/trading.py Resolved
@carlito-hummingbot
carlito-hummingbot force-pushed the fix/cancel-by-exchange-order-id branch from 6479396 to 7aaabce Compare November 10, 2026 12:05
@carlito-hummingbot

Copy link
Copy Markdown
Author

Re: P2 "Cancellation appears complete too early" — fixed, thank you.

You are right: for a tracked order the service calls connector.cancel(...), which does safe_ensure_future(self._execute_cancel(...)) and returns immediately. Returning a bare "cancelled" was wrong — it claimed venue confirmation for something the API had only scheduled.

The response now carries the path and how far it actually got, and the message follows it:

// tracked order — work scheduled, not confirmed
{"success": true, "order_id": "hbot-123", "via": "tracker",
 "status": "initiated", "message": "Order 'hbot-123' cancellation initiated"}

// untracked order — cancellation transaction accepted by the venue
{"success": true, "order_id": "106553233", "via": "venue",
 "status": "submitted", "message": "Order '106553233' cancellation submitted to the venue"}

Neither wording claims the order is gone; the docstring says so explicitly ("Neither means the order is confirmed gone — check the venue for that"), which is also why the reconcile routine I use re-reads account_offers after cancelling rather than trusting the response.

Live check against a running API (venue path, non-existent sequence → temBAD_SEQUENCE, treated as already-gone):

{"success":true,"message":"Order '999999999' cancellation submitted to the venue",
 "order_id":"999999999","via":"venue","status":"submitted"}

Tests updated and extended to 5, all passing:

Case Assertion
untracked via=venue, status=submitted
tracked via=tracker, status=initiated
route wording (new) tracked message contains "initiated" and never "cancelled"
5 passed, 7 warnings in 3.75s

Rebased onto main. `cancel_order` gates on `connector.in_flight_orders`, an in-memory map,
so an order whose owning process is gone cannot be cancelled at all -- it rests and can still
fill. Adds `POST /{account}/{connector}/orders/by-exchange-id/{id}/cancel` and
`AccountsService.cancel_order_by_exchange_id`, which prefers the tracker and otherwise calls
the connector's `cancel_by_exchange_order_id`. The response says whether the cancel was
`initiated` (scheduled via the tracker) or `submitted` to the venue.
@carlito-hummingbot
carlito-hummingbot force-pushed the fix/cancel-by-exchange-order-id branch from 7aaabce to 67902cf Compare November 16, 2026 09:14
Two blank lines before the appended endpoint, as the repo's flake8
config requires. No logic change.
The previous push trimmed the final newline off every file it carried, so
`end-of-file-fixer` failed on files whose content was otherwise correct. Re-sent
byte-exact.

(For 8552 this is on top of the real fix: the tests now map the symbol the connector
needs, so the cancel path runs instead of raising KeyError.)
@carlito-hummingbot

Copy link
Copy Markdown
Author

Superseded by #258. The venue-id cancel is in that PR together with the refusal fix (#255) and the post-archive balance refresh (#257), which share services/accounts_service.py.

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