-
Notifications
You must be signed in to change notification settings - Fork 3
/
match.go
397 lines (376 loc) · 12.5 KB
/
match.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package amm
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// PriceDirection specifies estimated price direction within this batch.
type PriceDirection int
const (
PriceStaying PriceDirection = iota + 1
PriceIncreasing
PriceDecreasing
)
func (dir PriceDirection) String() string {
switch dir {
case PriceStaying:
return "PriceStaying"
case PriceIncreasing:
return "PriceIncreasing"
case PriceDecreasing:
return "PriceDecreasing"
default:
return fmt.Sprintf("PriceDirection(%d)", dir)
}
}
// FillOrder fills the order by given amount and price.
func FillOrder(order Order, amt sdk.Int, price sdk.Dec) (quoteCoinDiff sdk.Int) {
matchableAmt := MatchableAmount(order, price)
if amt.GT(matchableAmt) {
panic(fmt.Errorf("cannot match more than open amount; %s > %s", amt, matchableAmt))
}
var paid, received sdk.Int
switch order.GetDirection() {
case Buy:
paid = price.MulInt(amt).Ceil().TruncateInt()
received = amt
quoteCoinDiff = paid
case Sell:
paid = amt
received = price.MulInt(amt).TruncateInt()
quoteCoinDiff = received.Neg()
}
order.SetPaidOfferCoinAmount(order.GetPaidOfferCoinAmount().Add(paid))
order.SetReceivedDemandCoinAmount(order.GetReceivedDemandCoinAmount().Add(received))
order.SetOpenAmount(order.GetOpenAmount().Sub(amt))
return
}
// FulfillOrder fills the order by its remaining open amount at given price.
func FulfillOrder(order Order, price sdk.Dec) (quoteCoinDiff sdk.Int) {
quoteCoinDiff = sdk.ZeroInt()
matchableAmt := MatchableAmount(order, price)
if matchableAmt.IsPositive() {
quoteCoinDiff = quoteCoinDiff.Add(FillOrder(order, matchableAmt, price))
}
return
}
// FulfillOrders fills multiple orders by their remaining open amount
// at given price.
func FulfillOrders(orders []Order, price sdk.Dec) (quoteCoinDiff sdk.Int) {
quoteCoinDiff = sdk.ZeroInt()
for _, order := range orders {
quoteCoinDiff = quoteCoinDiff.Add(FulfillOrder(order, price))
}
return
}
func FindMatchPrice(ov OrderView, tickPrec int) (matchPrice sdk.Dec, found bool) {
highestBuyPrice, found := ov.HighestBuyPrice()
if !found {
return sdk.Dec{}, false
}
lowestSellPrice, found := ov.LowestSellPrice()
if !found {
return sdk.Dec{}, false
}
if highestBuyPrice.LT(lowestSellPrice) {
return sdk.Dec{}, false
}
prec := TickPrecision(tickPrec)
lowestTickIdx := prec.TickToIndex(prec.LowestTick())
highestTickIdx := prec.TickToIndex(prec.HighestTick())
var i, j int
i, found = findFirstTrueCondition(lowestTickIdx, highestTickIdx, func(i int) bool {
sellAmt := ov.SellAmountUnder(prec.TickFromIndex(i), true)
return sellAmt.IsPositive() && ov.BuyAmountOver(prec.TickFromIndex(i+1), true).LTE(sellAmt)
})
if !found {
return sdk.Dec{}, false
}
j, found = findFirstTrueCondition(highestTickIdx, lowestTickIdx, func(i int) bool {
buyAmt := ov.BuyAmountOver(prec.TickFromIndex(i), true)
return buyAmt.IsPositive() && buyAmt.GTE(ov.SellAmountUnder(prec.TickFromIndex(i-1), true))
})
if !found {
return sdk.Dec{}, false
}
midTick := TickFromIndex(i, tickPrec).Add(TickFromIndex(j, tickPrec)).QuoInt64(2)
return RoundPrice(midTick, tickPrec), true
}
// FindMatchableAmountAtSinglePrice returns the largest matchable amount of orders
// when matching orders at single price(batch auction).
func (ob *OrderBook) FindMatchableAmountAtSinglePrice(matchPrice sdk.Dec) (matchableAmt sdk.Int, found bool) {
type Side struct {
ticks []*orderBookTick
totalMatchableAmt sdk.Int
i int
partialMatchAmt sdk.Int
}
buildSide := func(ticks []*orderBookTick, priceIncreasing bool) (side *Side) {
side = &Side{totalMatchableAmt: zeroInt}
for i, tick := range ticks {
if (priceIncreasing && tick.price.GT(matchPrice)) ||
(!priceIncreasing && tick.price.LT(matchPrice)) {
break
}
side.ticks = ticks[:i+1]
side.totalMatchableAmt = side.totalMatchableAmt.Add(TotalMatchableAmount(tick.orders, matchPrice))
}
side.i = len(side.ticks) - 1
return
}
buySide := buildSide(ob.buys.ticks, ob.buys.priceIncreasing)
if len(buySide.ticks) == 0 {
return sdk.Int{}, false
}
sellSide := buildSide(ob.sells.ticks, ob.sells.priceIncreasing)
if len(sellSide.ticks) == 0 {
return sdk.Int{}, false
}
sides := map[OrderDirection]*Side{
Buy: buySide,
Sell: sellSide,
}
// Repeatedly check both buy/sell side to see if there is an order to drop.
// If there is not, then the loop is finished.
for {
ok := true
for _, dir := range []OrderDirection{Buy, Sell} {
side := sides[dir]
i := side.i
tick := side.ticks[i]
tickAmt := TotalMatchableAmount(tick.orders, matchPrice)
// side.partialMatchAmt can be negative at this moment, but
// FindMatchableAmountAtSinglePrice won't return a negative amount because
// the if-block below would set ok = false if otherTicksAmt >= matchAmt
// and the loop would be continued.
matchableAmt = sdk.MinInt(buySide.totalMatchableAmt, sellSide.totalMatchableAmt)
otherTicksAmt := side.totalMatchableAmt.Sub(tickAmt)
side.partialMatchAmt = matchableAmt.Sub(otherTicksAmt)
if otherTicksAmt.GTE(matchableAmt) ||
(dir == Sell && matchPrice.MulInt(side.partialMatchAmt).TruncateInt().IsZero()) {
if i == 0 { // There's no orders left, which means orders are not matchable.
return sdk.Int{}, false
}
side.totalMatchableAmt = side.totalMatchableAmt.Sub(tickAmt)
side.i--
ok = false
}
}
if ok {
return matchableAmt, true
}
}
}
// MatchAtSinglePrice matches all matchable orders(buy orders with higher(or equal) price
// than the price and sell orders with lower(or equal) price than the price)
// at the price.
func (ob *OrderBook) MatchAtSinglePrice(matchPrice sdk.Dec) (quoteCoinDiff sdk.Int, matched bool) {
matchableAmt, found := ob.FindMatchableAmountAtSinglePrice(matchPrice)
if !found {
return sdk.Int{}, false
}
quoteCoinDiff = sdk.ZeroInt()
distributeToTicks := func(ticks []*orderBookTick) {
remainingAmt := matchableAmt
for _, tick := range ticks {
tickAmt := TotalMatchableAmount(tick.orders, matchPrice)
if tickAmt.LTE(remainingAmt) {
quoteCoinDiff = quoteCoinDiff.Add(FulfillOrders(tick.orders, matchPrice))
remainingAmt = remainingAmt.Sub(tickAmt)
if remainingAmt.IsZero() {
break
}
} else {
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToTick(tick, remainingAmt, matchPrice))
break
}
}
}
distributeToTicks(ob.buys.ticks)
distributeToTicks(ob.sells.ticks)
matched = true
return
}
// PriceDirection returns the estimated price direction within this batch
// considering the last price.
func (ob *OrderBook) PriceDirection(lastPrice sdk.Dec) PriceDirection {
// TODO: use OrderBookView
buyAmtOverLastPrice := sdk.ZeroInt()
buyAmtAtLastPrice := sdk.ZeroInt()
for _, tick := range ob.buys.ticks {
if tick.price.LT(lastPrice) {
break
}
amt := TotalMatchableAmount(tick.orders, lastPrice)
if tick.price.Equal(lastPrice) {
buyAmtAtLastPrice = amt
break
}
buyAmtOverLastPrice = buyAmtOverLastPrice.Add(amt)
}
sellAmtUnderLastPrice := sdk.ZeroInt()
sellAmtAtLastPrice := sdk.ZeroInt()
for _, tick := range ob.sells.ticks {
if tick.price.GT(lastPrice) {
break
}
amt := TotalMatchableAmount(tick.orders, lastPrice)
if tick.price.Equal(lastPrice) {
sellAmtAtLastPrice = amt
break
}
sellAmtUnderLastPrice = sellAmtUnderLastPrice.Add(amt)
}
switch {
case buyAmtOverLastPrice.GT(sellAmtAtLastPrice.Add(sellAmtUnderLastPrice)):
return PriceIncreasing
case sellAmtUnderLastPrice.GT(buyAmtAtLastPrice.Add(buyAmtOverLastPrice)):
return PriceDecreasing
default:
return PriceStaying
}
}
// Match matches orders sequentially, starting from buy orders with the highest price
// and sell orders with the lowest price.
// The matching continues until there's no more matchable orders.
func (ob *OrderBook) Match(lastPrice sdk.Dec) (matchPrice sdk.Dec, quoteCoinDiff sdk.Int, matched bool) {
if len(ob.buys.ticks) == 0 || len(ob.sells.ticks) == 0 {
return sdk.Dec{}, sdk.Int{}, false
}
matchPrice = lastPrice
dir := ob.PriceDirection(lastPrice)
quoteCoinDiff, matched = ob.MatchAtSinglePrice(lastPrice)
if dir == PriceStaying {
return matchPrice, quoteCoinDiff, matched
}
if !matched {
quoteCoinDiff = sdk.ZeroInt()
}
bi, si := 0, 0
for bi < len(ob.buys.ticks) && si < len(ob.sells.ticks) && ob.buys.ticks[bi].price.GTE(ob.sells.ticks[si].price) {
buyTick := ob.buys.ticks[bi]
sellTick := ob.sells.ticks[si]
var p sdk.Dec
switch dir {
case PriceIncreasing:
p = sellTick.price
case PriceDecreasing:
p = buyTick.price
}
buyTickOpenAmt := TotalMatchableAmount(buyTick.orders, p)
sellTickOpenAmt := TotalMatchableAmount(sellTick.orders, p)
if !buyTickOpenAmt.IsPositive() {
bi++
continue
}
if !sellTickOpenAmt.IsPositive() {
si++
continue
}
if buyTickOpenAmt.LTE(sellTickOpenAmt) {
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToTick(buyTick, buyTickOpenAmt, p))
bi++
} else {
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToTick(buyTick, sellTickOpenAmt, p))
}
if sellTickOpenAmt.LTE(buyTickOpenAmt) {
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToTick(sellTick, sellTickOpenAmt, p))
si++
} else {
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToTick(sellTick, buyTickOpenAmt, p))
}
matchPrice = p
matched = true
}
return
}
// DistributeOrderAmountToTick distributes the given order amount to the orders
// at the tick.
// Orders with higher priority(have lower batch id) get matched first,
// then the remaining amount is distributed to the remaining orders.
func DistributeOrderAmountToTick(tick *orderBookTick, amt sdk.Int, price sdk.Dec) (quoteCoinDiff sdk.Int) {
remainingAmt := amt
quoteCoinDiff = sdk.ZeroInt()
groups := GroupOrdersByBatchId(tick.orders)
for _, group := range groups {
openAmt := TotalMatchableAmount(group.Orders, price)
if openAmt.IsZero() {
continue
}
if remainingAmt.GTE(openAmt) {
quoteCoinDiff = quoteCoinDiff.Add(FulfillOrders(group.Orders, price))
remainingAmt = remainingAmt.Sub(openAmt)
} else {
SortOrders(group.Orders)
quoteCoinDiff = quoteCoinDiff.Add(DistributeOrderAmountToOrders(group.Orders, remainingAmt, price))
remainingAmt = sdk.ZeroInt()
}
if remainingAmt.IsZero() {
break
}
}
return
}
// DistributeOrderAmountToOrders distributes the given order amount to the orders
// proportional to each order's amount.
// The caller must sort orders before calling DistributeOrderAmountToOrders.
// After distributing the amount based on each order's proportion,
// remaining amount due to the decimal truncation is distributed
// to the orders again, by priority.
// This time, the proportion is not considered and each order takes up
// the amount as much as possible.
func DistributeOrderAmountToOrders(orders []Order, amt sdk.Int, price sdk.Dec) (quoteCoinDiff sdk.Int) {
totalAmt := TotalAmount(orders)
totalMatchedAmt := sdk.ZeroInt()
matchedAmtByOrder := map[Order]sdk.Int{}
for _, order := range orders {
matchableAmt := MatchableAmount(order, price)
if matchableAmt.IsZero() {
continue
}
orderAmt := order.GetAmount().ToDec()
proportion := orderAmt.QuoTruncate(totalAmt.ToDec())
matchedAmt := sdk.MinInt(matchableAmt, proportion.MulInt(amt).TruncateInt())
if matchedAmt.IsPositive() {
matchedAmtByOrder[order] = matchedAmt
totalMatchedAmt = totalMatchedAmt.Add(matchedAmt)
}
}
remainingAmt := amt.Sub(totalMatchedAmt)
for _, order := range orders {
if remainingAmt.IsZero() {
break
}
prevMatchedAmt, ok := matchedAmtByOrder[order]
if !ok { // TODO: is it possible?
prevMatchedAmt = sdk.ZeroInt()
}
matchableAmt := MatchableAmount(order, price)
matchedAmt := sdk.MinInt(remainingAmt, matchableAmt.Sub(prevMatchedAmt))
matchedAmtByOrder[order] = prevMatchedAmt.Add(matchedAmt)
remainingAmt = remainingAmt.Sub(matchedAmt)
}
var matchedOrders, notMatchedOrders []Order
for _, order := range orders {
matchedAmt, ok := matchedAmtByOrder[order]
if !ok {
matchedAmt = sdk.ZeroInt()
}
if !matchedAmt.IsZero() && (order.GetDirection() == Buy || price.MulInt(matchedAmt).TruncateInt().IsPositive()) {
matchedOrders = append(matchedOrders, order)
} else {
notMatchedOrders = append(notMatchedOrders, order)
}
}
if len(notMatchedOrders) > 0 {
if len(matchedOrders) == 0 {
return DistributeOrderAmountToOrders(orders[:len(orders)-1], amt, price)
} else {
return DistributeOrderAmountToOrders(matchedOrders, amt, price)
}
}
quoteCoinDiff = sdk.ZeroInt()
for order, matchedAmt := range matchedAmtByOrder {
quoteCoinDiff = quoteCoinDiff.Add(FillOrder(order, matchedAmt, price))
}
return
}