Continuous trading, line by line
Reading Carnet.passer: walk the opposite side, stop at the right moment, store or drop the remainder.
Lesson 412 min
By the end of this lesson
- Follow the Carnet.passer loop line by line down to the remainder
- Predict the trade price the engine uses in continuous trading
- Explain what happens to a market order’s remainder and to the caller’s object
In this lesson4
In continuous trading, each incoming order is immediately matched against the opposite side of the book; it can produce zero, one or several trades, and its remainder is ranked by price then time priority (Euronext Trading Manual, 2021, section 3.2.1). In the teaching engine, this whole rule fits in one method: Carnet.passer. Let us read it.
Continuous trading
A trading mode in which each incoming order is matched at once against the opposite side of the book.
The BVMAC rules provide for this mode alongside the auction (BVMAC General Rules 2025, art. 14). Moving a security to continuous trading requires prior approval from COSUMAF (art. 136). Today, the hours published by the BVMAC describe an auction session, with one auction a day (session hours): continuous trading is a capability to build, not current practice.
The method, in four stages
passer(ordre): Execution[] {
// 1. Validate and number
const entrant = { ...ordre, rang: ordre.rang ?? this.prochainRang++ }
if (entrant.quantite <= 0 || !Number.isInteger(entrant.quantite)) throw new Error('…')
// 2. Walk the opposite side, from best to worst
const opposes = entrant.sens === 'achat' ? this.ventes : this.achats
let reste = entrant.quantite
for (const enAttente of trierCote(opposes, sensOppose)) {
if (reste === 0) break
if (!compatibles(achat, vente)) break
const prix = enAttente.limite ?? entrant.limite ?? this.dernierPrix
if (prix === null) break
const quantite = Math.min(reste, enAttente.quantite)
executions.push({ achat: achat.id, vente: vente.id, prix, quantite })
reste -= quantite
enAttente.quantite -= quantite
this.dernierPrix = prix
}
// 3. Remove exhausted orders
this.nettoyer()
// 4. Store the remainder of a limit order
if (reste > 0 && entrant.limite !== null) cote.push({ ...entrant, quantite: reste })
return executions
}
Three points make the mechanism:
- The two
breaks. The loop stops as soon as the quantity is filled, or as soon as the next order opposite is too expensive (or not expensive enough). Since the opposite side is sorted, no order further down could fit: stopping is correct, not just fast. - The trade price. The engine uses the price of the order already present:
enAttente.limitefirst. This is a design choice of the teaching engine; our sources do not set this rule for the BVMAC. - The mutation.
enAttente.quantite -= quantitechanges the order stored in the book. The engine then removes orders at zero withnettoyer().
Now watch the order itself go through the method. Its reste shrinks with each execution, each sell filled grows thinner then fades out, and the loop stops at the first sell that is too expensive. Look where the remainder is stored at the last step.
Sell side
Executions returned
[]
passer({ id: 'a1', achat, 50, 10050 })
A buy comes in
Follow the loop line by line
Three sell orders are resting: v1 (5 at 1,010), v2 then v3 (5 each at 1,005). Pick an incoming buy order, then step through line by line. The executions shown are those the engine returns.
1const opposes = trierCote(this.ventes, "vente")2for (const enAttente of opposes) {3 if (reste === 0) break4 if (!compatibles(achat, enAttente)) break5 const prix = enAttente.limite ?? entrant.limite ?? dernierPrix6 const quantite = Math.min(reste, enAttente.quantite)7 executions.push({ achat, vente, prix, quantite }); reste -= quantite8}9if (reste > 0 && entrant.limite !== null) this.achats.push(reliquat)
Sells are sorted: v2, v3, v1.
Sorted sells
- v2 · 5 × 1,005 rank 2
- v3 · 5 × 1,005 rank 3
- v1 · 5 × 1,010 rank 1
reste
8
executions
- []
From memory: a buy of 12 at 1,005 arrives against v2, v3 (1,005) and v1 (1,010). What happens?
A market order leaves no remainder
A market order has no limit. It is therefore compatible with any order opposite and only stops when quantity runs out. If some is left, the condition entrant.limite !== null keeps it out of the book: its remainder disappears.
The BVMAC General Rules 2025 say that the remainder of a market order is removed "on expiry" (art. 157 to 160). The teaching engine removes it at once, without waiting. The gap is known: a real engine will have to decide, and a test will have to pin it down.
The animated book below replays a full session with the same engine. The last order, a market sell, consumes the buy orders from best to worst.
Buy
- Empty book
Sell
- Empty book
Gap between best bid and best ask : ·
Trades
- No trade yet.
Going further
What is mutated, what is not. As the lesson The book, a sorted structure showed, trierCote copies the array but not the orders: enAttente.quantite -= quantite therefore changes the order stored in the book. The incoming order, for its part, is copied ({ ...ordre }), then its remainder is stored in another copy. The caller's object never changes:
const c = new Carnet(10000)
c.passer({ id: 'v1', sens: 'vente', quantite: 20, limite: 10000 })
const o = { id: 'a1', sens: 'achat', quantite: 50, limite: 10050 } as const
c.passer(o) // [{ achat: 'a1', vente: 'v1', prix: 10000, quantite: 20 }]
o.quantite // 50: the caller's object is intact
c.ordres().achats // [{ id: 'a1', …, quantite: 30, rang: 2 }]: a copy, with the remainder
A test that pins this behaviour protects callers: a gateway that reuses its objects after passer must not see them change behind its back.
A branch nothing reaches. The line if (prix === null) break covers one case: neither order has a limit and the book has no last price. Yet a market order never enters the book; the resting order therefore always has a limit, and prix is never null. Step by step, on a book with no reference price:
const c = new Carnet() // no reference price: c.prix is null
c.passer({ id: 'v1', sens: 'vente', quantite: 10, limite: null }) // []
c.ordres() // { achats: [], ventes: [] }: the remainder has vanished
c.prix // null
- The market sell finds an empty buy side: the loop does not run.
resteis 10 andlimiteisnull: the order is not stored.- The book stays empty and has no price.
A coverage tool will flag the prix === null branch as never executed. Do not delete it for all that: it becomes reachable again as soon as a resting order loses its limit, for instance by changing an object returned by ordres(). That is a test to write, not a fix to make in the engine.
Two orders from the same client. The engine never looks at auteur:
const c = new Carnet(10000)
c.passer({ id: 'v1', sens: 'vente', quantite: 10, limite: 10000, auteur: 'Awa' })
c.passer({ id: 'a1', sens: 'achat', quantite: 10, limite: 10000, auteur: 'Awa' })
// [{ achat: 'a1', vente: 'v1', prix: 10000, quantite: 10 }]
Our sources do not say whether a trade between two orders from the same client should be prevented, flagged or accepted, at the BVMAC or elsewhere. It is not a known defect of the engine: it is a question to ask the business before writing the test.
From memory: on the sell side, v1 at 10,050 (rank 1), v2 and v3 at 10,000 (ranks 2 and 3). In what order does trierCote put them, and why can the loop stop at the first refusal?
Your turn
Read next: Euronext Trading Manual, 5 juillet 2021
A question? Ask your teacher, or an AI assistant, giving it the title of this lesson.