price generators and the design of trading exchanges
DESCRIPTION
Price Generators and the Design of Trading Exchanges. by Mark Goetsch. Designing Trading Engines. The Pieces. Trading Lifecycle and Engines. Arbitrage. Quotes. Order Matching. STP. Working with Orders. Orderbook Analysis Pattern. - PowerPoint PPT PresentationTRANSCRIPT
by Mark Goetsch
The Pieces
Arbitrage
Quotes
Order Matching
STP
04/21/23
4
ud Use Case Model
OrderMatcher
Order
Enter an Order
Delete Order
Modify Order
Check Order
Validate Order
Order Matched
Clearing System
Account System
Margin System«include»
«include»
«include»
«include»
«include»
“I enter orders[1] in an order book[2] for a specific market[3]. The orders are matched at discrete time time intervals that are controlled by a heartbeat[4]. The orders can be either buy[5] or sell[6] orders[1]. The order matcher will will check whenever a new order[1] is entered into the market. The time will be marked by the heartbeat[4]. If there is a match[7] then the order matcher will either fully match the order[8] or partially match the order[9].”
Patterns Used1. Orders2. Orderbook3. Market4. Heartbeat5. Buy Order6. Sell Order7. Match8. Full Match9. Partial Match
04/21/235
Time Beat
Invalid Order
Match
Match RulesSell Orders
Buy Orders
Full MatchPartial Match
Orders
04/21/236
A customer authenticates[1] to a particular contract[2] which belongs to a market[3]. An order[5] is then presented to the orderbook[4] but not before checking the order[5] against the customers margin[6] which is different for every contract[2].
Gateway Analysis Pattern
Patterns Used1. Authenticate2. Contract3. Market4. Orderbook5. Order6. Margin
04/21/237
Authentication
Margin
Contract
Order
Market
Order Book
04/21/238
Rules that are used for every matching possibility.
04/21/239
04/21/2310
Opening Rules => How are orders entered before the opening
Closing Rules => How are orders handled at the end
Trading Rules => How are orders matched
04/21/2311
Limit OrderAgainst
Limit Order
Buy Limit in book >= incoming sell Limit
Sell Limit in book <= incoming buy Limit
Match buy quantity
Best Buy Limit
Match sell quantity
Best Sell Limit
“The incoming order is a limit order. It matches against the best limit in the book (bid price >= ask price) for the incoming limit order. The best limit in the book determines the price”
Remaining buy quantity
Remaining sell quantity
04/21/23
12
Pre-Opening State of Book Conditions Settle Exists No Settle Exists
No Entries --nothing-- Settle --nothing--
Bids Only Bid > Settle
Bid = Settle
Bid < Settle
Bid
Bid
Settle
Bid
Bid
Bid
Offers Only Offer > Settle
Offer = Settle
Offer < Settle
Settle
Offer
Offer
Offer
Offer
Offer
Bids and Offers
No Trades Possible
Settle = Offer
Settle = Offer
Bid < Settle < Offer
Bid = Settle
Bid > Settle
Offer
Offer
Settle
Bid
Bid
Bid
Bid
Bid
Bid
Bid
Bids and Offers
Trades Possible
Bid > Offer IOP
(Indicative Opening Price)
IOP
(Indicative Opening Price)
Three Stages to Opening the Market
1. Staging Period – Orders are entered and a countdown begins which calculates the Indicative Opening Price (IOP) is possible (see table below).
2. Non-Cancel Period – Can enter new orders but not cancel orders. The final IOP is calculated.
3. Opening – Orders that can be matched are matched.
04/21/2313
Order MatcherOrder
Matcher
CMI
FIX 4.2
Ilink FIX 4.2
FIX 4.2
FIX 2.3 Express
Confirm Record
CMTA/Allocation
Trade Correctio
n
Confirm RecordGUS/
AllocationTrade Correctio
n
Firm Back Office
Systems
FCM Back Office
Systems
OCC
CME Clearing
Front End Connectivity
CME GLOBEX API
CBOEdirect APICBOE Trade Match
CME GLOBEX Trade Match
Trade Processing and Clearing
04/21/2314
04/21/2315
95.00 95.05 95.10 95.15 95.20 95.25
Orders
Price Generator
Timer++
Limit Orders
Add Order Match
Matched Orders
[Array List]
[FIFO Doubly Linked List]
[Queue] [Queue]
Knowing financial theory
How to Test the Engine
Testing Engine Trading Engine
Sample Buy and Sell Transactions
Various prices above and below the book are generated according to a random distribution.
https://www.khanacademy.org/economics-finance-domain/core-finance/derivative-securities/Black-Scholes/v/introduction-to-the-black-scholes-formula
https://www.khanacademy.org/economics-finance-domain/core-finance/derivative-securities/forward-futures-contracts/v/motivation-for-the-futures-exchange
Random Walk
public static double SimulateAsset(double s0, double mu, double sigma,double tau, double delta_t,MCG g)
{//Purpose: Simulates an Asset Price run using a random walk and returns a final asset
price.// so = Price of the asset at time 0 (current time)// mu = Historical Mean// sigma = Historical Volatility (variance)// delta_t = period of time (% of a year or a day)// g = Random variable double s = s0;// Made the steps = to the number of days which is the same as daily changes.
double nSteps =tau; for (int i=0; i < (int)nSteps; i++) {// s = s0 * (1 + mean + standard deviation * gaussian random number * squareRoot of the
time period.
s= s * (1 + mu * delta_t + sigma * g.gaussian() * Math.sqrt(delta_t)); }//Returns the final Price return s;}
Simulating an Asset as a Random Walk (or drunkards walk)
public static double MeasureVolatilityFromHistoric(double[] historic, double delta_t, int length){// Purpose: Measures the Volatility for scaled prices.
double sum = 0;double variance = 0;double volatility = 0;// length - 1 instead of length since n prices generates n-1 returnsfor (int i=0; i< length -1; i++){//Random variable X^2sum = sum + Math.pow((historic[i+1]-historic[i])/historic[i],2);}// E[X^2] - E[X]^2 variance =
sum / (length -1) - Math.pow(MeasureMeanFromHistoric( historic, delta_t,length) * delta_t,2);
// Volatility = SquareRoot(variance/ dt) which is the standard deviation scaled for a time increment
volatility = Math.sqrt(variance/delta_t);return volatility;}
Measuring the Volatility Associated with the Trade
public static double MeasureMeanFromHistoric(double[] historic, double delta_t, int length)
{//Purpose: Measures the mean of the scaled prices. (Scaled indicates that the level of the// Prices is not important.
double sum = 0;double average = 0;double waverage = 0;double returns = 0;//length-1 because the scaling requires n prices to generate a sequence of n-1 scaled
returns.for (int i=0;i < (length-1); i++){
// Scales the returns and sums themreturns = (historic[i+1]-historic[i])/historic[i];sum = sum + returns;
}//computes the average of the returnsaverage = sum/(length-1);// divides the average by dt so that the average applies to each time incrementwaverage = average/delta_t;return waverage;}
Measuring the averages
Defining your engine
Patterns Used1. Quote
Instrument, bid, offer, number, spread (bid-offer), mid (bid+offer / 2), one-way quote, two-way quote.
Board Exercise
“To understand the value of a contract, we need to understand the price of the goods being traded. Goods are often priced differently depending on whether they are bought or sold. This two-way pricing behavior can be captured by a quote[1]” Martin Fowler, Analysis Patterns 1997
Patterns Used1. Scenario
Instrument, Quote, Timepoint, Price, Quote, Party, Information Source, Market Indicator
Board Exercise
“In volatile markets, prices can change rapidly. Traders need to value goods against a range of possible changes. The scenario[1] puts together a combination of conditions that can act as a single state for the market for valuation. Scenarios can be complex, and we need a way to define their construction so we can use the same scenario construction at different times in a consistent manner. Scenarios are useful for any domain with complex price changes.” Martin Fowler, Analysis Patterns 1997