-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpricelevel.go
248 lines (207 loc) · 5.9 KB
/
pricelevel.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
package orderbook
import (
local_tree "github.com/geseq/orderbook/pkg/tree"
"github.com/geseq/udecimal"
decimal "github.com/geseq/udecimal"
)
// priceLevel implements facade to operations with order queue
type priceLevel struct {
priceTree *local_tree.Tree[decimal.Decimal, *orderQueue]
priceType PriceType
volume decimal.Decimal
numOrders uint64
depth int
}
// Comparator compares two Decimal objects
func Comparator(a, b decimal.Decimal) int {
return a.Cmp(b)
}
// PriceType represents the type of price levels
type PriceType byte
const (
// BidPrice is for big orders
BidPrice PriceType = iota
// AskPrice is for ask orders
AskPrice
// TrigPrice is for stop loss / take profit orders
TrigPrice
// TakePrice is for take profit orders
TakePrice
)
// newPriceLevel creates new priceLevel manager
func newPriceLevel(priceType PriceType) *priceLevel {
return &priceLevel{
priceTree: local_tree.NewWithTree[udecimal.Decimal, *orderQueue](Comparator, 1),
priceType: priceType,
volume: decimal.Zero,
}
}
// Len returns amount of orders
func (pl *priceLevel) Len() uint64 {
return pl.numOrders
}
// Depth returns depth of market
func (pl *priceLevel) Depth() int {
return pl.depth
}
// Volume returns total amount of quantity in side
func (pl *priceLevel) Volume() decimal.Decimal {
return pl.volume
}
// Append appends order to definite price level
func (pl *priceLevel) Append(o *Order) *Order {
price := o.GetPrice(pl.priceType)
priceQueue, ok := pl.priceTree.Get(price)
if !ok {
priceQueue = newOrderQueue(price)
pl.priceTree.Put(price, priceQueue)
pl.depth++
}
pl.numOrders++
pl.volume = pl.volume.Add(o.Qty)
o.queue = priceQueue
return priceQueue.Append(o)
}
// Remove removes order from definite price level
func (pl *priceLevel) Remove(o *Order) *Order {
price := o.GetPrice(pl.priceType)
priceQueue := o.queue
o = priceQueue.Remove(o)
if priceQueue.Len() == 0 {
pl.priceTree.Remove(price)
pl.depth--
priceQueue.Release()
}
pl.numOrders--
pl.volume = pl.volume.Sub(o.Qty)
return o
}
// MaxPriceQueue returns maximal level of price
func (pl *priceLevel) MaxPriceQueue() *orderQueue {
if pl.depth > 0 {
if value, found := pl.priceTree.GetMax(); found {
return value.Value
}
}
return nil
}
// MinPriceQueue returns maximal level of price
func (pl *priceLevel) MinPriceQueue() *orderQueue {
if pl.depth > 0 {
if value, found := pl.priceTree.GetMin(); found {
return value.Value
}
}
return nil
}
// LargestLessThan returns largest orderQueue with price less than given
func (pl *priceLevel) LargestLessThan(price decimal.Decimal) *orderQueue {
if node, ok := pl.priceTree.LargestLessThan(price); ok {
return node.Value
}
return nil
}
// SmallestGreaterThan returns smallest orderQueue with price greater than given
func (pl *priceLevel) SmallestGreaterThan(price decimal.Decimal) *orderQueue {
if node, ok := pl.priceTree.SmallestGreaterThan(price); ok {
return node.Value
}
return nil
}
// LargestLessThanOrEqual returns largest orderQueue with price less than or
// equal to given
func (pl *priceLevel) LargestLessThanOrEqual(price decimal.Decimal) *orderQueue {
if node, ok := pl.priceTree.Floor(price); ok {
return node.Value
}
return nil
}
// SmallestGreaterThanOrEqual returns smallest orderQueue with price greater than
// or equal to given
func (pl *priceLevel) SmallestGreaterThanOrEqual(price decimal.Decimal) *orderQueue {
if node, ok := pl.priceTree.Ceiling(price); ok {
return node.Value
}
return nil
}
// Orders returns all of * orders
func (pl *priceLevel) Orders() (orders []*Order) {
it := pl.priceTree.Iterator()
for i := 0; it.Next(); i++ {
iter := it.Value().Head()
for iter != nil {
orders = append(orders, iter)
iter = iter.next
}
}
return
}
// GetQueue returns the max/min order queue by the price type
func (pl *priceLevel) GetQueue() *orderQueue {
switch pl.priceType {
case BidPrice:
return pl.MaxPriceQueue()
case AskPrice:
return pl.MinPriceQueue()
default:
panic("invalid call to GetQueue")
}
}
// GetNextQueue returns the next level order queue by the price type
func (pl *priceLevel) GetNextQueue(price decimal.Decimal) *orderQueue {
switch pl.priceType {
case BidPrice:
return pl.LargestLessThan(price)
case AskPrice:
return pl.SmallestGreaterThan(price)
default:
panic("invalid call to GetQueue")
}
}
func (pl *priceLevel) processMarketOrder(ob *OrderBook, takerOrderID uint64, qty decimal.Decimal, flag FlagType) (qtyProcessed decimal.Decimal) {
// TODO: This wont work as priceLevel volumes aren't accounted for corectly
if flag&(AoN|FoK) != 0 && qty.GreaterThan(pl.Volume()) {
return decimal.Zero
}
qtyLeft := qty
qtyProcessed = decimal.Zero
for orderQueue := pl.GetQueue(); qtyLeft.GreaterThan(decimal.Zero) && orderQueue != nil; orderQueue = pl.GetQueue() {
_, q := orderQueue.process(ob, takerOrderID, qtyLeft)
qtyLeft = qtyLeft.Sub(q)
qtyProcessed = qtyProcessed.Add(q)
}
return
}
func (pl *priceLevel) processLimitOrder(ob *OrderBook, compare func(price decimal.Decimal) bool, takerOrderID uint64, qty decimal.Decimal, flag FlagType) (qtyProcessed decimal.Decimal) {
orderQueue := pl.GetQueue()
if orderQueue == nil || !compare(orderQueue.Price()) {
return
}
// TODO: Fix AoN
if flag&(AoN|FoK) != 0 {
if qty.GreaterThan(pl.Volume()) {
return decimal.Zero
}
canFill := false
for orderQueue != nil && compare(orderQueue.Price()) {
if qty.LessThanOrEqual(orderQueue.TotalQty()) {
canFill = true
break
}
qty = qty.Sub(orderQueue.TotalQty())
orderQueue = pl.GetNextQueue(orderQueue.Price())
}
if !canFill {
return decimal.Zero
}
}
orderQueue = pl.GetQueue()
qtyLeft := qty
qtyProcessed = decimal.Zero
for orderQueue := pl.GetQueue(); qtyLeft.GreaterThan(decimal.Zero) && orderQueue != nil; orderQueue = pl.GetQueue() {
_, q := orderQueue.process(ob, takerOrderID, qtyLeft)
qtyLeft = qtyLeft.Sub(q)
qtyProcessed = qtyProcessed.Add(q)
}
return
}