Order life cycle and the FIX protocol
A state machine for the order, then the FIX messages that carry it and the session that numbers them.
Lesson 814 min
By the end of this lesson
- Describe an order’s state machine and its forbidden transitions
- Recognise the FIX messages for an order and their default values
- Handle sequence numbers and duplicates in a FIX session
An order is not a fixed row: it is an object that changes state. Received, placed in the book, partly filled, then filled, cancelled, expired or rejected. Modelling these states as a state machine, with allowed and forbidden transitions, makes the code safer and the tests easier to write.
Order life cycle
The sequence of states of an order, from receipt to a final state: filled, cancelled, expired or rejected.
The state machine
The seven states below are this track's modelling choice. Three facts from the rules justify them: "day" validity is the default, and fixed-term validity does not exceed 30 days (BVMAC General Rules 2025, art. 162), hence the "expired" state; an order can be filled in several parts, hence "partly filled"; and it can be cancelled while it is waiting.
Trigger the events. Executions and cancellations come from a real Carnet: notice that a sell at 9,950 executes at 10,000, the price of the order already present.
- The brokerage firm has received the order. Pick an event.
In code, a transition table is enough, and a test checks that no forbidden transition gets through:
type Etat = 'recu' | 'rejete' | 'actif' | 'partiel' | 'execute' | 'annule' | 'expire'
const transitions: Record<Etat, Etat[]> = {
recu: ['actif', 'partiel', 'execute', 'rejete'],
actif: ['partiel', 'execute', 'annule', 'expire'],
partiel: ['partiel', 'execute', 'annule', 'expire'],
execute: [], annule: [], expire: [], rejete: [], // final states
}
const peutPasser = (de: Etat, vers: Etat) => transitions[de].includes(vers)
A received order can go straight to "filled": in continuous trading, an order that finds enough counterparty executes as soon as it enters.
The table says which transitions exist; the scene shows what triggers them. Each arc lights up with the engine call that causes it, and the tracked object changes a field. Compare the Cancel and Expiry scenarios: the same annuler leads to two different final states, because the engine does not know why it is called.
tracked order
receipt
Received
From memory: can a filled order be cancelled? What does Carnet.annuler return then?
The FIX protocol in brief
Between a brokerage firm and a market, orders and their states travel in messages. An open standard exists for this: FIX. It is developed by the FIX Trading Community, an independent non-profit standards body; its specifications are open and cover the whole life cycle, from pre-trade to settlement (FIX Trading Community, standards). The recommended version is FIX Latest; FIX 4.4 and 4.2 remain supported for existing systems (FIX protocol).
FIX protocol
An open standard for electronic trading messages, maintained by the FIX Trading Community.
Four messages are enough to follow the life cycle modelled here. Their type (MsgType) is a short value (Orchimate, FIX Latest):
- NewOrderSingle (D): a new order. Engine side,
Carnet.passer. - OrderCancelRequest (F): a cancel request. Engine side,
Carnet.annuler. - OrderCancelReplaceRequest (G): an amend request. The engine has none; remember that it loses priority if the limit changes or the quantity increases (Euronext Trading Manual, 2021, section 2). The lesson Amending and cancelling an order deals with this message.
- ExecutionReport (8): the return message, through which the sender of the order learns what becomes of it.
In NewOrderSingle, four fields are required: ClOrdID (11), Side (54), TransactTime (60) and OrdType (40). Price (44) is required for a limit order. TimeInForce (59) is optional, and its absence means a day order (Orchimate, NewOrderSingle). A gateway translates these fields into the engine's type:
// Translation sketch: coded values (Side, OrdType) are read from the specification
function versOrdre(m: NewOrderSingle): Omit<Ordre, 'rang'> {
return {
id: m.ClOrdID, // tag 11
sens: lireSens(m.Side), // tag 54
quantite: lireQuantite(m.OrderQtyData), // OrderQtyData component
limite: estLimite(m.OrdType) ? versEntier(m.Price) : null, // tags 40 and 44
}
}
The rank does not come from the message: the engine assigns it, on receipt. This is the choice made in Modelling an order: an integer rank rather than a time, assigned in a single place.
Follow a fictional order as messages: session opening, NewOrderSingle, reports, cancel request. At each arrival, look at the engine call the gateway derives from it.
Logon (A)
Logon (A) →
Session opening
The session: sequence numbers
A FIX session is a bidirectional stream of ordered messages, with a continuous series of sequence numbers starting at 1, in three stages: opening (Logon, type A), exchange, closing (FIX Session Layer). The session layer provides for resynchronising messages; the ResendRequest message (type 2) is used to ask for messages again. For a tester, this is a scenario in its own right: cut the connection in the middle of a session and check that no order is lost or processed twice.
FIX's text format (TagValue: "tag=value" pairs separated by a control character, opened by BeginString, BodyLength and MsgType, closed by a checksum) has only been confirmed by implementation guides: the official specification was not read. Do not build a parser on this basis without reading it. The fields of ExecutionReport, including the order's state, are read on Orchimate; this track does not copy them. Finally, we found no source on the BVMAC's use of FIX.
Going further
Store the state, or compute it?
The transition table assumes a stored etat field updated on each event. Design alternative: store only the facts (order quantity, quantity filled, presence in the book, possible end) and compute the state. Two sources of truth can then no longer diverge.
interface Suivi {
quantite: number
executee: number
enCarnet: boolean
fin: 'rejete' | 'annule' | 'expire' | null
}
function etat(s: Suivi): Etat {
if (s.fin === 'rejete') return 'rejete'
if (s.executee === s.quantite) return 'execute'
if (s.fin !== null) return s.fin
if (!s.enCarnet) return 'recu'
return s.executee > 0 ? 'partiel' : 'actif'
}
Step by step, with a real Carnet and order K-7 from the scene:
passer({ id: 'K-7', sens: 'achat', quantite: 100, limite: 10000 })returns[]:enCarnetistrue,etatgives'actif'.- A sell of 30 at 9,950 arrives: one execution of 30 at 10,000.
executeeis 30,etatgives'partiel'. annuler('K-7')returnstrue:finis'annule',etatgives'annule'.- A second
annuler('K-7')returnsfalse: nothing changes, the final state holds.
The order of the checks in etat matters: a fully filled order stays 'execute', even if a late cancellation is recorded afterwards.
The cost of the table can be measured too: 7 states give 49 possible pairs, 12 of them allowed ("partly filled" to "partly filled" included). A test that walks the 49 pairs with peutPasser also checks the 37 forbidden ones, which no example test does.
The same report, twice
After a disconnection, the FIX session layer provides for resynchronisation, and ResendRequest asks for messages again (FIX Session Layer). A gateway must therefore assume it may receive the same ExecutionReport twice. If it adds up naively, executee exceeds quantite.
const dejaVus = new Set<string>()
function appliquerExecution(s: Suivi, cle: string, quantite: number): Suivi {
if (dejaVus.has(cle)) return s // already counted: ignore it
dejaVus.add(cle)
return { ...s, executee: s.executee + quantite }
}
Which ExecutionReport field is the unique key of an execution? This track does not copy its fields: read them on Orchimate before choosing. The principle does not depend on FIX: an execution must be counted once, however many times it arrives.
From memory, lesson 4: a market order for 20 finds only 12 shares. What does the engine do with the remainder, and in which state does the order end?
Your turn
Read next: Orchimate, messages de FIX Latest (FIX Trading Community)
A question? Ask your teacher, or an AI assistant, giving it the title of this lesson.