-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathqueue.go
More file actions
315 lines (272 loc) · 7.23 KB
/
queue.go
File metadata and controls
315 lines (272 loc) · 7.23 KB
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
package match
import (
"time"
"github.com/quagmt/udecimal"
"github.com/0x5487/matching-engine/protocol"
"github.com/0x5487/matching-engine/structure"
)
// UpdateEvent represents a change in price or size at a specific price level.
type UpdateEvent struct {
Price string `json:"price"`
Size string `json:"size"`
}
type priceUnit struct {
totalSize udecimal.Decimal
head *Order
tail *Order
count int64
}
type queue struct {
side Side
totalOrders int64
depths int64
orderedPrices *structure.PooledSkiplist // Ordered price levels
priceList map[udecimal.Decimal]*priceUnit // Quick lookup by price
orders map[string]*Order
}
const defaultPriceCapacity = 3000
// newBuyerQueue creates a new queue for buy orders (bids).
// The orders are sorted by price in descending order (highest price first).
func newBuyerQueue() *queue {
return &queue{
side: Buy,
orderedPrices: structure.NewPooledSkiplistWithOptions(
defaultPriceCapacity,
time.Now().UnixNano(),
structure.SkiplistOptions{Descending: true}, // Highest first
),
priceList: make(map[udecimal.Decimal]*priceUnit),
orders: make(map[string]*Order),
}
}
// newSellerQueue creates a new queue for sell orders (asks).
// The orders are sorted by price in ascending order (lowest price first).
func newSellerQueue() *queue {
return &queue{
side: Sell,
orderedPrices: structure.NewPooledSkiplist(
defaultPriceCapacity,
time.Now().UnixNano(),
), // Default: Lowest first
priceList: make(map[udecimal.Decimal]*priceUnit),
orders: make(map[string]*Order),
}
}
// order finds an order by its ID.
func (q *queue) order(id string) *Order {
return q.orders[id]
}
// insertOrder inserts an order into the end of the queue for its price level.
func (q *queue) insertOrder(order *Order) {
q.doInsert(order, false)
}
// pushFront inserts an order into the front of the queue for its price level.
func (q *queue) pushFront(order *Order) {
q.doInsert(order, true)
}
//nolint:revive // flag-parameter: internal method used by insertOrder and pushFront
func (q *queue) doInsert(order *Order, isFront bool) {
unit, ok := q.priceList[order.Price]
if ok {
if isFront {
// Push Front
order.next = unit.head
order.prev = nil
if unit.head != nil {
unit.head.prev = order
}
unit.head = order
if unit.tail == nil {
unit.tail = order
}
} else {
// Push Back
order.prev = unit.tail
order.next = nil
if unit.tail != nil {
unit.tail.next = order
}
unit.tail = order
if unit.head == nil {
unit.head = order
}
}
unit.totalSize = unit.totalSize.Add(order.Size)
unit.count++
q.orders[order.ID] = order
q.totalOrders++
} else {
unit := &priceUnit{
head: order,
tail: order,
totalSize: order.Size,
count: 1,
}
order.next = nil
order.prev = nil
q.orders[order.ID] = order
q.priceList[order.Price] = unit
q.orderedPrices.MustInsert(order.Price)
q.totalOrders++
q.depths++
}
}
// removeOrder removes an order from the queue by price and ID.
// It also cleans up the price unit if it becomes empty.
func (q *queue) removeOrder(price udecimal.Decimal, id string) {
unit, ok := q.priceList[price]
if !ok {
return
}
order, ok := q.orders[id]
if !ok {
return
}
// Remove from linked list
if order.prev != nil {
order.prev.next = order.next
} else {
unit.head = order.next
}
if order.next != nil {
order.next.prev = order.prev
} else {
unit.tail = order.prev
}
// Clear pointers to avoid leaks and for safety in pool
order.next = nil
order.prev = nil
unit.totalSize = unit.totalSize.Sub(order.Size)
unit.count--
delete(q.orders, id)
q.totalOrders--
if unit.count == 0 {
q.orderedPrices.Delete(price)
delete(q.priceList, price)
q.depths--
}
}
// updateOrderSize updates the size of an order in-place.
// This is used when the size is decreased, preserving the order's priority.
// The caller should NOT update order.Size before calling this, or this method will not update unit.totalSize correctly.
func (q *queue) updateOrderSize(id string, newSize udecimal.Decimal) {
order, ok := q.orders[id]
if !ok {
return
}
unit, ok := q.priceList[order.Price]
if ok {
diff := order.Size.Sub(newSize)
unit.totalSize = unit.totalSize.Sub(diff)
order.Size = newSize
}
}
// peekHeadOrder returns the order at the front of the queue (best price) without removing it.
func (q *queue) peekHeadOrder() *Order {
bestPrice, ok := q.orderedPrices.Min()
if !ok {
return nil
}
unit, ok := q.priceList[bestPrice]
if !ok {
return nil
}
return unit.head
}
// popHeadOrder removes and returns the order at the front of the queue.
func (q *queue) popHeadOrder() *Order {
ord := q.peekHeadOrder()
if ord != nil {
q.removeOrder(ord.Price, ord.ID)
}
return ord
}
// orderCount returns the total number of orders in the queue.
func (q *queue) orderCount() int64 {
return q.totalOrders
}
// depthCount returns the number of price levels in the queue.
func (q *queue) depthCount() int64 {
return q.depths
}
// toSnapshot serializes the queue into a slice of *Order pointers.
func (q *queue) toSnapshot() []*Order {
snapshots := make([]*Order, 0, q.totalOrders)
// Iterate over all price levels
iter := q.orderedPrices.Iterator()
for iter.Valid() {
price := iter.Price()
unit, ok := q.priceList[price]
if !ok {
iter.Next()
continue
}
// Iterate over all orders at this price level
order := unit.head
for order != nil {
o := &Order{
ID: order.ID,
Side: order.Side,
Price: order.Price,
Size: order.Size,
UserID: order.UserID,
Type: order.Type,
Timestamp: order.Timestamp,
VisibleLimit: order.VisibleLimit,
HiddenSize: order.HiddenSize,
}
snapshots = append(snapshots, o)
order = order.next
}
iter.Next()
}
return snapshots
}
// depth returns the order book depth up to the specified limit.
func (q *queue) depth(limit uint32) []*protocol.DepthItem {
result := make([]*protocol.DepthItem, 0, limit)
it := q.priceIterator()
count := uint32(0)
for it.Valid() && count < limit {
price, unit := it.PriceUnit()
d := &protocol.DepthItem{
Price: price.String(),
Size: unit.totalSize.String(),
Count: unit.count,
}
result = append(result, d)
count++
it.Next()
}
return result
}
// priceIterator returns an iterator over price levels.
// Used for FOK order validation.
func (q *queue) priceIterator() *queuePriceIterator {
iter := q.orderedPrices.Iterator()
return &queuePriceIterator{
skipIter: iter,
priceList: q.priceList,
}
}
// queuePriceIterator iterates over price levels in the queue.
type queuePriceIterator struct {
skipIter *structure.SkiplistIterator
priceList map[udecimal.Decimal]*priceUnit
}
// Valid returns true if the iterator points to a valid price level.
func (it *queuePriceIterator) Valid() bool {
return it.skipIter.Valid()
}
// Next advances the iterator to the next price level.
func (it *queuePriceIterator) Next() {
it.skipIter.Next()
}
// PriceUnit returns the current price and its priceUnit.
func (it *queuePriceIterator) PriceUnit() (udecimal.Decimal, *priceUnit) {
if !it.skipIter.Valid() {
return udecimal.Zero, nil
}
price := it.skipIter.Price()
return price, it.priceList[price]
}