-
Notifications
You must be signed in to change notification settings - Fork 59
/
orderbook_test.go
153 lines (133 loc) · 2.84 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
package hftorderbook
import (
"testing"
"math"
"math/rand"
//"fmt"
)
func TestOrderbookEmpty(t *testing.T) {
b := NewOrderbook()
if b.BLength() != 0 {
t.Errorf("book should be empty")
}
if b.ALength() != 0 {
t.Errorf("book should be empty")
}
}
func TestOrderbookAddOne(t *testing.T) {
b := NewOrderbook()
bid := &Order{
BidOrAsk: true,
}
ask := &Order{
BidOrAsk: false,
}
b.Add(1.0, bid)
b.Add(2.0, ask)
if b.BLength() != 1 {
t.Errorf("book should have 1 bid")
}
if b.ALength() != 1 {
t.Errorf("book should have 1 ask")
}
}
func TestOrderbookAddMultiple(t *testing.T) {
b := NewOrderbook()
for i := 0; i < 100; i += 1 {
bid := &Order{
BidOrAsk: true,
}
b.Add(float64(i), bid)
}
for i := 100; i < 200; i += 1 {
bid := &Order{
BidOrAsk: false,
}
b.Add(float64(i), bid)
}
if b.BLength() != 100 {
t.Errorf("book should have 100 bids")
}
if b.ALength() != 100 {
t.Errorf("book should have 100 asks")
}
if b.GetBestBid() != 99.0 {
t.Errorf("best bid should be 99.0")
}
if b.GetBestOffer() != 100.0 {
t.Errorf("best offer should be 100.0")
}
}
func TestOrderbookAddAndCancel(t *testing.T) {
b := NewOrderbook()
bid1 := &Order{
Id: 1,
BidOrAsk: true,
}
bid2 := &Order{
Id: 2,
BidOrAsk: true,
}
b.Add(1.0, bid1)
b.Add(2.0, bid2)
if b.GetBestBid() != 2.0 {
t.Errorf("best bid should be 2.0")
}
b.Cancel(bid2)
if b.GetBestBid() != 1.0 {
t.Errorf("best bid should be 1.0 now")
}
}
func TestGetVolumeAtLimit(t *testing.T) {
b := NewOrderbook()
bid1 := &Order{
Id: 1,
BidOrAsk: true,
Volume: 0.1,
}
bid2 := &Order{
Id: 2,
BidOrAsk: true,
Volume: 0.2,
}
b.Add(1.0, bid1)
b.Add(1.0, bid2)
if math.Abs(b.GetVolumeAtBidLimit(1.0) - 0.3) > 0.0000001 {
t.Errorf("invalid volume at limit: %0.8f", b.GetVolumeAtBidLimit(1.0))
}
}
func benchmarkOrderbookLimitedRandomInsert(n int, b *testing.B) {
book := NewOrderbook()
// maximum number of levels in average is 10k
limitslist := make([]float64, n)
for i := range limitslist {
limitslist[i] = rand.Float64()
}
// preallocate empty orders
orders := make([]*Order, 0, b.N)
for i := 0; i < b.N; i += 1 {
orders = append(orders, &Order{})
}
// measure insertion time
b.ResetTimer()
for i := 0; i < b.N; i += 1 {
price := limitslist[rand.Intn(len(limitslist))]
// create a new order
o := orders[i]
o.Id = i
o.Volume = rand.Float64()
o.BidOrAsk = price < 0.5
// add to the book
book.Add(price, o)
}
//fmt.Printf("bid size %d, ask size %d\n", book.BLength(), book.ALength())
}
func BenchmarkOrderbook5kLevelsRandomInsert(b *testing.B) {
benchmarkOrderbookLimitedRandomInsert(5000, b)
}
func BenchmarkOrderbook10kLevelsRandomInsert(b *testing.B) {
benchmarkOrderbookLimitedRandomInsert(10000, b)
}
func BenchmarkOrderbook20kLevelsRandomInsert(b *testing.B) {
benchmarkOrderbookLimitedRandomInsert(20000, b)
}