DesignFest Home

OrderMatcher

An Order Matcher for an Electronic Stock Exchange (RevisedVersion)


[ Home | Problem Collection ]

Prepared by

Torsten Layda (SWX Swiss Exchange)


Problem Domain Description

The OrderMatcher Application

Specific Requirements

Use Cases

Concrete Interfaces

Bibliography

Glossary


Problem Domain Description

Abstract

Most financial markets have moved away from the traditional open out-cry mode (with people shouting at each other) towards an electronic system with participants sitting behind their computer screens. Only electronic systems are considered capable of meeting today's challenges of globalisation and rapidly increasing turn-over. This evolution is by no means limited to financial markets, but virtually applies to all commodities which can be bought and sold.

Electronic trading has given rise to a new price-fixing mechanism. In open out-cry trading the price derives from a process in which every seller seeks the highest bidding buyer and every buyer looks for the seller asking for the lowest price; this process is also referred to as two-sided auction. In an electronic exchange an order book is kept: All buy and sell orders are entered into this order book and the prices are set according to specific rules. Bids and asks are matched and trades occur.

We would like you to design a program, called OrderMatcher, that can determine in real-time the current market price and combine matching orders to trades. This is challenging, because of the complex matching rules and the high number of trading parameters involved and due to the large amount of orders entered by the market participants.

The price forming mechanism

In a market-oriented system, no matter whether cows, apples, books, CDs or billions worth of stock are involved, the price always regulates the balance between supply and demand. A higher price results in a larger supply, while demand decreases. Conversely, at a low price the demand is greater and the supply smaller. The equilibrium price lies somewhere half-way and allows the execution of the greatest possible volume of orders. At any other price the turnover would be lower either because of less supply or less demand.

In practice, this simple rule is subject to certain complications. First, specific price increments must be considered for price fixing. They determine the difference between two adjacent valid prices. They must neither be too small, to assure enough order volume at each price increment, nor too big, to avoid big price jumps. Price increments range from hundredths of the monetary unit at low prices up to multiples of the monetary unit at high prices. There are also markets where price increments lie on, e.g., multiples of thirty-seconds of the monetary unit.

Supply and demand are neither changing in a smooth manner. The fewer bid and ask prices are available, the more randomness there is, leading to discontinuity in the supply and demand curves. For this reason, additional rules are sometimes needed in order to arrive at definitive price fixing, which can easily be explained to customers.

A particular beast are market orders. For such orders, no price is specified, which indicates that a buyer is willing to pay any price, while a seller is prepared to accept any price. Obviously, before entering a market order one has to be sure that no important price changes will lead to bad surprises.

Stock exchanges

A stock exchange is a market place where buyers and sellers of financial instruments (securities) get together to conclude trades.

A stock market can be operated in basically two modes: quote driven and order driven. In a quote driven market, so-called market makers always have to mention (to "quote") a bid and an ask price for the securities they are trading. Potential buyers or sellers walk from market maker to market maker, ask for quotes and make their choice. In an order driven market, all participants feed their buy or sell orders into the price forming process.

A stock market has to be attractive to both, to investors willing to spent money and to issuers introducing securities to trade. Therefore, the market has to be

liquid
A market is liquid when there is enough supply and demand to allow prices to be determined consistently. Liquidity is basically a function of volume of trades.
transparent
The most important way to ensure the fairness of a market is transparency. The price forming process has to be tracable by all participants and the bid and ask prices as well as the trades have to be uniformly and instantly published.
cost-efficient
Transaction costs include all the commissions and operating costs which have to be paid by a customer involved in a deal. These costs increase with the number of parties involved (every broker, e.g., charges a significant fee), with the inefficiency of the procedures (fax takes more expensive time than e-mail) and with the costs of the involved systems.

To reach these objectives, more and more markets are moving away from the open out-cry system, where traders physically meet around rings or in pits, to an electronic, networked system. The access to this market from remote sites is not an issue anymore, not only due to the advent of the internet. This means that participants can be anywhere in the world, which in turn leads to increased liquidity and more consistent prices due to the centralised market place. Also, the complete order and trade routing cycle from the customer via the broker to the exchange and back via the clearing house (where the trade is executed) and the broker to the customer can become paper-less, or, at least, less paper intensive.

The order book

In an electronic exchange, the situation of supply and demand is represented in the form of an order book. This is the compilation of all buy and sell orders for a specific security at a certain point in time. Consider the following example for the share of OO Tech, Inc.

Buy orders

Bid

Price

Ask

Sell orders

Individual orders

Cumulative per price

Cumulative least favorable price

Cumulative least favorable price

Cumulative per price

Individual orders

400 + 300

700

700

Market

200

200

900

101.0

300

300

1200

100.5

400

400

1600

100.0

4200

500

200+300

500

500

2100

99.5

3700

700

700

800 + 100

900

3000

99.0

3000

500

500

1000

1000

4000

98.5

2500

300

100 + 200

700 + 200

900

4900

98.0

2200

300

300

97.5

1900

300

100 + 200

97.0

1600

100

100

Market

1500

1500

700 + 800

In the center column, all possible prices are shown in decreasing sequence from top to bottom. On the left side the buy orders (demand) are shown, and on the right side the sell orders (supply). The outmost columns show the individual orders. The columns next to them show the cumulative total of order volumes per price. The two columns directly beside the price column are decisive. Here all orders are accumulated from the least favorable prices onwards, starting with market orders. On the buy side, market orders and then the orders with the highest purchase limits down to the lowest are added continuously. On the sell side, accumulation starts with market orders and then goes from the lowest sell price up to the highest.

Maximum execution occurs at a price of 99, where 3000 units change hands.

The order book states

Generally speaking, securities can be traded either

Correspondingly, the order book is said to be in various states. The two most important order book states are

Trading
Whenever there is a change to the order book, the matching rules are applied. Trades occur whenever an incoming order can be matched with an order already in the order book.
Break
Orders can be inserted or removed from the order book. The matching rules are applied, but only trade candidates are identified. No trades are actually generated. Only the price at which they would occur is published. It is called the theoretical opening price.

Other less important order book states include

Stop Trading
A trade would have caused a stop trading (see below), and trading is interrupted for a fixed amount of time. Thereafter, trading is resumed independently of the price.
New Security
The security has just been listed but is not traded, yet. Not even orders can be entered for this security.
Accepting Orders
Orders can be inserted, but no orders will be matched and no theoretical opening price will be calculated, yet.
Suspended Security
The security has been suspended from matching, because, e.g., an important news with significant influence on the price is expected.
Security Delisted
The security is not traded anymore and no more orders can be entered.

Not all states are valid successors for a given state. E.g., it is not possible to put a new security immediately into permanent trading. There must be an Accepting Orders state followed by a break before.

Note that an auction is not a state of its own, but happens instantly. All trades resulting from the auction bear the same time stamp.

Typical 24 hours order book state cycles are (each 'A' stands for an auction)

Break --- -A- Trading --- Break(60'@12:00am) --- -A- Trading --- Break

(i.e. permanent trading during the day with a break for lunch)

or

Break --- -A- Trading --- Break (10'@4:50pm) --- -A- --- Break

(i.e. permanent trading with a closing auction)

or

Break -A- Break -A- Break -A- Break -A- Break -A- Break -A- Break -A- Break

(i.e. seven auctions a day).

Matching rules

As on any market place, also the trading on an electronic exchange needs a set of rules which govern the matching of orders to trades. These matching rules are representing the market model and aim at the generation of trades as the participants expect them to occur.

There is one set of matching rules for every order book state in which matching can occur. Usually they are quite complicated (cf. Ref. 1). The precise rules vary from exchange to exchange. In this sense, the following is to be understood as an example only.

During a break, the opening price is most often determined on the basis of the orders in the order book, according to the principle of maximum execution. All trades occur at the same price, the opening price. In the example above, this would be 99. After the opening, the only orders remaining in the order book are those not executable due to a lack of matching orders.

It may well happen that no opening price can be fixed because there are no orders in the order book which could be matched according to the rules. In such a case, trading is either opened without an opening price or not opened at all.

In permanent trading, matching is governed by different rules than the determination of the opening price. Here an attempt is made to execute each new incoming order and hence trigger a trade (or several trades). If no matching is possible, or if only part of the order has been matched, the remainder is entered in the order book.

Most commonly, the orders are matched in price/time priority: The order with the better price has a higher priority than an order with a worse price. Here "better" has to be read as "higher" for a buy order and "lower" for a sell order: A buyer who is prepared to pay more money than anybody else gets the deal; in the same way a sell order which is asking for the least money gets highest priority. Remember that market orders are always considered to have the best price. For orders at the same price, the order which was entered first is executed first.

Stop trading

A common way to avoid big price fluctuations is to impose a limit on the difference between two subsequently paid prices. When this so called stop trading range would be exceeded, trading is stopped for a few minutes to give everybody a chance to settle down.

Typical stop trading ranges go from 10% or 5% of the last paid price for illiquid securities down to less than a percent of the last paid price for securities with high liquidity and of particular importance. The latter include the securities from which the market index is calculated.

If the order book is in a break or in permanent trading, each insertion or deletion of an order in the book triggers the order matcher to apply the matching rules.

No trade is generated if the difference between a resulting trade price and the last paid price exceeds the stop trading range. If no stop trading arises, the trades are either generated and published (permanent trading) or the theoretical opening price is updated (break). If a stop trading occurs, the order book is either put into stop trading (permanent trading) or the opening is delayed (break).

Market segments

Each security traded on an electronic exchange belongs to a market segment, e.g. to the domestic shares or the foreign issuer bonds. Trading hours and trading mode (permanent trading or auction) are typically set on a per segment basis.

Outlook

This description has been restricted to the most important key features of an electronic exchange, but it should be sufficient to grasp all basic principles.

More complicating features of an electronic exchange such as

have been omitted.


The OrderMatcher Application

Develop a design for a program "OrderMatcher", which implements an order matcher for an electronic, order driven stock exchange.

OrderMatcher has to keep an order book for each security. The OrderMatcher has to support various states and has to switch from one state to the next based on a schedule established for the segment the security belongs to.

We want you to focus on the actual matching of orders. The matching is based on the matching rules applicable to the current order book state and on the trading parameters for the security. Keep in mind that trades must only be generated if the order book is in the trading state.

As for the matching rules, a first version should observe price/time priority during permanent trading, and in a break, in addition, the principle of maximum execution (cf. the use cases).

The OrderMatcher has to support stop trading, by first determining all trade candidates for one cycle of the application of the matching rules and then verifying that none of the trade prices is further away from the last paid price than the stop trading range. Only then the actual trades are to be generated.

Also design an infrastructure based on which the order book states and trading parameters for a given security and a given segment can be readily adjusted to market requirements by the exchange personnel.

You can safely ignore the mechanisms of how orders reach the order book and of how the trading parameters are actually set-up.


Specific Requirements

The following requirements apply to the order matcher application:

Structure summary

Some of these aspects can be visualised by a class diagram, produced during a previous DesignFest®.


Use Cases

The following use cases give examples of typical scenarios as they occur in the operation of an electronic stock exchange. They illustrate some of the above requirements.

For the matching rule use cases, incoming orders are underlined, while the orders which actually match are in italics. The resulting trade price or the new theoretical opening price is in bold-face. All orders at a given price are accumulated.

Matching Rules

Use case #1 : Matching Rule: Incoming order against another order

A buy order with a price better than or equal to the incoming order price (from the point of view of the incoming order) is already in the order book.

Last Paid Price = 44

Bid

Ask

200

40

39

100

100

38

41

100

100

37

43

100

Use case #2 : Matching Rule: Incoming order against market order

No order with a price better than the incoming order price (from the point of view of the incoming order) is already in the order book.

Last Paid Price = 44

Bid

Ask

200

Market

46

200

200

41

200

40

Use case #3 : Matching Rule: Incoming order against market order

A sell order with a price better than or equal to the incoming order price (from the point of view of the incoming order) is already in the order book.

Last Paid Price = 44

Bid

Ask

200

43

Market

200

42

200

43

200

Note that the order determining the trade price is actually not involved in the trade.

Use case #4 : Matching Rule: Incoming market order against market order

No order with a price better than the reference price (from the point of view of the incoming order) is already in the order book.

Last Paid Price = 44

Bid

Ask

100

Market

Market

100

Use case #5 : Matching Rule (Break): Incoming order against another order

No order with a price better than the arithmetic mean of the last two orders matched is in the order book.

Last Paid Price = 44
Arithmetic Mean = 39.5, rounded to 40 (price increment = 1)

Bid

Ask

100

40

39

100

100

39

Use case #6 : Matching Rule (Break): Incoming order against another order

Buy order with a price better than the arithmetic mean of the last two orders matched is in the order book.

Last Paid Price = 44
Arithmetic Mean = 39.5 (price increment = 0.25)

Bid

Ask

100

40

39

100

100

39.75

Note that the order determining the opening price would actually not be executed in the auction.

Miscellaneous

Use case #7 : Change of the matching rules

A new stock exchange act has become effective. The matching rules have to be adapted such that for the same price, customer orders have priority over orders entered on behalf of the broker, irrespective of their time of entry.

The matching rules are adapted by the development department, tested with the members and put into operation on the pre-defined date.

Use case #8 : Introduction of new securities

The trading and listing department introduces ("lists") 7 new securities and assigns them to the relevant market segments:

The securities become tradable in two weeks and will accept orders in a week from now.

Use case #9 : Change of a stop trading range

The shares from which the market index is deduced show too much volatility: The oscillations of the paid prices do not reflect the market behaviour, but are mostly due to input errors of market participants due to a lack of experience. Last week, e.g., somebody sold 5000 pieces at 100 instead of 100 pieces at 5000.

The stop trading range for these shares is reduced by the trading department from 2% to 0.5%.

Use case #10 : Change of price increments

Program trading has drastically increased the order flow. Participants frequently adapt all their order prices in the market when the market situation changes. The trading department decides to double the price increments, so that only larger fluctuations justify an adaptation of order prices.

The change is executed at the end of a pre-defined bank working day. All orders in the concerned order books are deleted to get rid of orders sitting on the now invalid intermediate ticks.

Use case #11 : Changing of order book state cycles

In a couple of low volume bonds the last paid prices become out-dated and do no longer reflect the current market situation. Also, the bonds become quite volatile. The trading department switches these market segments from permanent trading to auction mode.

Use case #12 : Re-partitioning of the order matcher instances

Trading volume in the B Chemicals security has risen and is expected to continue to do so at a high pace due to various rumours in the market. The order matcher for B Chemicals is moved to a dedicated hardware unit.


Concrete Interfaces

The OrderMatcher is interfaced to the systems of the market participants, from which orders are received and to which the resulting trades are broadcast. Also, there is an interface to the clearing house, to which all trades are forwarded for clearing and settlement. The exact mechanisms are beyond the scope of this problem. It is sufficient to assume that there is an infrastructure for market participants to insert and delete orders and for trades to flow out.

The same holds for the maintenance of the trading parameters by the exchange personnel.

The OrderMatcher would be the core of an Electronic Commercial Network (ECN). Other components are discussed in DesignFest, too: The thin client (client/server) application, e.g., would be used by the participants to enter orders and receive trade information.


Bibliography

All these books, and many more, can be ordered at the bookstore of the SWX Swiss Exchange.


Glossary

Auction
Transition of the order book from a break to permanent trading. As many orders as possible are executed at a fixed price, the opening price.
Ask
The price of a sell order, i.e. the amount of money the seller is requesting to receive.
Bid
The price of a buy order, i.e. the amount of money the buyer is willing to pay.
Clearing
Execution of a trade by physically delivering the traded securities against the payment of the agreed price. Most frequently, trades are cleared 3 bank business days after the trade has been concluded.
Clearing house
Organisational body responsible for the clearing of trades.
Delisting
Opposite of listing a security. The security can no longer be traded. This happens, e.g., for bonds after they have been refunded by the issuing company.
Derivative
Security which is based on another security, the underlying. Derivatives are often standard contracts (options) which entitle to buy or to sell a given number of underlyings at a pre-defined price.
Index
Weighted average of the prices of a set of selected securities. The prices are weighted with the number of shares available for each company. Examples are the Dow Jones Industrial Index or the Swiss Market Index.
Last Paid Price
The price paid in the most recent trade for this security.
Listing
The introduction of a security into the set of financial instruments tradable on a given stock exchange. Only listed securities can be traded.
Market order
An order for which no limit price is specified, indicating that the buyer/seller is willing to pay/accept any price.
Matching
The combination of orders to trades based on a set of matching rules. E.g., the orders with the best price are executed before those with a worse price; within a given price, the order which was entered first is executed first.
Matching rules
The set of rules on which the matching of orders is based.
Open out-cry
Stock exchange operation mode where the participants are physically present around rings or in pits and prices are formed through direct inter-human communication. Examples include the New York Stock Exchange.
Opening
Auction after which the order book is put in the (permanent) trading state.
Opening price
The theoretical opening price applicable at the moment of an auction. All trades resulting from the auction are executed at the opening price.
Order book state
The mode the order book is currently operated on. Most important are break (where prices are determined but no trades occur) and permanent trading.
Order book state cycle
Chain of subsequent order book states a particular security goes through.
Order driven market
The market price is determined based on the orders of the market participants, which are compiled in the order book. Whenever the order book changes, the market price is re-evaluated.
Price increments
Difference between two authorised prices. Usually the price increment is a function of the price itself: At higher prices, the increments are higher as well.
Quote
The simultaneous committment to a bid and an ask price.
Quote driven market
The market price is set by nominated market makers, who have to provide bid and ask price pairs (quotes) to anybody interested in trading a given security.
Security
Class of financial instruments with well-defined characteristics. Such characteristics include nominal value, rights and obligations of the bearer, e.g. to receive a dividend, and rights and obligations of the issuer, e.g. to pay a periodic interest.
Share
Security which grants ownership to a fraction of a company. Also entitles to receive regular dividend payments, if the company pays dividend at all, and possibly to vote at general assemblies of the company.
Spread
Difference between the best bid and the best ask price for a given security.
Stock Exchange
Financial market place where a defined set of financial instruments (securities) is traded.
Side
The combined set of all buy orders or all sell orders in the order book of a given security.
Stop trading
Interruption of permanent trading because the next trade would occur at a price which is too far away from the last paid price. After a pre-defined time interval has elapsed, trading is resumed regardless of the trade price.
Stop trading duration
Time interval after which trading is resumed after a stop trading, regardless of the trade price.
Stop trading range
The number of price increments, or the percentage, or the absolute price difference, which must not be exceeded by two subsequent trade prices. Otherwise, a stop trading occurs.
Tick
Synonym for price increment.
Theoretical opening price
Price at which trades would be concluded during an auction if the auction would take place now.
Trading parameters
The set of all attributes defining the trading characteristics of a given security. These include the trading hours, the order book state cycle, the stop trading range and duration, as well as the price increments.

Last updated by Torsten Layda, SWX Swiss Exchange, DesignFest® Webmaster.