-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
sliceorderbook.go
201 lines (159 loc) · 3.7 KB
/
sliceorderbook.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
package types
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
// SliceOrderBook is a general order book structure which could be used
// for RESTful responses and websocket stream parsing
//go:generate callbackgen -type SliceOrderBook
type SliceOrderBook struct {
Symbol string
Bids PriceVolumeSlice
Asks PriceVolumeSlice
lastUpdateTime time.Time
loadCallbacks []func(book *SliceOrderBook)
updateCallbacks []func(book *SliceOrderBook)
}
func NewSliceOrderBook(symbol string) *SliceOrderBook {
return &SliceOrderBook{
Symbol: symbol,
}
}
func (b *SliceOrderBook) LastUpdateTime() time.Time {
return b.lastUpdateTime
}
func (b *SliceOrderBook) Spread() (fixedpoint.Value, bool) {
bestBid, ok := b.BestBid()
if !ok {
return fixedpoint.Zero, false
}
bestAsk, ok := b.BestAsk()
if !ok {
return fixedpoint.Zero, false
}
return bestAsk.Price.Sub(bestBid.Price), true
}
func (b *SliceOrderBook) BestBid() (PriceVolume, bool) {
if len(b.Bids) == 0 {
return PriceVolume{}, false
}
return b.Bids[0], true
}
func (b *SliceOrderBook) BestAsk() (PriceVolume, bool) {
if len(b.Asks) == 0 {
return PriceVolume{}, false
}
return b.Asks[0], true
}
func (b *SliceOrderBook) SideBook(sideType SideType) PriceVolumeSlice {
switch sideType {
case SideTypeBuy:
return b.Bids
case SideTypeSell:
return b.Asks
default:
return nil
}
}
func (b *SliceOrderBook) IsValid() (bool, error) {
bid, hasBid := b.BestBid()
ask, hasAsk := b.BestAsk()
if !hasBid {
return false, errors.New("empty bids")
}
if !hasAsk {
return false, errors.New("empty asks")
}
if bid.Price.Compare(ask.Price) > 0 {
return false, fmt.Errorf("bid price %s > ask price %s", bid.Price.String(), ask.Price.String())
}
return true, nil
}
func (b *SliceOrderBook) PriceVolumesBySide(side SideType) PriceVolumeSlice {
switch side {
case SideTypeBuy:
return b.Bids.Copy()
case SideTypeSell:
return b.Asks.Copy()
}
return nil
}
func (b *SliceOrderBook) updateAsks(pvs PriceVolumeSlice) {
for _, pv := range pvs {
if pv.Volume.IsZero() {
b.Asks = b.Asks.Remove(pv.Price, false)
} else {
b.Asks = b.Asks.Upsert(pv, false)
}
}
}
func (b *SliceOrderBook) updateBids(pvs PriceVolumeSlice) {
for _, pv := range pvs {
if pv.Volume.IsZero() {
b.Bids = b.Bids.Remove(pv.Price, true)
} else {
b.Bids = b.Bids.Upsert(pv, true)
}
}
}
func (b *SliceOrderBook) update(book SliceOrderBook) {
b.updateBids(book.Bids)
b.updateAsks(book.Asks)
b.lastUpdateTime = time.Now()
}
func (b *SliceOrderBook) Reset() {
b.Bids = nil
b.Asks = nil
}
func (b *SliceOrderBook) Load(book SliceOrderBook) {
b.Reset()
b.update(book)
b.EmitLoad(b)
}
func (b *SliceOrderBook) Update(book SliceOrderBook) {
b.update(book)
b.EmitUpdate(b)
}
func (b *SliceOrderBook) Print() {
fmt.Print(b.String())
}
func (b *SliceOrderBook) String() string {
sb := strings.Builder{}
sb.WriteString("BOOK ")
sb.WriteString(b.Symbol)
sb.WriteString("\n")
if len(b.Asks) > 0 {
sb.WriteString("ASKS:\n")
for i := len(b.Asks) - 1; i >= 0; i-- {
sb.WriteString("- ASK: ")
sb.WriteString(b.Asks[i].String())
sb.WriteString("\n")
}
}
if len(b.Bids) > 0 {
sb.WriteString("BIDS:\n")
for _, bid := range b.Bids {
sb.WriteString("- BID: ")
sb.WriteString(bid.String())
sb.WriteString("\n")
}
}
return sb.String()
}
func (b *SliceOrderBook) CopyDepth(limit int) OrderBook {
var book SliceOrderBook
book.Symbol = b.Symbol
book.Bids = b.Bids.CopyDepth(limit)
book.Asks = b.Asks.CopyDepth(limit)
return &book
}
func (b *SliceOrderBook) Copy() OrderBook {
var book SliceOrderBook
book.Symbol = b.Symbol
book.Bids = b.Bids.Copy()
book.Asks = b.Asks.Copy()
return &book
}