-
Notifications
You must be signed in to change notification settings - Fork 55
/
orderbook.go
232 lines (208 loc) 路 5.99 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
package amm
import (
"fmt"
"sort"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// OrderBook is an order book.
type OrderBook struct {
buys, sells *orderBookTicks
}
// NewOrderBook returns a new OrderBook.
func NewOrderBook(orders ...Order) *OrderBook {
ob := &OrderBook{
buys: newOrderBookBuyTicks(),
sells: newOrderBookSellTicks(),
}
ob.AddOrder(orders...)
return ob
}
// AddOrder adds orders to the order book.
func (ob *OrderBook) AddOrder(orders ...Order) {
for _, order := range orders {
if MatchableAmount(order, order.GetPrice()).IsPositive() {
switch order.GetDirection() {
case Buy:
ob.buys.addOrder(order)
case Sell:
ob.sells.addOrder(order)
}
}
}
}
// Orders returns all orders in the order book.
func (ob *OrderBook) Orders() []Order {
var orders []Order
for _, tick := range append(ob.buys.ticks, ob.sells.ticks...) {
orders = append(orders, tick.orders...)
}
return orders
}
// BuyOrdersAt returns buy orders at given price in the order book.
// Note that the orders are not sorted.
func (ob *OrderBook) BuyOrdersAt(price sdk.Dec) []Order {
return ob.buys.ordersAt(price)
}
// SellOrdersAt returns sell orders at given price in the order book.
// Note that the orders are not sorted.
func (ob *OrderBook) SellOrdersAt(price sdk.Dec) []Order {
return ob.sells.ordersAt(price)
}
func (ob *OrderBook) HighestPrice() (sdk.Dec, bool) {
highestBuyPrice, _, foundBuy := ob.buys.highestPrice()
highestSellPrice, _, foundSell := ob.sells.highestPrice()
switch {
case foundBuy && foundSell:
return sdk.MaxDec(highestBuyPrice, highestSellPrice), true
case foundBuy:
return highestBuyPrice, true
case foundSell:
return highestSellPrice, true
default:
return sdk.Dec{}, false
}
}
func (ob *OrderBook) LowestPrice() (sdk.Dec, bool) {
lowestBuyPrice, _, foundBuy := ob.buys.lowestPrice()
lowestSellPrice, _, foundSell := ob.sells.lowestPrice()
switch {
case foundBuy && foundSell:
return sdk.MinDec(lowestBuyPrice, lowestSellPrice), true
case foundBuy:
return lowestBuyPrice, true
case foundSell:
return lowestSellPrice, true
default:
return sdk.Dec{}, false
}
}
func (ob *OrderBook) stringRepresentation(prices []sdk.Dec) string {
if len(prices) == 0 {
return "<nil>"
}
sort.Slice(prices, func(i, j int) bool {
return prices[i].GT(prices[j])
})
var b strings.Builder
b.WriteString("+--------sell--------+------------price-------------+--------buy---------+\n")
for _, price := range prices {
buyAmt := TotalMatchableAmount(ob.BuyOrdersAt(price), price)
sellAmt := TotalMatchableAmount(ob.SellOrdersAt(price), price)
_, _ = fmt.Fprintf(&b, "| %18s | %28s | %-18s |\n", sellAmt, price.String(), buyAmt)
}
b.WriteString("+--------------------+------------------------------+--------------------+")
return b.String()
}
// FullString returns a full string representation of the order book.
// FullString includes all possible price ticks from the order book's
// highest price to the lowest price.
func (ob *OrderBook) FullString(tickPrec int) string {
var prices []sdk.Dec
highest, found := ob.HighestPrice()
if !found {
return "<nil>"
}
lowest, _ := ob.LowestPrice()
for ; lowest.LTE(highest); lowest = UpTick(lowest, tickPrec) {
prices = append(prices, lowest)
}
return ob.stringRepresentation(prices)
}
// String returns a compact string representation of the order book.
// String includes a tick only when there is at least one order on it.
func (ob *OrderBook) String() string {
priceSet := map[string]sdk.Dec{}
for _, tick := range append(ob.buys.ticks, ob.sells.ticks...) {
priceSet[tick.price.String()] = tick.price
}
prices := make([]sdk.Dec, 0, len(priceSet))
for _, price := range priceSet {
prices = append(prices, price)
}
return ob.stringRepresentation(prices)
}
// orderBookTicks represents a list of orderBookTick.
// This type is used for both buy/sell sides of OrderBook.
type orderBookTicks struct {
ticks []*orderBookTick
priceIncreasing bool
}
func newOrderBookBuyTicks() *orderBookTicks {
return &orderBookTicks{
priceIncreasing: false,
}
}
func newOrderBookSellTicks() *orderBookTicks {
return &orderBookTicks{
priceIncreasing: true,
}
}
func (ticks *orderBookTicks) findPrice(price sdk.Dec) (i int, exact bool) {
i = sort.Search(len(ticks.ticks), func(i int) bool {
if ticks.priceIncreasing {
return ticks.ticks[i].price.GTE(price)
} else {
return ticks.ticks[i].price.LTE(price)
}
})
if i < len(ticks.ticks) && ticks.ticks[i].price.Equal(price) {
exact = true
}
return
}
func (ticks *orderBookTicks) addOrder(order Order) {
i, exact := ticks.findPrice(order.GetPrice())
if exact {
ticks.ticks[i].addOrder(order)
} else {
if i < len(ticks.ticks) {
// Insert a new order book tick at index i.
ticks.ticks = append(ticks.ticks[:i], append([]*orderBookTick{newOrderBookTick(order)}, ticks.ticks[i:]...)...)
} else {
// Append a new order book tick at the end.
ticks.ticks = append(ticks.ticks, newOrderBookTick(order))
}
}
}
func (ticks *orderBookTicks) ordersAt(price sdk.Dec) []Order {
i, exact := ticks.findPrice(price)
if !exact {
return nil
}
return ticks.ticks[i].orders
}
func (ticks *orderBookTicks) highestPrice() (sdk.Dec, int, bool) {
if len(ticks.ticks) == 0 {
return sdk.Dec{}, 0, false
}
if ticks.priceIncreasing {
return ticks.ticks[len(ticks.ticks)-1].price, len(ticks.ticks) - 1, true
} else {
return ticks.ticks[0].price, 0, true
}
}
func (ticks *orderBookTicks) lowestPrice() (sdk.Dec, int, bool) {
if len(ticks.ticks) == 0 {
return sdk.Dec{}, 0, false
}
if ticks.priceIncreasing {
return ticks.ticks[0].price, 0, true
} else {
return ticks.ticks[len(ticks.ticks)-1].price, len(ticks.ticks) - 1, true
}
}
// orderBookTick represents a tick in OrderBook.
type orderBookTick struct {
price sdk.Dec
orders []Order
}
func newOrderBookTick(order Order) *orderBookTick {
return &orderBookTick{
price: order.GetPrice(),
orders: []Order{order},
}
}
func (tick *orderBookTick) addOrder(order Order) {
tick.orders = append(tick.orders, order)
}