-
Notifications
You must be signed in to change notification settings - Fork 790
/
orderbook.go
351 lines (318 loc) · 9.39 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
351
package orderbook
import (
"fmt"
"sort"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/log"
)
// Get checks and returns the orderbook given an exchange name and currency pair
func Get(exchange string, p currency.Pair, a asset.Item) (*Base, error) {
return service.Retrieve(exchange, p, a)
}
// GetDepth returns a Depth pointer allowing the caller to stream orderbook changes
func GetDepth(exchange string, p currency.Pair, a asset.Item) (*Depth, error) {
return service.GetDepth(exchange, p, a)
}
// DeployDepth sets a depth struct and returns a depth pointer. This allows for
// the loading of a new orderbook snapshot and incremental updates via the
// streaming package.
func DeployDepth(exchange string, p currency.Pair, a asset.Item) (*Depth, error) {
return service.DeployDepth(exchange, p, a)
}
// SubscribeToExchangeOrderbooks returns a pipe to an exchange feed
func SubscribeToExchangeOrderbooks(exchange string) (dispatch.Pipe, error) {
service.mu.Lock()
defer service.mu.Unlock()
exch, ok := service.books[strings.ToLower(exchange)]
if !ok {
return dispatch.Pipe{}, fmt.Errorf("%w for %s exchange",
errCannotFindOrderbook, exchange)
}
return service.Mux.Subscribe(exch.ID)
}
// Update stores orderbook data
func (s *Service) Update(b *Base) error {
name := strings.ToLower(b.Exchange)
mapKey := key.PairAsset{
Base: b.Pair.Base.Item,
Quote: b.Pair.Quote.Item,
Asset: b.Asset,
}
s.mu.Lock()
m1, ok := s.books[name]
if !ok {
id, err := s.Mux.GetID()
if err != nil {
s.mu.Unlock()
return err
}
m1 = Exchange{
m: make(map[key.PairAsset]*Depth),
ID: id,
}
s.books[name] = m1
}
book, ok := m1.m[mapKey]
if !ok {
book = NewDepth(m1.ID)
book.AssignOptions(b)
m1.m[mapKey] = book
}
err := book.LoadSnapshot(b.Bids, b.Asks, b.LastUpdateID, b.LastUpdated, true)
s.mu.Unlock()
if err != nil {
return err
}
return s.Mux.Publish(book, m1.ID)
}
// DeployDepth used for subsystem deployment creates a depth item in the struct
// then returns a ptr to that Depth item
func (s *Service) DeployDepth(exchange string, p currency.Pair, a asset.Item) (*Depth, error) {
if exchange == "" {
return nil, errExchangeNameUnset
}
if p.IsEmpty() {
return nil, errPairNotSet
}
if !a.IsValid() {
return nil, errAssetTypeNotSet
}
mapKey := key.PairAsset{
Base: p.Base.Item,
Quote: p.Quote.Item,
Asset: a,
}
s.mu.Lock()
defer s.mu.Unlock()
m1, ok := s.books[strings.ToLower(exchange)]
if !ok {
id, err := s.Mux.GetID()
if err != nil {
return nil, err
}
m1 = Exchange{
m: make(map[key.PairAsset]*Depth),
ID: id,
}
s.books[strings.ToLower(exchange)] = m1
}
book, ok := m1.m[mapKey]
if !ok {
book = NewDepth(m1.ID)
book.exchange = exchange
book.pair = p
book.asset = a
m1.m[mapKey] = book
}
return book, nil
}
// GetDepth returns the actual depth struct for potential subsystems and
// strategies to interact with
func (s *Service) GetDepth(exchange string, p currency.Pair, a asset.Item) (*Depth, error) {
s.mu.Lock()
defer s.mu.Unlock()
m1, ok := s.books[strings.ToLower(exchange)]
if !ok {
return nil, fmt.Errorf("%w for %s exchange",
errCannotFindOrderbook, exchange)
}
book, ok := m1.m[key.PairAsset{
Base: p.Base.Item,
Quote: p.Quote.Item,
Asset: a,
}]
if !ok {
return nil, fmt.Errorf("%w associated with base currency %s",
errCannotFindOrderbook,
p.Quote)
}
return book, nil
}
// Retrieve gets orderbook depth data from the stored tranches and returns the
// base equivalent copy
func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Base, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if !a.IsValid() {
return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, a)
}
s.mu.Lock()
defer s.mu.Unlock()
m1, ok := s.books[strings.ToLower(exchange)]
if !ok {
return nil, fmt.Errorf("%w for %s exchange",
errCannotFindOrderbook,
exchange)
}
book, ok := m1.m[key.PairAsset{
Base: p.Base.Item,
Quote: p.Quote.Item,
Asset: a,
}]
if !ok {
return nil, fmt.Errorf("%w associated with base currency %s",
errCannotFindOrderbook,
p.Quote)
}
return book.Retrieve()
}
// GetDepth returns the concrete book allowing the caller to stream orderbook changes
func (b *Base) GetDepth() (*Depth, error) {
return service.GetDepth(b.Exchange, b.Pair, b.Asset)
}
// TotalBidsAmount returns the total amount of bids and the total orderbook
// bids value
func (b *Base) TotalBidsAmount() (amountCollated, total float64) {
for x := range b.Bids {
amountCollated += b.Bids[x].Amount
total += b.Bids[x].Amount * b.Bids[x].Price
}
return amountCollated, total
}
// TotalAsksAmount returns the total amount of asks and the total orderbook
// asks value
func (b *Base) TotalAsksAmount() (amountCollated, total float64) {
for y := range b.Asks {
amountCollated += b.Asks[y].Amount
total += b.Asks[y].Amount * b.Asks[y].Price
}
return amountCollated, total
}
// Verify ensures that the orderbook items are correctly sorted prior to being
// set and will reject any book with incorrect values.
// Bids should always go from a high price to a low price and
// Asks should always go from a low price to a higher price
func (b *Base) Verify() error {
if !b.VerifyOrderbook {
return nil
}
// Checking for both ask and bid lengths being zero has been removed and
// a warning has been put in place for some exchanges that return zero
// level books. In the event that there is a massive liquidity change where
// a book dries up, this will still update so we do not traverse potential
// incorrect old data.
if len(b.Asks) == 0 || len(b.Bids) == 0 {
log.Warnf(log.OrderBook,
bookLengthIssue,
b.Exchange,
b.Pair,
b.Asset,
len(b.Bids),
len(b.Asks))
}
err := checkAlignment(b.Bids, b.IsFundingRate, b.PriceDuplication, b.IDAlignment, b.ChecksumStringRequired, dsc, b.Exchange)
if err != nil {
return fmt.Errorf(bidLoadBookFailure, b.Exchange, b.Pair, b.Asset, err)
}
err = checkAlignment(b.Asks, b.IsFundingRate, b.PriceDuplication, b.IDAlignment, b.ChecksumStringRequired, asc, b.Exchange)
if err != nil {
return fmt.Errorf(askLoadBookFailure, b.Exchange, b.Pair, b.Asset, err)
}
return nil
}
// checker defines specific functionality to determine ascending/descending
// validation
type checker func(current Tranche, previous Tranche) error
// asc specifically defines ascending price check
var asc = func(current Tranche, previous Tranche) error {
if current.Price < previous.Price {
return errPriceOutOfOrder
}
return nil
}
// dsc specifically defines descending price check
var dsc = func(current Tranche, previous Tranche) error {
if current.Price > previous.Price {
return errPriceOutOfOrder
}
return nil
}
// checkAlignment validates full orderbook
func checkAlignment(depth Tranches, fundingRate, priceDuplication, isIDAligned, requiresChecksumString bool, c checker, exch string) error {
for i := range depth {
if depth[i].Price == 0 {
switch {
case exch == "Bitfinex" && fundingRate: /* funding rate can be 0 it seems on Bitfinex */
default:
return errPriceNotSet
}
}
if depth[i].Amount <= 0 {
return errAmountInvalid
}
if fundingRate && depth[i].Period == 0 {
return errPeriodUnset
}
if requiresChecksumString && (depth[i].StrAmount == "" || depth[i].StrPrice == "") {
return errChecksumStringNotSet
}
if i != 0 {
prev := i - 1
if err := c(depth[i], depth[prev]); err != nil {
return err
}
if isIDAligned && depth[i].ID < depth[prev].ID {
return errIDOutOfOrder
}
if !priceDuplication && depth[i].Price == depth[prev].Price {
return errDuplication
}
if depth[i].ID != 0 && depth[i].ID == depth[prev].ID {
return errIDDuplication
}
}
}
return nil
}
// Process processes incoming orderbooks, creating or updating the orderbook
// list
func (b *Base) Process() error {
if b.Exchange == "" {
return errExchangeNameUnset
}
if b.Pair.IsEmpty() {
return errPairNotSet
}
if b.Asset.String() == "" {
return errAssetTypeNotSet
}
if b.LastUpdated.IsZero() {
b.LastUpdated = time.Now()
}
if err := b.Verify(); err != nil {
return err
}
return service.Update(b)
}
// Reverse reverses the order of orderbook items; some bid/asks are
// returned in either ascending or descending order. One bid or ask slice
// depending on what's received can be reversed. This is usually faster than
// using a sort algorithm as the algorithm could be impeded by a worst case time
// complexity when elements are shifted as opposed to just swapping element
// values.
func (ts *Tranches) Reverse() {
eLen := len(*ts)
var target int
for i := eLen/2 - 1; i >= 0; i-- {
target = eLen - 1 - i
(*ts)[i], (*ts)[target] = (*ts)[target], (*ts)[i]
}
}
// SortAsks sorts ask items to the correct ascending order if pricing values are
// scattered. If order from exchange is descending consider using the Reverse
// function.
func (ts Tranches) SortAsks() {
sort.Slice(ts, func(i, j int) bool { return ts[i].Price < ts[j].Price })
}
// SortBids sorts bid items to the correct descending order if pricing values
// are scattered. If order from exchange is ascending consider using the Reverse
// function.
func (ts Tranches) SortBids() {
sort.Slice(ts, func(i, j int) bool { return ts[i].Price > ts[j].Price })
}