This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderbook_processor.go
192 lines (161 loc) · 4.57 KB
/
orderbook_processor.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
package dydx
import (
"fmt"
"strings"
"github.com/fardream/go-dydx/heap"
)
// OrderbookProcessor maintains the state of the order book
//
// For now the bids and asks are maintained as a heap and
// it is easy to return top of the book. There doesn't look to be a
// difficult way to implement this as list as always sorted.
type OrderbookProcessor struct {
Market string
Bids
Asks
dropData bool
Data []*OrderbookChannelResponse
}
// NewOrderbookProcessor creates a orderbook processor.
// - set `dropData` to true to drop the updates.
func NewOrderbookProcessor(market string, dropData bool) *OrderbookProcessor {
return &OrderbookProcessor{
Market: market,
dropData: dropData,
Bids: Bids{mappedBook: mappedBook{locations: make(map[string]int)}},
Asks: Asks{mappedBook: mappedBook{locations: make(map[string]int)}},
}
}
// Process a update from the orderbook
func (ob *OrderbookProcessor) Process(resp *OrderbookChannelResponse) {
if !ob.dropData {
ob.Data = append(ob.Data, resp)
}
contents := resp.Contents
if contents == nil {
return
}
if contents.Offset != nil {
ob.Bids.updateOffset(contents.Offset)
ob.Asks.updateOffset(contents.Offset)
}
ob.updateBook(contents.Bids, &ob.Bids)
ob.updateBook(contents.Asks, &ob.Asks)
}
// updateBook updates one side of the book (bids or asks)
func (ob *OrderbookProcessor) updateBook(updates []*OrderbookOrder, book singleSideOrderbook) {
for _, order := range updates {
if order == nil {
continue
}
updatePriceLevel(book, order)
}
}
// updatePriceLevel update one price level
func updatePriceLevel[T singleSideOrderbook](ob T, order *OrderbookOrder) {
index, orig_order, ok := ob.getPriceLevelIndex(order.PriceString)
if ok && !orig_order.IsOtherNewerOffset(order) {
return
}
switch {
case order.Size.IsZero() && ok:
heap.Remove[T, *OrderbookOrder](ob, index)
case !order.Size.IsZero() && ok:
ob.updatePriceLevelSize(order.PriceString, order.Size)
case !order.Size.IsZero() && !ok:
heap.Push(ob, order)
}
}
// BookTop returns the best bid and ask of the book. nil if the side of the book is empty.
func (ob *OrderbookProcessor) BookTop() (*OrderbookOrder, *OrderbookOrder) {
var bid *OrderbookOrder
if len(ob.Bids.Orders) > 0 {
bid = ob.Bids.Orders[0]
}
var ask *OrderbookOrder
if len(ob.Asks.Orders) > 0 {
ask = ob.Asks.Orders[0]
}
return bid, ask
}
// singleSideOrderbook describe bids or asks side.
// An interface is used to remove the check of the direction in the `Less` function.
type singleSideOrderbook interface {
// allow heap operations on this type
heap.Interface[*OrderbookOrder]
getPriceLevelIndex(priceStr string) (int, *OrderbookOrder, bool)
updatePriceLevelSize(priceStr string, newSize *Decimal)
}
var (
_ singleSideOrderbook = (*Bids)(nil)
_ singleSideOrderbook = (*Asks)(nil)
)
// Bids side of the book.
type Bids struct {
mappedBook
}
func (b *Bids) Less(i, j int) bool {
return b.Orders[i].Price.GreaterThan(b.Orders[j].Price)
}
// Asks side of the book.
type Asks struct {
mappedBook
}
func (a *Asks) Less(i, j int) bool {
return a.Orders[i].Price.LessThan(a.Orders[j].Price)
}
// mappedBook contains all the supporting functions for singleSideOrderbook without the less function.
type mappedBook struct {
Orders []*OrderbookOrder
locations map[string]int
}
func (m *mappedBook) updateOffset(offset *int64) {
for _, v := range m.Orders {
if v.Offset == nil {
v.Offset = offset
}
}
}
func (m *mappedBook) Len() int {
return len(m.Orders)
}
func (m *mappedBook) Swap(i, j int) {
m.locations[m.Orders[i].PriceString], m.locations[m.Orders[j].PriceString] = j, i
m.Orders[i], m.Orders[j] = m.Orders[j], m.Orders[i]
}
func (m *mappedBook) Pop() *OrderbookOrder {
if len(m.Orders) == 0 {
return nil
}
order := m.Orders[len(m.Orders)-1]
m.Orders = m.Orders[0 : len(m.Orders)-1]
delete(m.locations, order.PriceString)
return order
}
func (m *mappedBook) Push(order *OrderbookOrder) {
m.Orders = append(m.Orders, order)
m.locations[order.PriceString] = len(m.Orders) - 1
}
func (m *mappedBook) getPriceLevelIndex(priceStr string) (int, *OrderbookOrder, bool) {
r, ok := m.locations[priceStr]
if !ok {
return r, nil, false
}
if r >= len(m.Orders) {
return 0, nil, false
}
return r, m.Orders[r], true
}
func (m *mappedBook) updatePriceLevelSize(priceStr string, news_size *Decimal) {
r, ok := m.locations[priceStr]
if ok {
m.Orders[r].Size = news_size
}
}
func (m *mappedBook) PrintBook() string {
var b strings.Builder
for index, v := range m.Orders {
fmt.Fprintf(&b, "%d : %s @ $%s\n", index, v.Price.String(), v.Size.String())
}
return b.String()
}