-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.go
275 lines (244 loc) · 7.27 KB
/
book.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
package orderbook
import "errors"
// Book is a limit-price orderbook for a particular instrument,
// that matches buys and sells in continuous time.
type Book struct {
bidTree *limitPriceTree
askTree *limitPriceTree
bestBid *limitPrice
bestAsk *limitPrice
orderMap map[OrderID]*order
bidMap map[uint]*limitPrice
askMap map[uint]*limitPrice
}
// Init initializes a new order book.
func Init() *Book {
return &Book{
bidTree: &limitPriceTree{},
askTree: &limitPriceTree{},
orderMap: make(map[OrderID]*order),
bidMap: make(map[uint]*limitPrice),
askMap: make(map[uint]*limitPrice),
}
}
// Submit submits a single order to the order book. The order must have a
// specified Side, either Buy or Sell, a nonzero price and nonzero size.
//
// The return values are:
// * The order id for this order created during the matching process.
// * A list a order executions that, if order matching was possible, will include the execution details
// of the orders that are matched, including the originally submitted order.
// * An optional error.
func (b *Book) Submit(side Side, price uint, size uint) (OrderID, []Execution, error) {
var (
matchedQty uint
matches []Execution
)
if price == 0 || size == 0 {
return 0, matches, errors.New("price/size cannot be zero")
}
// General methodology:
// Check if we can match immediately at the best bid/offer,
// taking liquidity up to the price limit specified.
// handle partial fills,
// report order executions
// handle inserting into the book if we cant fill the entire order.
newOrderID := genID()
if side == Bid {
// Looking to buy, match aginst existing asks.
matchedQty, matches = b.matchBid(price, size)
} else {
// Looking to sell, match against existing bids.
matchedQty, matches = b.matchAsk(price, size)
}
// Report the fact that the submitted order
// has been at least partially matched.
if matchedQty != 0 {
matches = append(matches, Execution{OrderID: newOrderID,
FilledQuantity: matchedQty,
RemainingQuantity: size - matchedQty})
}
// Add new order to the book if the new order wasn't completely filled.
if matchedQty != size {
o := &order{
id: newOrderID,
price: price,
size: size - matchedQty,
}
b.orderMap[newOrderID] = o
if side == Bid {
// Check if the price limit already exists.
lim, ok := b.bidMap[price]
if !ok {
// Doesn't exist, add new limit/order to tree.
lim = b.bidTree.addLimit(price, o)
} else {
// Exists, just add the order to it.
lim.orders.add(o)
}
// Adjust best bid if needed.
if b.bestBid == nil || price > b.bestBid.price {
b.bestBid = lim
}
} else {
// Check if the price limit already exists.
lim, ok := b.askMap[price]
if !ok {
// Doesn't exist, add new limit/order to tree.
lim = b.askTree.addLimit(price, o)
} else {
// Exists, just add the order to it.
lim.orders.add(o)
}
// Adjust best bid if needed.
if b.bestAsk == nil || price > b.bestAsk.price {
b.bestAsk = lim
}
}
}
return newOrderID, matches, nil
}
func (b *Book) matchBid(bidPrice, bidSize uint) (uint, []Execution) {
// Matching methodology:
//
// find best ask price.
// iterate through existing ask orders at that price.
// if an ask order is filled, remove from the
// order list and remove from the order map.
// keep iterating until the bid is filled.
// if the buy cannot be filled entirely at this price, advance to the next-best ask price.
// if this happens, delete the old best ask limit from the ask tree
// and delete the ask limit from the price map
// repeat this process until the next ask limit is higher than the bidPrice,
// there are no more ask limits, or the bid is filled.
matches := []Execution{}
remaining := bidSize
for remaining != 0 {
if b.bestAsk == nil || bidPrice < b.bestAsk.price {
// Cant match, exit.
break
}
potentialmatch := b.bestAsk.orders.first
if potentialmatch == nil {
// No orders left at this level. advance to the next limit price.
next := b.bestAsk.higher()
b.askTree.removeLimit(b.bestAsk.price)
delete(b.askMap, b.bestAsk.price)
b.bestAsk = next
continue
}
if potentialmatch.size <= remaining {
// Fill existing order and remove from order map.
remaining -= potentialmatch.size
delete(b.orderMap, potentialmatch.id)
b.bestAsk.orders.remove(0)
matches = append(matches, Execution{
OrderID: potentialmatch.id,
FilledQuantity: potentialmatch.size,
RemainingQuantity: 0,
})
} else {
// Partial fill the existing ask order and move on.
matches = append(matches, Execution{
OrderID: potentialmatch.id,
FilledQuantity: remaining,
RemainingQuantity: potentialmatch.size - remaining,
})
potentialmatch.size -= remaining
remaining = 0
}
}
return bidSize - remaining, matches
}
func (b *Book) matchAsk(askPrice, askSize uint) (uint, []Execution) {
matches := []Execution{}
remaining := askSize
for remaining != 0 {
if b.bestBid == nil || askPrice > b.bestBid.price {
// Cant match, exit.
break
}
potentialmatch := b.bestBid.orders.first
if potentialmatch == nil {
// No orders left at this price. advance to the next limit price.
next := b.bestBid.lower()
b.bidTree.removeLimit(b.bestBid.price)
delete(b.bidMap, b.bestBid.price)
b.bestBid = next
continue
}
if potentialmatch.size <= remaining {
// Fill existing bid order and remove from map.
remaining -= potentialmatch.size
delete(b.orderMap, potentialmatch.id)
b.bestBid.orders.remove(0)
matches = append(matches, Execution{
OrderID: potentialmatch.id,
FilledQuantity: potentialmatch.size,
RemainingQuantity: 0,
})
} else {
// Partial fill the existing bid order and move on.
matches = append(matches, Execution{
OrderID: potentialmatch.id,
FilledQuantity: remaining,
RemainingQuantity: potentialmatch.size - remaining,
})
potentialmatch.size -= remaining
remaining = 0
}
}
return askSize - remaining, matches
}
// Cancel order.
func (b *Book) Cancel(id OrderID) (bool, error) {
// Check existence in map and return if not in.
order, orderExists := b.orderMap[id]
if !orderExists {
return false, errors.New("order does not exist")
}
p := order.price
s := order.side
// Remove order from orders map and remove order from its list.
var (
lim *limitPrice
priceExists bool
)
if s == Bid {
lim, priceExists = b.bidMap[p]
} else {
lim, priceExists = b.askMap[p]
}
if !priceExists {
return false, errors.New("price does not exist, this is should not happen")
}
delete(b.orderMap, id)
lim.orders.removeID(id)
// If that order was the last in its price level, remove that price level
// from the price level map and from the bid/ask tree.
//
// If a removed price level is the best bid/ask, the best bid/ask need to
// be replaced with the next best.
if lim.orders.Size() == 0 {
if s == Bid {
//
if b.bestBid.price == p {
b.bestBid = b.bestBid.lower()
}
delete(b.bidMap, p)
b.bidTree.removeLimit(p)
} else {
//
if b.bestAsk.price == p {
b.bestAsk = b.bestAsk.higher()
}
delete(b.askMap, p)
b.askTree.removeLimit(p)
}
}
return true, nil
}
// Top of the book.
func (b *Book) Top() (bid, ask uint) {
return b.bestBid.price, b.bestAsk.price
}