-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine_test.go
483 lines (388 loc) · 12.4 KB
/
engine_test.go
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package orderbook_test
import (
"math/rand"
"strconv"
"testing"
"time"
"github.com/shal/orderbook"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
type testCase struct {
Orders []*orderbook.Order
Trades []orderbook.Trade
Rejects []*orderbook.Order
}
// New creates new instance of order model.
func NewOrder(id uint64, side orderbook.Side, t orderbook.Kind, price, volume decimal.Decimal) *orderbook.Order {
var locked decimal.Decimal
if t == orderbook.KindMarket {
locked = volume
} else {
locked = orderbook.CalculateLocked(volume, price, side)
}
return &orderbook.Order{
ID: id,
Side: side,
Kind: t,
Price: price,
Volume: volume,
Locked: locked,
}
}
func fabricateOrder(id uint64, amountStr, priceStr string, side orderbook.Side, oType orderbook.Kind) *orderbook.Order {
price, err := decimal.NewFromString(priceStr)
if err != nil {
panic(err)
}
amount, err := decimal.NewFromString(amountStr)
if err != nil {
panic(err)
}
return NewOrder(id, side, oType, price, amount)
}
func randomPrice(min, max float64) string {
price := min + rand.Float64()*(max-min)
return strconv.FormatFloat(price, 'f', 2, 64)
}
func marketOrder(id uint64, amount string, side orderbook.Side) *orderbook.Order {
return fabricateOrder(id, amount, "0.0", side, orderbook.KindMarket)
}
func limitOrder(id uint64, amount, price string, side orderbook.Side) *orderbook.Order {
return fabricateOrder(id, amount, price, side, orderbook.KindLimit)
}
func (tCase *testCase) OrderLimit(side orderbook.Side, amount string, price string) *testCase {
order := limitOrder(uint64(len(tCase.Orders)+1), amount, price, side)
tCase.Orders = append(tCase.Orders, order)
return tCase
}
func (tCase *testCase) OrderMarket(side orderbook.Side, amount, locked string) *testCase {
order := marketOrder(uint64(len(tCase.Orders)+1), amount, side)
order.Locked, _ = decimal.NewFromString(locked)
tCase.Orders = append(tCase.Orders, order)
return tCase
}
func (tCase *testCase) Trade(buyID uint64, sellID uint64, amountx, pricex string) *testCase {
price, err := decimal.NewFromString(pricex)
if err != nil {
panic(err)
}
amount, err := decimal.NewFromString(amountx)
if err != nil {
panic(err)
}
trade := orderbook.NewTrade(
limitOrder(buyID, amountx, pricex, orderbook.SideBuy),
limitOrder(sellID, amountx, pricex, orderbook.SideSell),
amount,
price,
)
tCase.Trades = append(tCase.Trades, *trade)
return tCase
}
func (tCase *testCase) Reject(id uint64, side orderbook.Side) *testCase {
reject := marketOrder(id, "0.0", side)
tCase.Rejects = append(tCase.Rejects, reject)
return tCase
}
func (tCase *testCase) Assert(t *testing.T) {
test := assert.New(t)
book := orderbook.New()
var trades []orderbook.Trade
var rejects []*orderbook.Order
for _, obj := range tCase.Orders {
traded, reject, err := book.Match(obj)
test.NoError(err)
trades = append(trades, traded...)
if reject != nil {
rejects = append(rejects, reject)
}
}
test.Len(trades, len(tCase.Trades), "trades number mismatch")
for i := 0; i < len(trades); i++ {
test.Equal(tCase.Trades[i].Buy.ID, trades[i].Buy.ID)
test.Equal(tCase.Trades[i].Sell.ID, trades[i].Sell.ID)
test.Equal(tCase.Trades[i].Amount, trades[i].Amount)
test.Equal(tCase.Trades[i].Price, trades[i].Price)
}
test.Len(rejects, len(tCase.Rejects), "rejects number mismatch")
for i := 0; i < len(rejects); i++ {
test.Equal(tCase.Rejects[i].ID, rejects[i].ID)
test.Equal(tCase.Rejects[i].Price, rejects[i].Price)
test.Equal(tCase.Rejects[i].Side, rejects[i].Side)
}
}
func TestEngine_Limit_Buy_NoMatch(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderLimit(orderbook.SideBuy, "1.0", "1000.0").
Assert(t)
}
func TestEngine_Limit_Buy_ExactMatch(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderLimit(orderbook.SideBuy, "1.0", "6000.0").
Trade(2, 1, "1.0", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_SamePricePoint(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "1.5", "6000.0").
OrderLimit(orderbook.SideSell, "2.0", "6000.1").
OrderLimit(orderbook.SideBuy, "1.5", "6000.2").
OrderLimit(orderbook.SideBuy, "0.5", "6000.3").
Trade(3, 2, "1.5", "6000.1").
Trade(4, 2, "0.5", "6000.1").
Assert(t)
}
func TestEngine_Limit_Buy_PartialMatch(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderLimit(orderbook.SideBuy, "0.5", "6000.0").
OrderLimit(orderbook.SideBuy, "0.5", "6000.0").
Trade(2, 1, "0.5", "6000.0").
Trade(3, 1, "0.5", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_PartialMatch_Add(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "0.4", "6000.0").
OrderLimit(orderbook.SideSell, "0.5", "6000.0").
OrderLimit(orderbook.SideBuy, "1.0", "6000.0").
Trade(3, 1, "0.4", "6000.0").
Trade(3, 2, "0.5", "6000.0").
Assert(t)
}
func TestEngine_Limit_Sell_PartialMatch_Add(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.4", "6000.0").
OrderLimit(orderbook.SideBuy, "0.5", "6000.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
Trade(1, 3, "0.4", "6000.0").
Trade(2, 3, "0.5", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_PartialMatch_DifferentPrices(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "0.5", "6000.0").
OrderLimit(orderbook.SideSell, "0.5", "6100.0").
OrderLimit(orderbook.SideSell, "0.5", "6110.0").
OrderLimit(orderbook.SideBuy, "1.5", "6200.0").
Trade(4, 1, "0.5", "6000.0").
Trade(4, 2, "0.5", "6100.0").
Trade(4, 3, "0.5", "6110.0").
Assert(t)
}
func TestEngine_Limit_Sell_PartialMatch_DifferentPrices(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.5", "6000.0").
OrderLimit(orderbook.SideBuy, "0.5", "6100.0").
OrderLimit(orderbook.SideBuy, "0.5", "6110.0").
OrderLimit(orderbook.SideSell, "1.5", "6000.0").
Trade(3, 4, "0.5", "6110.0").
Trade(2, 4, "0.5", "6100.0").
Trade(1, 4, "0.5", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_ExactMatch_Search(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6100.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderLimit(orderbook.SideBuy, "1.0", "6000.0").
Trade(3, 2, "1.0", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_Market(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6100.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderMarket(orderbook.SideBuy, "1.0", "6000.0").
Trade(3, 2, "1.0", "6000.0").
Assert(t)
}
func TestEngine_Limit_Buy_MarketNotEnoughLocked(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6200.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderMarket(orderbook.SideBuy, "1.5", "8000.0").
Reject(3, orderbook.SideBuy).
Assert(t)
}
func TestEngine_Limit_Buy_MaxPriceExceeded(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "1.0", "6200.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.0").
OrderMarket(orderbook.SideBuy, "3.0", "20000.0").
Reject(3, orderbook.SideBuy).
Assert(t)
}
func TestEngine_Limit_Buy_Market_Reject(t *testing.T) {
var testcase testCase
testcase.
OrderMarket(orderbook.SideBuy, "1.0", "1.0").
Reject(1, orderbook.SideBuy).
Assert(t)
}
func TestEngine_Limit_Buy_Market_RejectPartialMatch(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.5", "6000.0").
OrderLimit(orderbook.SideSell, "1.0", "6000.1").
OrderMarket(orderbook.SideBuy, "2.0", "61000.0").
Reject(3, orderbook.SideBuy).
Assert(t)
}
func TestEngine_Limit_Buy_Market_AfterPreviousPositionClosed(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "0.8", "5540.0").
OrderLimit(orderbook.SideBuy, "0.8", "5540.0").
Trade(2, 1, "0.8", "5540.0").
OrderLimit(orderbook.SideSell, "0.2", "5563.0").
OrderMarket(orderbook.SideBuy, "0.8", "100000.0").
Reject(4, orderbook.SideBuy).
Assert(t)
}
func TestEngine_Limit_Sell_Market_AfterPreviousPositionClosed(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.8", "5563.0").
OrderLimit(orderbook.SideSell, "0.8", "5563.0").
Trade(1, 2, "0.8", "5563.0").
OrderLimit(orderbook.SideBuy, "0.2", "5540.0").
OrderMarket(orderbook.SideSell, "0.8", "5563.0").
Reject(4, orderbook.SideSell).
Assert(t)
}
func TestEngine_Limit_Buy_Market_LowestPriceFirst(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "0.1", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5100.0").
OrderMarket(orderbook.SideBuy, "0.6", "3050.0").
Trade(3, 1, "0.1", "5000.0").
Trade(3, 2, "0.5", "5100.0").
Assert(t)
}
func TestEngine_Limit_Sell_Market_HighestPriceFirst(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.1", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5100.0").
OrderMarket(orderbook.SideSell, "0.1", "0.1").
Trade(2, 3, "0.1", "5100.0").
Assert(t)
}
func TestEngine_Limit_Sell_Market_MinPriceExceeded(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.1", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5100.0").
OrderMarket(orderbook.SideSell, "0.7", "0.7").
Reject(3, orderbook.SideSell).
Assert(t)
}
func TestEngine_MatchingLimitsOlderFirst(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
Trade(1, 3, "0.5", "5000.0").
Trade(2, 6, "0.5", "5000.0").
Trade(4, 7, "0.5", "5000.0").
Trade(5, 8, "0.5", "5000.0").
Assert(t)
}
func TestEngine_MatchingMarketBuyOlderFirst(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderLimit(orderbook.SideSell, "0.5", "5000.0").
OrderMarket(orderbook.SideBuy, "2.0", "20000.0").
Trade(6, 1, "0.5", "5000.0").
Trade(6, 2, "0.5", "5000.0").
Trade(6, 3, "0.5", "5000.0").
Trade(6, 4, "0.5", "5000.0").
Assert(t)
}
func TestEngine_MatchingMarketSellOlderFirst(t *testing.T) {
var testcase testCase
testcase.
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderLimit(orderbook.SideBuy, "0.5", "5000.0").
OrderMarket(orderbook.SideSell, "2.0", "2.1").
Trade(1, 6, "0.5", "5000.0").
Trade(2, 6, "0.5", "5000.0").
Trade(3, 6, "0.5", "5000.0").
Trade(4, 6, "0.5", "5000.0").
Assert(t)
}
func TestEngine_Cancel(t *testing.T) {
book := orderbook.New()
sell := limitOrder(1, "1.0", "1.0", orderbook.SideSell)
buy := limitOrder(2, "1.0", "1.0", orderbook.SideBuy)
trades, remain, err := book.Match(buy)
assert.NoError(t, err)
assert.Nil(t, remain)
assert.Empty(t, trades)
book.Cancel(buy)
trades, remain, err = book.Match(sell)
assert.NoError(t, err)
assert.Nil(t, remain)
assert.Empty(t, trades)
}
func BenchmarkOrderbook_InsertSellRandomPrice(b *testing.B) {
benchWithPrice(b, func(i int) string {
return randomPrice(100.0, 10000.0)
})
}
func BenchmarkOrderbook_InsertSellAscendingPrice(b *testing.B) {
benchWithPrice(b, func(i int) string {
return strconv.Itoa(i+1) + ".0"
})
}
func BenchmarkOrderbook_InsertSellDescendingPrice(b *testing.B) {
benchWithPrice(b, func(i int) string {
return strconv.Itoa(b.N-i) + ".0"
})
}
func benchWithPrice(b *testing.B, priceGen func(i int) string) {
book := orderbook.New()
fake := make([]*orderbook.Order, b.N)
for i := 0; i < b.N; i++ {
price := priceGen(i)
fake[i] = limitOrder(uint64(i+1), "100.0", price, orderbook.SideSell)
}
start := time.Now()
for i := 0; i < b.N; i++ {
_, _, err := book.Match(fake[i])
if err != nil {
b.Error(err)
}
}
b.Logf("%0.2f orders/sec", float64(b.N)/time.Since(start).Seconds())
}