-
Notifications
You must be signed in to change notification settings - Fork 9
/
orderbook_test.go
743 lines (628 loc) · 19 KB
/
orderbook_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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package orderbook
import (
"bufio"
"fmt"
"regexp"
"strconv"
"strings"
"testing"
decimal "github.com/geseq/udecimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var tok uint64
var depth = `
# add depth to the orderbook
1 L B 2 50 0 N
2 L B 2 60 0 N
3 L B 2 70 0 N
4 L B 2 80 0 N
5 L B 2 90 0 N
6 L S 2 100 0 N
7 L S 2 110 0 N
8 L S 2 120 0 N
9 L S 2 130 0 N
10 L S 2 140 0 N
`
var re = regexp.MustCompile("#.*")
func addPrefix(input, prefix string) string {
lines := []string{}
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' {
continue
}
lines = append(lines, prefix+line)
}
return strings.Join(lines, "\n")
}
func processOrders(ob *OrderBook, input string) {
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' {
continue
}
processLine(ob, line)
}
}
func processLine(ob *OrderBook, line string) {
line = string(re.ReplaceAll([]byte(line), nil))
parts := strings.Split(line, "\t")
if len(parts) == 0 {
return
}
oid, _ := strconv.Atoi(strings.TrimSpace(parts[0]))
class := Market
if strings.TrimSpace(parts[1]) == "L" {
class = Limit
}
side := Buy
if strings.TrimSpace(parts[2]) == "S" {
side = Sell
}
qty, _ := decimal.Parse(strings.TrimSpace(parts[3]))
price, _ := decimal.Parse(strings.TrimSpace(parts[4]))
trigPrice, _ := decimal.Parse(strings.TrimSpace(parts[5]))
flag := None
switch strings.TrimSpace(parts[6]) {
case "I":
flag = IoC
case "A":
flag = AoN
case "F":
flag = FoK
case "SL":
flag = StopLoss
case "TP":
flag = TakeProfit
case "S":
flag = Snapshot
}
ob.AddOrder(tok, uint64(oid), class, side, qty, price, trigPrice, flag)
tok++
}
func addDepth(ob *OrderBook, prefix int) {
d := depth
if prefix > 0 {
d = addPrefix(d, strconv.Itoa(prefix))
}
processOrders(ob, d)
}
func getQtyProcessed(trades *[]Trade) decimal.Decimal {
qty := decimal.Zero
for _, trade := range *trades {
qty = qty.Add(trade.Qty)
}
return qty
}
func getError(notifications []Stringer) error {
for _, notification := range notifications {
if n, ok := notification.(orderNotification); ok && n.Error != nil {
return n.Error
}
}
return nil
}
func TestLimitOrder_Create(t *testing.T) {
n, ob := getTestOrderBook()
for i := 50; i < 100; i = i + 10 {
n.Reset()
processLine(ob, fmt.Sprintf("%d L B 2 %d 0 N", i, i))
n.Verify(t, []string{fmt.Sprintf("CreateOrder Accepted %d 2", i)})
}
for i := 100; i < 150; i = i + 10 {
n.Reset()
processLine(ob, fmt.Sprintf("%d L S 2 %d 0 N", i, i))
n.Verify(t, []string{fmt.Sprintf("CreateOrder Accepted %d 2", i)})
}
assert.Nil(t, ob.Order(999))
assert.NotNil(t, ob.Order(100))
}
func TestLimitOrder_CreateBuy(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "1100 L B 1 100 0 N")
n.Verify(t, []string{
"CreateOrder Accepted 1100 1",
"6 1100 FilledPartial FilledComplete 1 100",
})
n.Reset()
processLine(ob, "1150 L B 10 150 0 N")
n.Verify(t, []string{
"CreateOrder Accepted 1150 10",
"6 1150 FilledComplete FilledPartial 1 100",
"7 1150 FilledComplete FilledPartial 2 110",
"8 1150 FilledComplete FilledPartial 2 120",
"9 1150 FilledComplete FilledPartial 2 130",
"10 1150 FilledComplete FilledPartial 2 140",
})
}
func TestLimitOrder_CreateWithZeroQty(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "170 L S 0 40 0 N")
n.Verify(t, []string{
"CreateOrder Rejected 170 0 ErrInvalidQuantity",
})
}
func TestLimitOrder_CreateWithZeroPrice(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "170 L S 10 0 0 N")
n.Verify(t, []string{
"CreateOrder Rejected 170 0 ErrInvalidPrice",
})
}
func TestLimitOrder_CreateAndCancel(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "170 L S 10 1000 0 N")
ob.CancelOrder(tok, 170)
tok++
n.Verify(t, []string{
"CreateOrder Accepted 170 10",
"CancelOrder Canceled 170 10",
})
}
func TestLimitOrder_CancelNonExistent(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
ob.CancelOrder(tok, 170)
tok++
n.Verify(t, []string{
"CancelOrder Rejected 170 0 ErrOrderNotExists",
})
}
func TestLimitOrder_CreateIOCWithNoMatches(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "300 L S 1 200 0 I")
n.Verify(t, []string{
"CreateOrder Accepted 300 1",
})
}
func TestLimitOrder_CreateIOCWithMatches(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
t.Log(ob)
processLine(ob, "300 L S 1 90 0 I")
n.Verify(t, []string{
"CreateOrder Accepted 300 1",
"5 300 FilledPartial FilledComplete 1 90",
})
}
func TestLimitOrder_CreateSell(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
t.Log(ob)
processLine(ob, "340 L S 11 40 0 N")
processLine(ob, "343 L S 11 1 0 I")
n.Verify(t, []string{
"CreateOrder Accepted 340 11",
"5 340 FilledComplete FilledPartial 2 90",
"4 340 FilledComplete FilledPartial 2 80",
"3 340 FilledComplete FilledPartial 2 70",
"2 340 FilledComplete FilledPartial 2 60",
"1 340 FilledComplete FilledPartial 2 50",
"CreateOrder Accepted 343 11",
})
}
func TestLimitOrder_ClearSellBestPriceFirst(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "900 L B 11 1 0 N")
processLine(ob, "901 L S 11 1 0 N")
require.NoError(t, getError(n.n))
n.Verify(t, []string{
"CreateOrder Accepted 900 11",
"CreateOrder Accepted 901 11",
"5 901 FilledComplete FilledPartial 2 90",
"4 901 FilledComplete FilledPartial 2 80",
"3 901 FilledComplete FilledPartial 2 70",
"2 901 FilledComplete FilledPartial 2 60",
"1 901 FilledComplete FilledPartial 2 50",
"900 901 FilledPartial FilledComplete 1 1",
})
}
func TestMarketProcess(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "800 M B 3 0 0 N")
processLine(ob, "801 M B 0 0 0 N")
processLine(ob, "802 M S 12 0 0 N")
processLine(ob, "803 M B 12 0 0 A")
processLine(ob, "804 M B 12 0 0 N")
n.Verify(t, []string{
"CreateOrder Accepted 800 3",
"6 800 FilledComplete FilledPartial 2 100",
"7 800 FilledPartial FilledComplete 1 110",
"CreateOrder Rejected 801 0 ErrInvalidQuantity",
"CreateOrder Accepted 802 12",
"5 802 FilledComplete FilledPartial 2 90",
"4 802 FilledComplete FilledPartial 2 80",
"3 802 FilledComplete FilledPartial 2 70",
"2 802 FilledComplete FilledPartial 2 60",
"1 802 FilledComplete FilledPartial 2 50",
// TODO Add notification for market order unable to be filled fully
"CreateOrder Accepted 803 12",
"CreateOrder Accepted 804 12",
"7 804 FilledComplete FilledPartial 1 110",
"8 804 FilledComplete FilledPartial 2 120",
"9 804 FilledComplete FilledPartial 2 130",
"10 804 FilledComplete FilledPartial 2 140",
})
}
func TestMarketProcess_PriceLevel_FIFO(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
addDepth(ob, 1)
n.Reset()
processLine(ob, "801 M B 6 0 0 N")
n.Verify(t, []string{
"CreateOrder Accepted 801 6",
"6 801 FilledComplete FilledPartial 2 100",
"16 801 FilledComplete FilledPartial 2 100",
"7 801 FilledComplete FilledComplete 2 110",
})
}
func TestStopPlace(t *testing.T) {
n, ob := getTestOrderBook()
for i := 50; i < 100; i = i + 10 {
processLine(ob, fmt.Sprintf("%d L B 2 %d 10 SL", i, i))
}
for i := 100; i < 150; i = i + 10 {
processLine(ob, fmt.Sprintf("%d L S 2 %d 200 SL", i, i))
}
for i := 150; i < 200; i = i + 10 {
processLine(ob, fmt.Sprintf("%d M B 2 0 5 SL", i))
}
for i := 200; i < 250; i = i + 10 {
processLine(ob, fmt.Sprintf("%d1 L S 2 0 210 SL", i))
processLine(ob, fmt.Sprintf("%d2 M B 2 0 5 SL", i))
}
n.Verify(t, []string{
"CreateOrder Accepted 50 2",
"CreateOrder Accepted 60 2",
"CreateOrder Accepted 70 2",
"CreateOrder Accepted 80 2",
"CreateOrder Accepted 90 2",
"CreateOrder Accepted 100 2",
"CreateOrder Accepted 110 2",
"CreateOrder Accepted 120 2",
"CreateOrder Accepted 130 2",
"CreateOrder Accepted 140 2",
"CreateOrder Accepted 150 2",
"CreateOrder Accepted 160 2",
"CreateOrder Accepted 170 2",
"CreateOrder Accepted 180 2",
"CreateOrder Accepted 190 2",
"CreateOrder Accepted 2001 2",
"CreateOrder Accepted 2002 2",
"CreateOrder Accepted 2101 2",
"CreateOrder Accepted 2102 2",
"CreateOrder Accepted 2201 2",
"CreateOrder Accepted 2202 2",
"CreateOrder Accepted 2301 2",
"CreateOrder Accepted 2302 2",
"CreateOrder Accepted 2401 2",
"CreateOrder Accepted 2402 2",
})
assert.Nil(t, ob.Order(999))
assert.NotNil(t, ob.Order(100))
}
func TestStopProcess(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "100 L B 1 100 110 SL")
processLine(ob, "101 M B 2 0 0 N")
processLine(ob, "102 M B 2 0 0 N") // LP 110
processLine(ob, "103 M S 2 0 0 N")
processLine(ob, "104 M B 1 0 0 N")
processLine(ob, "105 M B 2 0 110 SL") // @ LP 120. This should trigger immediately
processLine(ob, "106 M B 1 0 0 N")
processLine(ob, "107 M S 1 0 0 N")
processLine(ob, "206 M S 2 0 100 SL") // @ LP 90. This should trigger immediately
processLine(ob, "207 M S 1 0 0 N")
processLine(ob, "208 L S 1 100 0 SL")
n.Verify(t, []string{
"CreateOrder Accepted 100 1",
"CreateOrder Accepted 101 2",
"6 101 FilledComplete FilledComplete 2 100",
"CreateOrder Accepted 102 2",
"7 102 FilledComplete FilledComplete 2 110",
"CreateOrder Accepted 103 2",
"100 103 FilledComplete FilledPartial 1 100",
"5 103 FilledPartial FilledComplete 1 90",
"CreateOrder Accepted 104 1",
"8 104 FilledPartial FilledComplete 1 120",
"CreateOrder Accepted 105 2",
"8 105 FilledComplete FilledPartial 1 120",
"9 105 FilledPartial FilledComplete 1 130",
"CreateOrder Accepted 106 1",
"9 106 FilledComplete FilledComplete 1 130",
"CreateOrder Accepted 107 1",
"5 107 FilledComplete FilledComplete 1 90",
"CreateOrder Accepted 206 2",
"4 206 FilledComplete FilledComplete 2 80",
"CreateOrder Accepted 207 1",
"3 207 FilledPartial FilledComplete 1 70",
"CreateOrder Rejected 208 1 ErrInvalidTriggerPrice",
})
}
func TestStopProcess_Limit(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "100 M B 1 0 0 N") // @ LP 100.
processLine(ob, "101 L S 1 90 90 SL")
processLine(ob, "102 M B 1 0 0 N") // @ LP 100.
processLine(ob, "103 M S 2 0 0 N") // @ LP 90. SL S trigger.
processLine(ob, "104 M B 1 0 0 N") // @ LP 90.
processLine(ob, "105 M S 1 0 0 N")
n.Verify(t, []string{
"CreateOrder Accepted 100 1",
"6 100 FilledPartial FilledComplete 1 100",
"CreateOrder Accepted 101 1",
"CreateOrder Accepted 102 1",
"6 102 FilledComplete FilledComplete 1 100",
"CreateOrder Accepted 103 2",
"5 103 FilledComplete FilledComplete 2 90",
"CreateOrder Accepted 104 1",
"101 104 FilledComplete FilledComplete 1 90",
"CreateOrder Accepted 105 1",
"4 105 FilledPartial FilledComplete 1 80",
})
}
func TestStopProcess_Market(t *testing.T) {
n, ob := getTestOrderBook()
addDepth(ob, 0)
n.Reset()
processLine(ob, "100 M B 1 0 0 N") // @ LP 100.
processLine(ob, "101 M S 1 0 90 SL")
processLine(ob, "102 M B 1 0 0 N") // @ LP 100.
processLine(ob, "103 M S 2 0 0 N") // @ LP 90. SL S trigger. LP 80
processLine(ob, "104 M B 1 0 0 N") // @ LP 110.
processLine(ob, "105 M S 1 0 0 N") // @ LP 80
n.Verify(t, []string{
"CreateOrder Accepted 100 1",
"6 100 FilledPartial FilledComplete 1 100",
"CreateOrder Accepted 101 1",
"CreateOrder Accepted 102 1",
"6 102 FilledComplete FilledComplete 1 100",
"CreateOrder Accepted 103 2",
"5 103 FilledComplete FilledComplete 2 90",
"4 101 FilledPartial FilledComplete 1 80",
"CreateOrder Accepted 104 1",
"7 104 FilledPartial FilledComplete 1 110",
"CreateOrder Accepted 105 1",
"4 105 FilledComplete FilledComplete 1 80",
})
}
func TestOrderBook_Ask(t *testing.T) {
n, ob := getTestOrderBook()
n.Reset()
processLine(ob, "1 L S 1 100 0 N") // Best Ask order
processLine(ob, "2 L S 2 110 0 N")
processLine(ob, "3 L S 3 120 0 N")
processLine(ob, "4 L S 1 130 0 N")
bestAsk := ob.Ask(tok)
tok++
require.NotNil(t, bestAsk, "Expected best ask order to be non-nil")
assert.Equal(t, decimal.New(100, 0), bestAsk.Price, "Expected best ask price to be 100")
assert.Equal(t, uint64(1), bestAsk.ID, "Expected best ask order ID to be 1")
n.Verify(t, []string{
"CreateOrder Accepted 1 1",
"CreateOrder Accepted 2 2",
"CreateOrder Accepted 3 3",
"CreateOrder Accepted 4 1",
})
n.Reset()
ob.CancelOrder(tok, bestAsk.ID)
tok++
bestAsk = ob.Ask(tok)
tok++
require.NotNil(t, bestAsk, "Expected best ask order to be non-nil after canceling the first one")
assert.Equal(t, decimal.New(110, 0), bestAsk.Price, "Expected best ask price to be 110 after cancellation")
assert.Equal(t, uint64(2), bestAsk.ID, "Expected best ask order ID to be 2 after cancellation")
n.Verify(t, []string{
"CancelOrder Canceled 1 1",
})
ob.CancelOrder(tok, 2)
tok++
ob.CancelOrder(tok, 3)
tok++
ob.CancelOrder(tok, 4)
tok++
assert.Nil(t, ob.Ask(tok), "Expected Ask to return nil after all sell orders are canceled")
tok++
n.Verify(t, []string{
"CancelOrder Canceled 1 1",
"CancelOrder Canceled 2 2",
"CancelOrder Canceled 3 3",
"CancelOrder Canceled 4 1",
})
}
func TestOrderBook_Bid(t *testing.T) {
n, ob := getTestOrderBook()
n.Reset()
processLine(ob, "1 L B 1 100 0 N") // Best Bid order
processLine(ob, "2 L B 2 90 0 N")
processLine(ob, "3 L B 3 80 0 N")
processLine(ob, "4 L B 1 70 0 N")
bestBid := ob.Bid(tok)
tok++
require.NotNil(t, bestBid, "Expected best bid order to be non-nil")
assert.Equal(t, decimal.New(100, 0), bestBid.Price, "Expected best bid price to be 100")
assert.Equal(t, uint64(1), bestBid.ID, "Expected best bid order ID to be 1")
n.Verify(t, []string{
"CreateOrder Accepted 1 1",
"CreateOrder Accepted 2 2",
"CreateOrder Accepted 3 3",
"CreateOrder Accepted 4 1",
})
n.Reset()
ob.CancelOrder(tok, bestBid.ID)
tok++
bestBid = ob.Bid(tok)
tok++
require.NotNil(t, bestBid, "Expected best bid order to be non-nil after canceling the first one")
assert.Equal(t, decimal.New(90, 0), bestBid.Price, "Expected best bid price to be 90 after cancellation")
assert.Equal(t, uint64(2), bestBid.ID, "Expected best bid order ID to be 2 after cancellation")
n.Verify(t, []string{
"CancelOrder Canceled 1 1",
})
ob.CancelOrder(tok, 2)
tok++
ob.CancelOrder(tok, 3)
tok++
ob.CancelOrder(tok, 4)
tok++
assert.Nil(t, ob.Bid(tok), "Expected Bid to return nil after all buy orders are canceled")
tok++
n.Verify(t, []string{
"CancelOrder Canceled 1 1",
"CancelOrder Canceled 2 2",
"CancelOrder Canceled 3 3",
"CancelOrder Canceled 4 1",
})
}
func TestOrderBook_BidAndAsk(t *testing.T) {
n, ob := getTestOrderBook()
n.Reset()
processLine(ob, "1 L B 1 100 0 N") // Best Bid order
processLine(ob, "2 L B 2 90 0 N")
processLine(ob, "3 L S 1 110 0 N") // Best Ask order
processLine(ob, "4 L S 2 120 0 N")
bestBid := ob.Bid(tok)
tok++
require.NotNil(t, bestBid, "Expected best bid order to be non-nil")
assert.Equal(t, decimal.New(100, 0), bestBid.Price, "Expected best bid price to be 100")
assert.Equal(t, uint64(1), bestBid.ID, "Expected best bid order ID to be 1")
bestAsk := ob.Ask(tok)
tok++
require.NotNil(t, bestAsk, "Expected best ask order to be non-nil")
assert.Equal(t, decimal.New(110, 0), bestAsk.Price, "Expected best ask price to be 110")
assert.Equal(t, uint64(3), bestAsk.ID, "Expected best ask order ID to be 3")
n.Verify(t, []string{
"CreateOrder Accepted 1 1",
"CreateOrder Accepted 2 2",
"CreateOrder Accepted 3 1",
"CreateOrder Accepted 4 2",
})
n.Reset()
ob.CancelOrder(tok, bestBid.ID)
tok++
bestBid = ob.Bid(tok)
tok++
require.NotNil(t, bestBid, "Expected best bid order to be non-nil after cancellation")
assert.Equal(t, decimal.New(90, 0), bestBid.Price, "Expected best bid price to be 90 after cancellation")
assert.Equal(t, uint64(2), bestBid.ID, "Expected best bid order ID to be 2 after cancellation")
ob.CancelOrder(tok, bestAsk.ID)
tok++
bestAsk = ob.Ask(tok)
tok++
require.NotNil(t, bestAsk, "Expected best ask order to be non-nil after cancellation")
assert.Equal(t, decimal.New(120, 0), bestAsk.Price, "Expected best ask price to be 120 after cancellation")
assert.Equal(t, uint64(4), bestAsk.ID, "Expected best ask order ID to be 4 after cancellation")
ob.CancelOrder(tok, 2)
tok++
ob.CancelOrder(tok, 4)
tok++
assert.Nil(t, ob.Bid(tok), "Expected Bid to return nil after all buy orders are canceled")
tok++
assert.Nil(t, ob.Ask(tok), "Expected Ask to return nil after all sell orders are canceled")
tok++
n.Verify(t, []string{
"CancelOrder Canceled 1 1", // Expected cancellation of Bid order 1
"CancelOrder Canceled 3 1", // Expected cancellation of Ask order 3
"CancelOrder Canceled 2 2", // Expected cancellation of Bid order 2
"CancelOrder Canceled 4 2", // Expected cancellation of Ask order 4
})
}
var j uint64
var k uint64 = 100000
func getTestOrderBook() (*Notification, *OrderBook) {
tok = 1
on := &Notification{}
ob := NewOrderBook(on)
return on, ob
}
type orderNotification struct {
MsgType MsgType
Status OrderStatus
OrderID uint64
Qty decimal.Decimal
Error error
}
func (o orderNotification) String() string {
if o.Error != nil {
var errName string
switch o.Error {
case ErrOrderNotExists:
errName = "ErrOrderNotExists"
case ErrInvalidQuantity:
errName = "ErrInvalidQuantity"
case ErrInvalidPrice:
errName = "ErrInvalidPrice"
case ErrOrderID:
errName = "ErrOrderID"
case ErrOrderExists:
errName = "ErrOrderExists"
case ErrInsufficientQuantity:
errName = "ErrInsufficientQuantity"
case ErrNoMatching:
errName = "ErrNoMatching"
case ErrInvalidTriggerPrice:
errName = "ErrInvalidTriggerPrice"
}
return fmt.Sprintf("%s %s %d %s %s", o.MsgType, o.Status, o.OrderID, o.Qty.String(), errName)
}
return fmt.Sprintf("%s %s %d %s", o.MsgType, o.Status, o.OrderID, o.Qty.String())
}
type Stringer interface {
String() string
}
type Notification struct {
n []Stringer
}
func (o *Notification) Reset() {
o.n = []Stringer{}
}
func (o *Notification) PutOrder(m MsgType, s OrderStatus, orderID uint64, qty decimal.Decimal, err error) {
o.n = append(o.n, orderNotification{m, s, orderID, qty, err})
}
func (o *Notification) PutTrade(mID, tID uint64, mStatus, tStatus OrderStatus, qty, price decimal.Decimal) {
o.n = append(o.n, Trade{mID, tID, mStatus, tStatus, qty, price})
}
func (o *Notification) Strings() []string {
res := make([]string, 0, len(o.n))
for _, n := range o.n {
res = append(res, n.String())
}
return res
}
func (o *Notification) String() string {
return strings.TrimSpace(strings.Join(o.Strings(), "\n"))
}
func (o *Notification) Verify(t *testing.T, expected []string) {
assert.Equal(t, expected, o.Strings())
}
type EmptyNotification struct {
}
func (o *EmptyNotification) PutOrder(m MsgType, s OrderStatus, orderID uint64, qty decimal.Decimal, err error) {
}
func (e *EmptyNotification) PutTrade(mID, tID uint64, mStatus, tStatus OrderStatus, qty, price decimal.Decimal) {
}