forked from bitfinexcom/bitfinex-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.go
207 lines (171 loc) · 5.18 KB
/
book.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
package book
import (
"encoding/json"
"errors"
"fmt"
"math"
"github.com/trever-io/bitfinex-api-go/pkg/convert"
"github.com/trever-io/bitfinex-api-go/pkg/models/common"
)
// BookAction represents a new/update or removal for a book entry.
type BookAction byte
const (
//BookEntry represents a new or updated book entry.
BookEntry BookAction = 0
//BookRemoveEntry represents a removal of a book entry.
BookRemoveEntry BookAction = 1
)
// Book represents an order book price update.
type Book struct {
Symbol string // book symbol
ID int64 // the book update ID, optional
Count int64 // updated count, optional
Period int64
Price float64 // updated price
Amount float64 // updated amount
Rate float64
PriceJsNum json.Number // update price as json.Number
AmountJsNum json.Number // update amount as json.Number
Side common.OrderSide // side
Action BookAction // action (add/remove)
}
type Snapshot struct {
Snapshot []*Book
}
func SnapshotFromRaw(symbol, precision string, raw [][]interface{}, rawNumbers interface{}) (*Snapshot, error) {
if len(raw) <= 0 {
return nil, fmt.Errorf("data slice too short for book snapshot: %#v", raw)
}
snap := make([]*Book, len(raw))
for i, v := range raw {
b, err := FromRaw(symbol, precision, v, rawNumbers.([]interface{})[i])
if err != nil {
return nil, err
}
snap[i] = b
}
return &Snapshot{Snapshot: snap}, nil
}
func IsRawBook(precision string) bool {
return precision == "R0"
}
// FromRaw creates a new book object from raw data. Precision determines how
// to interpret the side (baked into Count versus Amount)
// raw book updates [ID, price, qty], aggregated book updates [price, amount, count]
func FromRaw(symbol, precision string, raw []interface{}, rawNumbers interface{}) (b *Book, err error) {
if len(raw) < 3 {
return b, fmt.Errorf("raw slice too short for book, expected %d got %d: %#v", 3, len(raw), raw)
}
rawBook := IsRawBook(precision)
if len(raw) == 3 && rawBook {
b = rawTradingPairsBook(raw, rawNumbers)
}
if len(raw) == 3 && !rawBook {
b = tradingPairsBook(raw, rawNumbers)
}
if len(raw) >= 4 && rawBook {
b = rawFundingPairsBook(raw, rawNumbers)
}
if len(raw) >= 4 && !rawBook {
b = fundingPairsBook(raw, rawNumbers)
}
b.Symbol = symbol
return
}
// FromWSRaw - based on condition will return snapshot of books or single book
func FromWSRaw(symbol, precision string, data []interface{}) (interface{}, error) {
if len(data) == 0 {
return nil, errors.New("empty data slice")
}
_, isSnapshot := data[0].([]interface{})
if isSnapshot {
return SnapshotFromRaw(symbol, precision, convert.ToInterfaceArray(data), data)
}
return FromRaw(symbol, precision, data, data)
}
func rawTradingPairsBook(raw []interface{}, rawNumbers interface{}) *Book {
// [ ORDER_ID, PRICE, AMOUNT ] - raw trading pairs signature
var (
side common.OrderSide
action BookAction
)
rawNumSlice := rawNumbers.([]interface{})
price := convert.F64ValOrZero(raw[1])
amount := convert.F64ValOrZero(raw[2])
if amount > 0 {
side = common.Bid
} else {
side = common.Ask
}
if price <= 0 {
action = BookRemoveEntry
} else {
action = BookEntry
}
return &Book{
Price: math.Abs(price),
PriceJsNum: convert.FloatToJsonNumber(rawNumSlice[1]),
Amount: math.Abs(amount),
AmountJsNum: convert.FloatToJsonNumber(rawNumSlice[2]),
Side: side,
Action: action,
ID: convert.I64ValOrZero(raw[0]),
}
}
func tradingPairsBook(raw []interface{}, rawNumbers interface{}) *Book {
// [ PRICE, COUNT, AMOUNT ] - trading pairs signature
var (
price float64
count int64
priceNum json.Number
side common.OrderSide
action BookAction
)
rawNumSlice := rawNumbers.([]interface{})
amount := convert.F64ValOrZero(raw[2])
amountNum := convert.FloatToJsonNumber(rawNumSlice[2])
price = convert.F64ValOrZero(raw[0])
priceNum = convert.FloatToJsonNumber(rawNumSlice[0])
count = convert.I64ValOrZero(raw[1])
if amount > 0 {
side = common.Bid
} else {
side = common.Ask
}
if count <= 0 {
action = BookRemoveEntry
} else {
action = BookEntry
}
return &Book{
Price: math.Abs(price),
PriceJsNum: priceNum,
Count: count,
Amount: math.Abs(amount),
AmountJsNum: amountNum,
Side: side,
Action: action,
}
}
func rawFundingPairsBook(raw []interface{}, rawNumbers interface{}) *Book {
// [ ORDER_ID, PERIOD, RATE, AMOUNT ] - raw funding pairs signature
rawNumSlice := rawNumbers.([]interface{})
return &Book{
ID: convert.I64ValOrZero(raw[0]),
Period: convert.I64ValOrZero(raw[1]),
Rate: convert.F64ValOrZero(raw[2]),
Amount: convert.F64ValOrZero(raw[3]),
AmountJsNum: convert.FloatToJsonNumber(rawNumSlice[3]),
}
}
func fundingPairsBook(raw []interface{}, rawNumbers interface{}) *Book {
// [ RATE, PERIOD, COUNT, AMOUNT ], - funding pairs signature
rawNumSlice := rawNumbers.([]interface{})
return &Book{
Rate: convert.F64ValOrZero(raw[0]),
Period: convert.I64ValOrZero(raw[1]),
Count: convert.I64ValOrZero(raw[2]),
Amount: convert.F64ValOrZero(raw[3]),
AmountJsNum: convert.FloatToJsonNumber(rawNumSlice[3]),
}
}