Skip to content

fix(trading): a refused order is not a successful submit - #255

Closed
carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/refused-order-is-not-a-success
Closed

carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/refused-order-is-not-a-success

Conversation

@carlito-hummingbot

Copy link
Copy Markdown

The defect

POST /trading/orders answered 201 submitted for an order the venue had refused.

AccountsService.place_trade returns connector.buy() / connector.sell(), and those calls are fire-and-forget: they register the order with the tracker and return the client order id, while the venue call runs in the connector's own task. A refusal therefore lands on the in-flight order after the method has returned, and nothing on the request path ever looks at it.

Reproduced against Derive v3 with a funded subaccount whose risk universe does not contain the pair:

POST /trading/orders {"trading_pair":"SOL-USDC","trade_type":"BUY","order_type":"LIMIT","amount":0.1,"price":70}

HTTP 201 {"order_id":"0x860c500b8d7dd08491848427773f3f38","status":"submitted", ...}

Nothing on the venue had that id. The venue's answer was in the container log only:

DerivePerpetualDerivative - NETWORK - Error submitting buy LIMIT order to Derive_perpetual for 0.100 SOL-USDC 70.000.
OSError: Error submitting order 0x860c500b...: code=-32602 Invalid params
  (subaccount 121338 is in risk universe 1 but instrument SOL-PERP is in risk universe 3)

An agent reading that response records exposure it does not have.

The fix

place_trade now waits for the submission to resolve before answering:

failure = await self._await_submission(connector, order_id)
if failure:
    raise HTTPException(status_code=502, detail=f"{connector_name} refused the {trade_type.name} order for {trading_pair}: {failure}")

Three details matter, and the first two were wrong in my own first attempt:

  1. Read all_orders, not just in_flight_orders. The tracker drops an order the moment it reaches a terminal state, so a refused order is precisely the one missing from the live map. Checking only in_flight_orders reports every refusal as "not tracked".
  2. Absence is not an answer. Connectors register an order asynchronously — Derive's failure appears roughly 0.4 s after buy() returns — so the check polls to a bounded deadline (SUBMISSION_TIMEOUT_SEC = 15 s) instead of treating the first miss as success.
  3. Unconfirmed stays a success. If the order is still PENDING_CREATE at the deadline, the request keeps its 201: the order may well be live, and failing it would be the opposite error.

InFlightOrder in this version carries no failure text (only is_failure), so the detail degrades to "the venue rejected the order" when a connector exposes no reason; the connector still logs the venue's own error.

Verified live

Same account, same endpoint, before and after:

SOL-USDC   (universe 3, subaccount is universe 1)
  before   201 submitted  {"order_id":"0x860c500b...", "status":"submitted"}
  after    502  "derive_perpetual refused the BUY order for SOL-USDC: the venue rejected the order"

ETH-USDC   (universe 1 — accepted by the venue)
  before   201 submitted
  after    201 submitted        (no false failure)

Book left clean: 0 open orders, 0 positions.

Tests

test/test_place_order_refusal.py — 8 tests, and 4 fail on main:

test without fix with fix
refused order raises with the venue reason fail pass
refusal without a reason still fails fail pass
order registered late is still checked fail pass
failed order that left the in-flight map is caught fail pass
accepted order returns its id pass pass
unconfirmed order is still a success pass pass
untracked order is not a failure pass pass
sell path checks the outcome too pass pass
/opt/conda/envs/hummingbot-api/bin/python -m pytest test/test_place_order_refusal.py -q
8 passed

Not addressed

Reporting the venue's full error text through the API would need the connector to keep it on the order (InFlightOrder has nowhere to put it in this version). The classification here is deliberately conservative: refused, accepted, or unconfirmed — never "submitted" for something the venue threw away.

@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no outstanding findings.

Summary

place_trade waits for the connector’s answer and returns 502 for a confirmed refusal. Accepted orders and orders still unconfirmed at the deadline keep their successful response.

  • The order endpoint checks the venue’s result before reporting success.

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

Comment thread test/test_place_order_refusal.py Resolved
@carlito-hummingbot
carlito-hummingbot force-pushed the fix/refused-order-is-not-a-success branch from 5e99951 to 636d10d Compare November 13, 2026 14:05
Rebased onto main; the conflict was resolved by keeping this PR's refusal check with main's
surrounding code. `AccountsService.place_trade` returned `connector.buy()`/`sell()`, which are
fire-and-forget, so `POST /trading/orders` answered 201 for an order the venue had refused.
@carlito-hummingbot
carlito-hummingbot force-pushed the fix/refused-order-is-not-a-success branch from 636d10d to 6fea831 Compare November 16, 2026 09:14
After the rebase conflict was resolved by hand, the file needed isort and one log
call was 149 characters (flake8 limit 130). Formatting only.
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 refusal check is in that PR together with #252 and #257.

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