-
Notifications
You must be signed in to change notification settings - Fork 153
/
orderbook.go
350 lines (295 loc) · 9.81 KB
/
orderbook.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
package orderbook
import (
"container/list"
"encoding/json"
"time"
"github.com/shopspring/decimal"
)
// OrderBook implements standard matching algorithm
type OrderBook struct {
orders map[string]*list.Element // orderID -> *Order (*list.Element.Value.(*Order))
asks *OrderSide
bids *OrderSide
}
// NewOrderBook creates Orderbook object
func NewOrderBook() *OrderBook {
return &OrderBook{
orders: map[string]*list.Element{},
bids: NewOrderSide(),
asks: NewOrderSide(),
}
}
// PriceLevel contains price and volume in depth
type PriceLevel struct {
Price decimal.Decimal `json:"price"`
Quantity decimal.Decimal `json:"quantity"`
}
// ProcessMarketOrder immediately gets definite quantity from the order book with market price
// Arguments:
// side - what do you want to do (ob.Sell or ob.Buy)
// quantity - how much quantity you want to sell or buy
// * to create new decimal number you should use decimal.New() func
// read more at https://github.com/shopspring/decimal
// Return:
// error - not nil if price is less or equal 0
// done - not nil if your market order produces ends of anoter orders, this order will add to
// the "done" slice
// partial - not nil if your order has done but top order is not fully done
// partialQuantityProcessed - if partial order is not nil this result contains processed quatity from partial order
// quantityLeft - more than zero if it is not enought orders to process all quantity
func (ob *OrderBook) CalculatePriceAfterExecution(side Side,quantity decimal.Decimal) (price decimal.Decimal, err error) {
price = decimal.Zero
var (
level *OrderQueue
iter func(decimal.Decimal) *OrderQueue
)
if side == Buy {
level = ob.asks.MinPriceQueue()
iter = ob.asks.GreaterThan
} else {
level = ob.bids.MaxPriceQueue()
iter = ob.bids.LessThan
}
for quantity.Sign() > 0 && level != nil {
levelVolume := level.Volume()
levelPrice := level.Price()
if quantity.GreaterThanOrEqual(levelVolume) {
price = levelPrice
quantity = quantity.Sub(levelVolume)
level = iter(levelPrice)
} else {
price = levelPrice
quantity = decimal.Zero
}
}
return
}
func (ob *OrderBook) ProcessMarketOrder(side Side, quantity decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed, quantityLeft decimal.Decimal, err error) {
if quantity.Sign() <= 0 {
return nil, nil, decimal.Zero, decimal.Zero, ErrInvalidQuantity
}
var (
iter func() *OrderQueue
sideToProcess *OrderSide
)
if side == Buy {
iter = ob.asks.MinPriceQueue
sideToProcess = ob.asks
} else {
iter = ob.bids.MaxPriceQueue
sideToProcess = ob.bids
}
for quantity.Sign() > 0 && sideToProcess.Len() > 0 {
bestPrice := iter()
ordersDone, partialDone, partialProcessed, quantityLeft := ob.processQueue(bestPrice, quantity)
done = append(done, ordersDone...)
partial = partialDone
partialQuantityProcessed = partialProcessed
quantity = quantityLeft
}
quantityLeft = quantity
return
}
// ProcessLimitOrder places new order to the OrderBook
// Arguments:
// side - what do you want to do (ob.Sell or ob.Buy)
// orderID - unique order ID in depth
// quantity - how much quantity you want to sell or buy
// price - no more expensive (or cheaper) this price
// * to create new decimal number you should use decimal.New() func
// read more at https://github.com/shopspring/decimal
// Return:
// error - not nil if quantity (or price) is less or equal 0. Or if order with given ID is exists
// done - not nil if your order produces ends of anoter order, this order will add to
// the "done" slice. If your order have done too, it will be places to this array too
// partial - not nil if your order has done but top order is not fully done. Or if your order is
// partial done and placed to the orderbook without full quantity - partial will contain
// your order with quantity to left
// partialQuantityProcessed - if partial order is not nil this result contains processed quatity from partial order
func (ob *OrderBook) ProcessLimitOrder(side Side, orderID string, quantity, price decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed decimal.Decimal, err error) {
if _, ok := ob.orders[orderID]; ok {
return nil, nil, decimal.Zero, ErrOrderExists
}
if quantity.Sign() <= 0 {
return nil, nil, decimal.Zero, ErrInvalidQuantity
}
if price.Sign() <= 0 {
return nil, nil, decimal.Zero, ErrInvalidPrice
}
quantityToTrade := quantity
var (
sideToProcess *OrderSide
sideToAdd *OrderSide
comparator func(decimal.Decimal) bool
iter func() *OrderQueue
)
if side == Buy {
sideToAdd = ob.bids
sideToProcess = ob.asks
comparator = price.GreaterThanOrEqual
iter = ob.asks.MinPriceQueue
} else {
sideToAdd = ob.asks
sideToProcess = ob.bids
comparator = price.LessThanOrEqual
iter = ob.bids.MaxPriceQueue
}
bestPrice := iter()
for quantityToTrade.Sign() > 0 && sideToProcess.Len() > 0 && comparator(bestPrice.Price()) {
ordersDone, partialDone, partialQty, quantityLeft := ob.processQueue(bestPrice, quantityToTrade)
done = append(done, ordersDone...)
partial = partialDone
partialQuantityProcessed = partialQty
quantityToTrade = quantityLeft
bestPrice = iter()
}
if quantityToTrade.Sign() > 0 {
o := NewOrder(orderID, side, quantityToTrade, price, time.Now().UTC())
if len(done) > 0 {
partialQuantityProcessed = quantity.Sub(quantityToTrade)
partial = o
}
ob.orders[orderID] = sideToAdd.Append(o)
} else {
totalQuantity := decimal.Zero
totalPrice := decimal.Zero
for _, order := range done {
totalQuantity = totalQuantity.Add(order.Quantity())
totalPrice = totalPrice.Add(order.Price().Mul(order.Quantity()))
}
if partialQuantityProcessed.Sign() > 0 {
totalQuantity = totalQuantity.Add(partialQuantityProcessed)
totalPrice = totalPrice.Add(partial.Price().Mul(partialQuantityProcessed))
}
done = append(done, NewOrder(orderID, side, quantity, totalPrice.Div(totalQuantity), time.Now().UTC()))
}
return
}
func (ob *OrderBook) processQueue(orderQueue *OrderQueue, quantityToTrade decimal.Decimal) (done []*Order, partial *Order, partialQuantityProcessed, quantityLeft decimal.Decimal) {
quantityLeft = quantityToTrade
for orderQueue.Len() > 0 && quantityLeft.Sign() > 0 {
headOrderEl := orderQueue.Head()
headOrder := headOrderEl.Value.(*Order)
if quantityLeft.LessThan(headOrder.Quantity()) {
partial = NewOrder(headOrder.ID(), headOrder.Side(), headOrder.Quantity().Sub(quantityLeft), headOrder.Price(), headOrder.Time())
partialQuantityProcessed = quantityLeft
orderQueue.Update(headOrderEl, partial)
quantityLeft = decimal.Zero
} else {
quantityLeft = quantityLeft.Sub(headOrder.Quantity())
done = append(done, ob.CancelOrder(headOrder.ID()))
}
}
return
}
// Order returns order by id
func (ob *OrderBook) Order(orderID string) *Order {
e, ok := ob.orders[orderID]
if !ok {
return nil
}
return e.Value.(*Order)
}
// Depth returns price levels and volume at price level
func (ob *OrderBook) Depth() (asks, bids []*PriceLevel) {
level := ob.asks.MaxPriceQueue()
for level != nil {
asks = append(asks, &PriceLevel{
Price: level.Price(),
Quantity: level.Volume(),
})
level = ob.asks.LessThan(level.Price())
}
level = ob.bids.MaxPriceQueue()
for level != nil {
bids = append(bids, &PriceLevel{
Price: level.Price(),
Quantity: level.Volume(),
})
level = ob.bids.LessThan(level.Price())
}
return
}
// CancelOrder removes order with given ID from the order book
func (ob *OrderBook) CancelOrder(orderID string) *Order {
e, ok := ob.orders[orderID]
if !ok {
return nil
}
delete(ob.orders, orderID)
if e.Value.(*Order).Side() == Buy {
return ob.bids.Remove(e)
}
return ob.asks.Remove(e)
}
// CalculateMarketPrice returns total market price for requested quantity
// if err is not nil price returns total price of all levels in side
func (ob *OrderBook) CalculateMarketPrice(side Side, quantity decimal.Decimal) (price decimal.Decimal,quant decimal.Decimal, err error) {
price = decimal.Zero
quant = decimal.Zero
var (
level *OrderQueue
iter func(decimal.Decimal) *OrderQueue
)
if side == Buy {
level = ob.asks.MinPriceQueue()
iter = ob.asks.GreaterThan
} else {
level = ob.bids.MaxPriceQueue()
iter = ob.bids.LessThan
}
for quantity.Sign() > 0 && level != nil {
levelVolume := level.Volume()
levelPrice := level.Price()
if quantity.GreaterThanOrEqual(levelVolume) {
price = price.Add(levelPrice.Mul(levelVolume))
quantity = quantity.Sub(levelVolume)
quant = quant.Add(levelVolume)
level = iter(levelPrice)
} else {
price = price.Add(levelPrice.Mul(quantity))
quant = quant.Add(quantity)
quantity = decimal.Zero
}
}
if quantity.Sign() > 0 {
err = ErrInsufficientQuantity
}
return
}
// String implements fmt.Stringer interface
func (ob *OrderBook) String() string {
return ob.asks.String() + "\r\n------------------------------------" + ob.bids.String()
}
// MarshalJSON implements json.Marshaler interface
func (ob *OrderBook) MarshalJSON() ([]byte, error) {
return json.Marshal(
&struct {
Asks *OrderSide `json:"asks"`
Bids *OrderSide `json:"bids"`
}{
Asks: ob.asks,
Bids: ob.bids,
},
)
}
// UnmarshalJSON implements json.Unmarshaler interface
func (ob *OrderBook) UnmarshalJSON(data []byte) error {
obj := struct {
Asks *OrderSide `json:"asks"`
Bids *OrderSide `json:"bids"`
}{}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
ob.asks = obj.Asks
ob.bids = obj.Bids
ob.orders = map[string]*list.Element{}
for _, order := range ob.asks.Orders() {
ob.orders[order.Value.(*Order).ID()] = order
}
for _, order := range ob.bids.Orders() {
ob.orders[order.Value.(*Order).ID()] = order
}
return nil
}