Courses / Commodity & FX Hedging
Building against an order gateway — the failure modes (engineering)
Optional module for developers: order identity, safe retries, reading execution reports, and recovering when messages go missing.
Lesson 1 · free to read
Order identity, and how to retry without hedging twice
An order carries three different identifiers, and a surprising number of defects come from holding the wrong one.
- Your request id. You create it and send it with every request. In FIX this is the ClOrdID. It is unique per request, not per order: an amend or a cancel is a new request with a new id, linked back to the previous one (OrigClOrdID in FIX).
- The gateway's order id. Assigned when the gateway accepts the order, and stable for the whole life of the order, across every amend. This is the handle to store against your hedge record.
- The exchange or broker's order id. Arrives only once the order reaches the market. Some operations, including cancel, may not be possible until it exists.
The timeout problem. You send a sell order for 10 lots and the connection drops before any reply. Did the order arrive? You cannot tell. The two possible reactions have very different outcomes:
- Resend with a new request id. If the first one did arrive, you now have two live orders and the book is hedged twice. On a fast market that is a real loss before anyone notices.
- Resend with the same request id. If the first one arrived, the gateway recognises the id and rejects the duplicate. That rejection is good news: it tells you the order exists. If it did not arrive, the resend is accepted as normal.
So the request id must be generated once, written to your database before the order is sent, and reused on every retry. This makes the request idempotent: sending it twice has the same effect as sending it once. If your gateway does not reject duplicate request ids, find out what it offers instead before writing any retry logic.
Cancels have their own races.
- Cancel before the order is accepted. You may not be able to cancel an order that has no exchange id yet. The cancel has to wait, and your system must hold the intention to cancel rather than drop it.
- Cancel crossing a fill. You send a cancel just as the order fills. You receive "too late to cancel", then the fill. This is normal. The fill stands, and the position must reflect it.
Rate limits. Gateways cap how many messages you may send per second. A sweep that recalculates the whole book at the market open can produce a burst far above that. Queue outgoing orders, throttle them yourself, and let cancels jump the queue, because a cancel that waits behind 200 new orders is no longer a cancel.
The rest of this module
- Reading execution reports without corrupting the bookTake the cumulative figures as the truth, expect duplicates and corrections, and accept messages in any order.
- When messages go missing, and how to know you are doneDetect gaps, re-query and compare, keep the connection alive yourself, and test the failures on purpose.
Carry on with the course
The other lessons and the quiz are in the course itself, where your progress is saved and counts towards the certificate: Commodity & FX Hedging Operations.
Next module: Metals pricing — quotational periods, payable metal and final invoices