-
Notifications
You must be signed in to change notification settings - Fork 15
/
matching.q
396 lines (336 loc) · 15.3 KB
/
matching.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/ Matching Engine for HKEx
/ Last Modified: Jan 20, 2015
/ Created by: Raymond Sak, Damian Dutkiewicz
/ 1. Create dummy trades for testing
s:(),`FDP,`HSBC,`GOOG,`APPL,`REYA;
px:(),5,80,780,120,45;
st:09:00:00.000;
ot:`limit; /`auction
/ CreateData: Create random trade orders
CreateData:{[n]
dict:s!px;orderID:n?1000000000;sym:n?s;side:n?`bid`offer;
orderType:ot;price:((+;-)side=`bid) .'flip(dict sym;.050*n?1+til 10);
sample:flip`orderID`time`sym`side`orderType`price`quantity! (orderID;st+n?25200000;sym;side;orderType;price;100*n?1+til 10)
}; /orderType:n?ot
/ 2. Create bid, ask, trade, rejected book
book:([]orderID:`int$();time:`time$();sym:`$();side:`$();orderType:`$();price:`float$();quantity:`int$());
bidbook:`sym xasc `price xdesc `time xasc `orderID xkey book;
askbook:`sym`price`time xasc `orderID xkey book;
tradebook:`tradeID xkey ([]tradeID:`int$();bidOrderID:`int$();askOrderID:`int$();time:`time$();sym:`$();
bidOrderType:`$();askOrderType:`$();tradedPrice:`float$();bidOrderPrice:`float$();askOrderPrice:`float$();quantity:`int$());
rejectedbook:([]orderID:`int$();time:`time$());
/ 3. Handle incoming message
testMessage: "8=FIX.4.4|9=178|37=00001|52=09:00:00.000|55=APPL|54=1|44=239.5|14=100";
/fixTagToName:`1`6`8`9`10`11`12`13`14`15`17`19`21`29`30`31`32`34`35`37`38`39`49`52`54`56`151!`Account`AvgPx`BeginString`BodyLength`CheckSum`ClOrdID`Commission`CommType`CumQty`Currency`ExecID`ExecRefID`HandlInst`LastCapacity`LastMkt`LastPx`LastQty`MsgSeqNum`MsgType`OrderID`OrderQty`OrderStatus`SenderCompID`SendingTime`Side`TargetCompID`LeavesQty;
/fixTbl:(uj/){flip fixTagToName[key d]!value enlist each d:GetAllTags x} testMessage;
GetAllTags:{[msg](!)."S=|"0:msg};
GetTag:{[tag;msg](GetAllTags[msg])[tag]};
ProcessMessage:{[message]
oID: "I"$ GetTag[`37;message];
oTime: "T"$ GetTag[`52;message];
oSym: `$ GetTag[`55;message];
oSide: $[(`$ GetTag[`54;message])=`1;`bid;(`$ GetTag[`54;message])=`2;`offer;`$ GetTag[`54;message]]; // `1 = buy, 2 = sell
// type: oType
oPrice: "F"$ GetTag[`44;message]; // not sure if it should be 44 (Price) or 6 (av. price)
oQuantity: "I"$ GetTag[`14;message];
convertedOrder:`orderID`time`sym`side`orderType`price`quantity!(oID;oTime;oSym;oSide;`limit;oPrice;oQuantity);
MatchOrder[convertedOrder];
};
/ 4. Validating the input format
/ check the time, if it is the latest, etc.
/ minimum table spread https://www.hkex.com.hk/eng/rulesreg/traderules/sehk/Documents/sch-2_eng.pdf
/ call getminimumspread
/ 5. Create matching function (using the sample data now)
/ MatchOrder: Top level function to match market order with different types
/ assume the order is in the format of a dictionary
MatchOrder: {[order]
$[order[`side]=`bid;
BidOrderCheckCondition[order];
order[`side]=`offer;
AskOrderCheckCondition[order];
`WrongOrderSide]
};
/ BidOrderCheckCondition: Incoming order is the bid order, check which condition it applies
BidOrderCheckCondition: {[order]
orderPrice: order[`price];
askbookTopPrice: askbook[GetTopOfBookOrderID[order[`sym];`offer];`price];
askbookTopPrice10spread: askbookTopPrice+9*GetMinimumSpread[order[`price]];
nominalPriceDeviate9times: GetNominalPrice[order[`sym]]*9;
$[orderPrice < askbookTopPrice;
/ condition 1:
ProcessBidCondition1[order];
orderPrice = askbookTopPrice;
/ condition 2:
ProcessBidCondition2[order];
((orderPrice within(askbookTopPrice,askbookTopPrice10spread)) and (not orderPrice = askbookTopPrice));
/ condition 3:
ProcessBidCondition3[order];
((orderPrice within (askbookTopPrice10spread, nominalPriceDeviate9times)) and (not orderPrice = askbookTopPrice10spread));
/ condition 4:
ProcessBidCondition4[order];
orderPrice > nominalPriceDeviate9times;
/ condition 5:
ProcessBidCondition5[order];
]
};
/ AskOrderCheckCondition: Incoming order is the ask order, check which condition it applies
AskOrderCheckCondition: {[order]
orderPrice: order[`price];
bidbookTopPrice: bidbook[GetTopOfBookOrderID[order[`sym];`bid];`price];
bidbookTopPrice10spread: bidbookTopPrice-9*GetMinimumSpread[order[`price]];
nominalPriceDeviate9times: GetNominalPrice[order[`sym]]%9;
$[orderPrice > bidbookTopPrice;
/ condition 1:
ProcessAskCondition1[order];
orderPrice = bidbookTopPrice;
/ condition 2:
ProcessAskCondition2[order];
((orderPrice within(bidbookTopPrice10spread,bidbookTopPrice)) and (not orderPrice = bidbookTopPrice));
/ condition 3:
ProcessAskCondition3[order];
((orderPrice within (nominalPriceDeviate9times, bidbookTopPrice10spread)) and (not orderPrice = bidbookTopPrice10spread));
/ condition 4:
ProcessAskCondition4[order];
orderPrice < nominalPriceDeviate9times;
/ condition 5:
ProcessAskCondition5[order];
]
};
/ ============================= BID ORDER CONDITIONS =========================== /
ProcessBidCondition1:{[order] / Bid Order Below Top Ask Price
$[order[`orderType]=`speciallimit;
AddToRejectBook[order];
AddToBidBook[order]]; / If limit order OR Enhanced Limit Order
};
ProcessBidCondition2:{[order] / Bid Order = Top Ask Price
$[order[`orderType]=`speciallimit;
AddToRejectBook[MatchBidOrderAtTopAskPrice[order]];
AddToBidBook[MatchBidOrderAtTopAskPrice[order]]]; / if limit order OR enhanced limit order
};
ProcessBidCondition3:{[order] / Bid Order > Top Ask Price AND Bid Order < Price @ 9 Spreads Away
$[order[`orderType]=`limit;
AddToRejectBook[order];
order[`orderType]=`enhancedlimit;
AddToBidBook[MatchBidOrderUpTo9Spreads[order]];
AddToRejectBook[MatchBidOrderUpTo9Spreads[order]]]; / if special limit order
};
ProcessBidCondition4:{[order] / Bid Order > Prie @ 9 Spreads Away AND Ask Order < Price @ 9 deviations Away
$[order[`orderType]=`speciallimit;
AddToRejectBook[MatchBidOrderUpTo9Spreads[order]];
AddToRejectBook[order]]; / if limit order OR enhanced limit order
};
ProcessBidCondition5:{[order] / Bid Order > Price @ 9 deviations Away
AddToRejectBook[order];
};
/ ============================= ASK ORDER CONDITIONS =========================== /
ProcessAskCondition1:{[order] / Ask Order Above Top Bid Price
$[order[`orderType]=`speciallimit;
AddToRejectBook[order];
AddToAskBook[order]]; / If limit order OR Enhanced Limit Order
};
ProcessAskCondition2:{[order] / Ask Order = Top Bid Price
$[order[`orderType]=`speciallimit;
AddToRejectBook[MatchAskOrderAtTopBidPrice[order]];
AddToAskBook[MatchAskOrderAtTopBidPrice[order]]]; / if limit order OR enhanced limit order
};
ProcessAskCondition3:{[order] / Ask Order < Top Bid Price AND Ask Order > Price @ 9 Spreads away
$[order[`orderType]=`limit;
AddToRejectBook[order];
order[`orderType]=`enhancedlimit;
AddToAskBook[MatchAskOrderUpTo9Spreads[order]];
AddToRejectBook[MatchAskOrderUpTo9Spreads[order]]]; / if special limit order
};
ProcessAskCondition4:{[order] / Ask Order < Price @ 9 Spreads Away AND Ask Order > Price @ 9 deviations away
$[order[`orderType]=`speciallimit;
AddToRejectBook[MatchAskOrderUpTo9Spreads[order]];
AddToRejectBook[order]]; / if limit order OR enhanced limit order
};
ProcessAskCondition5:{[order] / Ask Order < Price @ 9 deviations Away
AddToRejectBook[order]; / Reject Order regardless of order type
};
/ ============================= Matching Functions =========================== /
/ Return: the unfilled order or an order with quantity=0 if fully executed
MatchBidOrderAtTopAskPrice: {[order]
topAskOrderID: GetTopOfBookOrderID[order[`sym];`offer];
topAskOrder: GetTopOfBookOrder[order[`sym];`offer]; / fetches the whole order dictionary
$[(order[`quantity]=0) | (order[`price]<>topAskOrder[`price]); / if Q = 0 or no more matching orders, return
:order;
[
tradeQuantity: min[order[`quantity],topAskOrder[`quantity]];
AddToTradeBook[topAskOrder;order;tradeQuantity;topAskOrder[`price]];
$[topAskOrder[`quantity]=tradeQuantity; // ask order quantity < OR = ask order quantity
delete from `askbook where orderID=topAskOrderID; // If true, delete from bidBook
askbook[topAskOrderID;`quantity]: topAskOrder[`quantity] - tradeQuantity]; // If false, update quantity
order[`quantity]: order[`quantity] - tradeQuantity;
:MatchBidOrderAtTopAskPrice[order];
]
];
};
MatchAskOrderAtTopBidPrice: {[order]
topBidOrderID: GetTopOfBookOrderID[order[`sym];`bid];
topBidOrder: GetTopOfBookOrder[order[`sym];`bid]; / fetches the whole order dictionary
$[(order[`quantity]=0) | (order[`price]<>topBidOrder[`price]); / if Q = 0 or no more matching orders, return
:order;
[
tradeQuantity: min[order[`quantity],topBidOrder[`quantity]];
AddToTradeBook[order;topBidOrder;tradeQuantity;topBidOrder[`price]];
$[topBidOrder[`quantity]=tradeQuantity; // bid order quantity < OR = ask order quantity
delete from `bidbook where orderID=topBidOrderID; // If true, delete from bidBook
bidbook[topBidOrderID;`quantity]: topBidOrder[`quantity] - tradeQuantity]; // If false, update quantity
order[`quantity]: order[`quantity] - tradeQuantity;
:MatchAskOrderAtTopBidPrice[order];
]
];
};
MatchBidOrderUpTo9Spreads: {[order]
topAskOrderID: GetTopOfBookOrderID[order[`sym];`offer];
topAskOrder: GetTopOfBookOrder[order[`sym];`offer]; / fetches the whole order dictionary
$[(order[`quantity]=0) | (order[`price]<topAskOrder[`price]); / if Q = 0 or no more matching orders, return
:order;
[
tradeQuantity: min[order[`quantity],topAskOrder[`quantity]];
AddToTradeBook[topAskOrder;order;tradeQuantity;topAskOrder[`price]];
$[topAskOrder[`quantity]=tradeQuantity; // bid order quantity < OR = ask order quantity
delete from `askbook where orderID=topAskOrderID; // If true, delete from bidBook
askbook[topAskOrderID;`quantity]: topAskOrder[`quantity] - tradeQuantity]; // If false, update quantity
order[`quantity]: order[`quantity] - tradeQuantity;
:MatchBidOrderUpTo9Spreads[order];
]
];
};
MatchAskOrderUpTo9Spreads: {[order]
topBidOrderID: GetTopOfBookOrderID[order[`sym];`bid];
topBidOrder: GetTopOfBookOrder[order[`sym];`bid]; / fetches the whole order dictionary
$[(order[`quantity]=0) | (order[`price]>topBidOrder[`price]); / if Q = 0 or no more matching orders, return
:order;
[
tradeQuantity: min[order[`quantity],topBidOrder[`quantity]];
AddToTradeBook[order;topBidOrder;tradeQuantity;topBidOrder[`price]];
$[topBidOrder[`quantity]=tradeQuantity; // bid order quantity < OR = ask order quantity
delete from `bidbook where orderID=topBidOrderID; // If true, delete from bidBook
bidbook[topBidOrderID;`quantity]: topBidOrder[`quantity] - tradeQuantity]; // If false, update quantity
order[`quantity]: order[`quantity] - tradeQuantity;
:MatchAskOrderUpTo9Spreads[order];
]
];
};
/ ============================== Books Operations ============================ /
AddToAskBook: {[order]
breakhere;
if[order[`quantity]<>0;
[
order[`orderType]:`limit;
`askbook insert order;
`sym`price`time xasc `askbook; /sort the table
]];
};
AddToBidBook: {[order]
if[order[`quantity]<>0;
[
order[`orderType]:`limit;
`bidbook insert order;
`sym xasc `price xdesc `time xasc `bidbook; / sort the table
]];
};
AddToTradeBook: {[askOrder; bidOrder; quantity; tradedPrice]
tradeTime:.z.T;
tradeID: 1+count tradebook;
`tradebook insert (tradeID;bidOrder[`orderID];askOrder[`orderID];tradeTime;
askOrder[`sym];bidOrder[`orderType];askOrder[`orderType];tradedPrice;bidOrder[`price];askOrder[`price];quantity);
`sym xasc `time xdesc `tradebook;
};
AddToRejectBook: {[order]
if[order[`quantity]<>0;`rejectedbook insert (order[`orderID]; .z.T)];
};
GetTopOfBookOrder: {[symbol;side]
$[side=`bid;
[
entry: select [1] from bidbook where sym=symbol;
output: (first key entry)+(first entry);
];
side=`offer;
[
entry: select [1] from askbook where sym=symbol;
output: (first key entry)+(first entry);
];
output: -1];
output
};
/ GetTopOfBookOrderID: Return the order id of the top of either the bid/ask book
GetTopOfBookOrderID: {[symbol;side]
$[side=`bid;
output: exec orderID[0] from bidbook where sym=symbol;
side=`offer;
output: exec orderID[0] from askbook where sym=symbol;
output: -1];
output
};
/ GetTopOfBookPrice: Return the top price of either the bid/ask book
GetTopOfBookPrice: {[symbol;side]
$[side=`bid;
output: exec price[0] from bidbook where sym=symbol;
side=`offer;
output: exec price[0] from askbook where sym=symbol;
output: -1];
output
};
/ ======================== Market-related Calculations ====================== /
/ GetNominalPrice: Return the nominal price at the time the function is called
/ Input parameter: symbol: the security symbol
/ Decision Tree:
/ pre-opening session -> IEP can be determined -> IEP (Note: Not implementing now)
/ -> IEP cannot be determined -> previous close
/ continuous session -> same day trade takes place -> currentbid > lastprice -> currentbid
/ -> currentask < last price -> currentask
/ -> else -> lastprice
/ -> not traded yet on the same day -> currentbid > previousclose -> currentbid
/ -> currentask < previousclose -> currentask
/ -> else -> previousclose
GetNominalPrice: {[symbol]
/ assume previous close equal to last recorded price,
/ might change later if the system has difficulty in handling too much order near the close
lastPrice: exec tradedPrice[0] from tradebook where sym=symbol;
prevClose: lastPrice;
currentBid: GetTopOfBookPrice[symbol;`bid];
currentAsk: GetTopOfBookPrice[symbol;`offer];
/ TODO: case for pre-opening session or continuous session
/ Assume it's always continuous session now
/ TODO: distinguish between if the last trade takes place in the same day or not
/ Last trade in the same day: take the last recorded price
/ Last trade in previous day: take the previous close
/ Assume it has the last trade in the same day now
$[currentBid>lastPrice;
output:currentBid;
currentAsk<lastPrice;
output:currentAsk;
output:lastPrice
];
output
};
/ Check if we need to change the .00001 design later
GetMinimumSpread: {[price]
$[price within(0.01,0.25);
output:0.001;
price within(0.2500001,0.50);
output:0.005;
price within(0.5000001,10.00);
output:0.010;
price within(10.0000001,20.00);
output:0.020;
price within(20.0000001,100.00);
output:0.050;
price within(100.0000001,200.00);
output:0.100;
price within(200.0000001,500.00);
output:0.200;
price within(500.0000001,1000.00);
output:0.500;
price within(1000.0000001,2000.00);
output:1.000;
price within(2000.0000001,5000.00);
output:2.000;
price within(5000.0000001,9995.00);
output:5.000;
]
};
/ 7. Export data