-
Notifications
You must be signed in to change notification settings - Fork 55
/
match.go
208 lines (180 loc) · 5.67 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type MatchEngine struct {
BuyOrderSource OrderSource
SellOrderSource OrderSource
TickPrecision int // price tick precision
}
func NewMatchEngine(buys, sells OrderSource, prec int) *MatchEngine {
return &MatchEngine{
BuyOrderSource: buys,
SellOrderSource: sells,
TickPrecision: prec,
}
}
func (engine *MatchEngine) Matchable() bool {
highestBuyPrice, found := engine.BuyOrderSource.HighestTick()
if !found {
return false
}
return engine.SellOrderSource.AmountLTE(highestBuyPrice).IsPositive()
}
func (engine *MatchEngine) EstimatedPriceDirection(lastPrice sdk.Dec) PriceDirection {
buyAmount := engine.BuyOrderSource.AmountGTE(lastPrice)
sellAmount := engine.SellOrderSource.AmountLTE(lastPrice)
if buyAmount.ToDec().GTE(lastPrice.MulInt(sellAmount)) {
return PriceIncreasing
}
return PriceDecreasing
}
// SwapPrice assumes that the last price is fit in tick.
func (engine *MatchEngine) SwapPrice(lastPrice sdk.Dec) sdk.Dec {
dir := engine.EstimatedPriceDirection(lastPrice)
tickSource := MergeOrderSources(engine.BuyOrderSource, engine.SellOrderSource) // temporary order source just for ticks
buysCache := map[int]sdk.Int{}
buyAmountGTE := func(i int) sdk.Int {
ba, ok := buysCache[i]
if !ok {
ba = engine.BuyOrderSource.AmountGTE(TickFromIndex(i, engine.TickPrecision))
buysCache[i] = ba
}
return ba
}
sellsCache := map[int]sdk.Int{}
sellAmountLTE := func(i int) sdk.Int {
sa, ok := sellsCache[i]
if !ok {
sa = engine.SellOrderSource.AmountLTE(TickFromIndex(i, engine.TickPrecision))
sellsCache[i] = sa
}
return sa
}
// TODO: lastPrice could be a price not fit in ticks.
currentPrice := lastPrice
for {
i := TickToIndex(currentPrice, engine.TickPrecision)
var nextPrice sdk.Dec
var found bool
switch dir {
case PriceIncreasing:
sa := sellAmountLTE(i)
hba := buyAmountGTE(i + 1)
ba := buyAmountGTE(i - 1)
lsa := sellAmountLTE(i - 2)
if currentPrice.MulInt(sa).GTE(hba.ToDec()) && ba.ToDec().GTE(TickFromIndex(i-1, engine.TickPrecision).MulInt(lsa)) {
return currentPrice
}
if buyAmountGTE(i + 1).IsZero() {
return currentPrice
}
nextPrice, found = tickSource.UpTick(currentPrice)
case PriceDecreasing:
sa := sellAmountLTE(i + 1)
hba := buyAmountGTE(i + 2)
ba := buyAmountGTE(i)
lsa := sellAmountLTE(i - 1)
if TickFromIndex(i+1, engine.TickPrecision).MulInt(sa).GTE(hba.ToDec()) && ba.ToDec().GTE(currentPrice.MulInt(lsa)) {
return currentPrice
}
if sellAmountLTE(i - 1).IsZero() {
return currentPrice
}
nextPrice, found = tickSource.DownTick(currentPrice)
}
if !found {
return currentPrice
}
currentPrice = nextPrice
}
}
func (engine *MatchEngine) Match(lastPrice sdk.Dec) (orderBook *OrderBook, swapPrice sdk.Dec, matched bool) {
if !engine.Matchable() {
return
}
swapPrice = engine.SwapPrice(lastPrice)
buyPrice, _ := engine.BuyOrderSource.HighestTick()
sellPrice, _ := engine.SellOrderSource.LowestTick()
orderBook = NewOrderBook(engine.TickPrecision)
for {
if buyPrice.LT(swapPrice) || sellPrice.GT(swapPrice) {
break
}
buyOrders := orderBook.BuyTicks.Orders(buyPrice)
if len(buyOrders) == 0 {
orderBook.AddOrders(engine.BuyOrderSource.Orders(buyPrice)...)
buyOrders = orderBook.BuyTicks.Orders(buyPrice)
}
sellOrders := orderBook.SellTicks.Orders(sellPrice)
if len(sellOrders) == 0 {
orderBook.AddOrders(engine.SellOrderSource.Orders(sellPrice)...)
sellOrders = orderBook.SellTicks.Orders(sellPrice)
}
MatchOrders(buyOrders, sellOrders, swapPrice)
matched = true
if buyPrice.Equal(swapPrice) && sellPrice.Equal(swapPrice) {
break
}
if buyOrders.RemainingAmount().IsZero() {
var found bool
buyPrice, found = engine.BuyOrderSource.DownTick(buyPrice)
if !found {
break
}
}
if sellOrders.RemainingAmount().IsZero() {
var found bool
sellPrice, found = engine.SellOrderSource.UpTick(sellPrice)
if !found {
break
}
}
}
return
}
// MatchOrders matches two order groups at given price.
func MatchOrders(buyOrders, sellOrders Orders, price sdk.Dec) {
buyAmount := buyOrders.RemainingAmount()
sellAmount := sellOrders.RemainingAmount()
if buyAmount.IsZero() || sellAmount.IsZero() {
return
}
matchAll := false
sellDemandAmount := price.MulInt(sellAmount).TruncateInt()
if buyAmount.Equal(sellDemandAmount) {
matchAll = true
}
buyDemandAmount := buyAmount.ToDec().QuoTruncate(price).TruncateInt()
var smallerOrders, biggerOrders Orders
var smallerAmount, biggerAmount sdk.Int
var smallerDemandAmount sdk.Int
if buyAmount.LTE(sellDemandAmount) { // Note that we use LTE here.
smallerOrders, biggerOrders = buyOrders, sellOrders
smallerAmount, biggerAmount = buyAmount, sellAmount
smallerDemandAmount = buyDemandAmount
} else {
smallerOrders, biggerOrders = sellOrders, buyOrders
smallerAmount, biggerAmount = sellAmount, buyAmount
smallerDemandAmount = sellDemandAmount
}
for _, order := range smallerOrders {
proportion := order.GetRemainingAmount().ToDec().QuoInt(smallerAmount)
order.SetRemainingAmount(sdk.ZeroInt())
in := proportion.MulInt(smallerDemandAmount).TruncateInt()
order.SetReceivedAmount(order.GetReceivedAmount().Add(in))
order.SetMatched(true)
}
for _, order := range biggerOrders {
proportion := order.GetRemainingAmount().ToDec().QuoInt(biggerAmount)
if matchAll {
order.SetRemainingAmount(sdk.ZeroInt())
} else {
out := proportion.MulInt(smallerDemandAmount).TruncateInt()
order.SetRemainingAmount(order.GetRemainingAmount().Sub(out))
}
in := proportion.MulInt(smallerAmount).TruncateInt()
order.SetReceivedAmount(order.GetReceivedAmount().Add(in))
order.SetMatched(true)
}
}