Part 10 · FinTech engineering
Capital markets systemsأنظمة أسواق المال
How a stock exchange matches buyers with sellers. Skip this entirely unless you work in trading.
5 minute read
Capital markets systems are what stands between you pressing buy and the share appearing in your portfolio. There are three layers: an exchange matching orders, a clearing house standing between the two sides, and a depository recording who owns what.
A request to buy a share becomes a row in a sorted list before it becomes a trade in your portfolio.
Why didn't my order fill at the price on the screen?
This is what most surprises a first-time share buyer. The screen says 50 riyals, they press buy, and it executes at 50.30.
Nobody deceived them. The price they saw was not a price. It was the last trade that happened, and that is in the past.
A market is not a shop with a price label. It is two lists: people willing to sell at the price they will accept, and people willing to buy at the price they will pay. That is called the order book.
When you buy at market, you are not saying "buy at fifty". You are saying "buy now at the best available". The system takes the cheapest sell offer there is, and if its size is not enough it takes the next one, which is dearer. So your price moves with the size of your order.
The gap between what you expected and what was actually paid is called slippage. It widens in thinly traded shares, it widens around news, and it widens with large orders.
If you want a guaranteed price, you use a limit order with a cap. Then you guarantee the price and not the execution, because there may be no seller willing to meet your cap. That is the unavoidable trade-off: you choose between guaranteeing price and guaranteeing execution, and you do not get both.
An order is conditional intent
An order states what an investor wants to buy or sell, the quantity, and acceptable price. Pressing buy does not mean the exchange immediately sold anything. There may be no seller at that price, or only part of the quantity may execute.
A market order takes the best available prices now. A limit order sets the highest buy or lowest sell price. An order for 100 shares with a SAR 50 limit will never pay more than 50, but may receive nothing.
The broker checks account, funds, instrument, and permission before routing the order. Status can be accepted, partial, filled, cancelled, or rejected. The app must not promise execution that did not occur.
The order book (دفتر الأوامر) has two sides
An order book contains sorted buy and sell lists. The highest buyer and lowest seller stand at the top. The gap between them is the spread (المدى السعري).
If the best buyer offers SAR 99 and the best seller asks 100, no trade occurs. A new buyer willing to pay 100 crosses the spread and can match the seller.
The visible book may not contain every market intention. Hidden orders, other venues, and market makers can exist. It remains the simplest model for the matching engine's input.
Price, then time
The matching engine applies a published fairness rule. Best price wins first. At the same price, the earliest arrival wins. This is price-time priority.
Two investors may bid SAR 50, with the first executed earlier. A third bidding 51 moves ahead because the price is better.
The market needs accurate clocks and an undisputed sequence. A fraction of a second can determine arrival order. Speed is therefore a genuine trading advantage in a way it is not for a savings app.
Execution can be partial
An investor may request 1,000 shares while only 300 are available at the limit. The engine fills 300 and leaves or cancels 700 according to the order condition.
A large market order may consume several price levels. Its average price can be worse than the first quote. This slippage grows when liquidity is low or quantity is large.
Broker records must separate order from execution. One order has an identifier; each fill has its own identifier, price, time, and quantity. Fees and settlement follow.
FIX is a session with memory
FIX is the long-standing institutional protocol for orders and executions. Unlike an independent HTTP request carrying JSON, it maintains an open stateful session.
Each side tracks the next expected sequence number. Receiving 102 after 100 reveals that 101 is missing and triggers recovery. After disconnection, the parties agree where to resume.
Delivery does not mean execution. A counterparty may acknowledge receipt, reject the order, or later send a partial fill. Integrations need a state model connecting client, broker, and venue identifiers.
Before and after the trade
Before routing, the broker checks funds, customer limits, instrument, and market hours. It can prevent an unsupported short sale or oversized order. These pre-trade controls stop errors before the venue.
After execution come clearing and settlement. A clearing party calculates obligations, then securities and cash move on the market's schedule. An app can display a share before every legal and cash transfer has completed.
The broker reconciles venue fills with orders and customer accounts. An unknown execution becomes an exception, not an entry assigned at random.
Cancellation is a race
Cancel does not erase history. The request may travel while the original order executes. If execution wins the race, the broker cannot reverse the trade because the screen said cancellation was pending.
The product should show “cancel requested,” followed by “cancelled” or “filled.” Only the venue can confirm the result.
Failure needs a safe gateway
If a broker loses market connectivity with live orders, it must not resend all of them under new identifiers. It restores the FIX session, recovers missing messages, and reconciles every order.
Venues and brokers use kill switches plus price, quantity, and velocity limits. If one client system sends thousands of bad orders, a gateway stops it before the engine. Speed never removes the need for risk controls.
For a beginner, the chain is enough: the customer creates an order, the broker checks it, the book sorts it, the engine matches by price then time, and clearing and settlement follow. Protocols, speed, and audit controls protect that chain and its fairness.
New words here
- Order bookدفتر الأوامر
- The two sorted lists of buy and sell offers waiting to be matched.
- Spreadالمدى السعري
- The gap between the best price anyone will buy at and the best price anyone will sell at.
The short version
- Best price first; ties broken by who got there first. That is the whole fairness rule.
- Speed is a genuine competitive feature here in a way it is nowhere else in FinTech.
- FIX keeps a stateful session open, unlike the stateless web APIs used elsewhere, and that shapes the entire integration.
Check your understanding
Answer before reading the explanation. Getting one wrong here is worth more to you than a lucky guess.
Official sources
For checking the rules stated above yourself. The article is complete without them.
This is a learning guide built from field experience. It is not legal, regulatory or investment advice, and it does not replace qualified professional counsel. FinTech rules change quickly and vary by country, so check the primary source before you make a decision that matters.