-
Notifications
You must be signed in to change notification settings - Fork 3
/
orderbook.go
412 lines (352 loc) · 10.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package model
import (
"fmt"
"github.com/stellar/kelp/support/utils"
)
// OrderAction is the action of buy / sell
type OrderAction bool
// OrderActionBuy and OrderActionSell are the two actions
const (
OrderActionBuy OrderAction = false
OrderActionSell OrderAction = true
)
// minQuoteVolumePrecision allows for having precise enough minQuoteVolume values
const minQuoteVolumePrecision = 10
const nilString = "<nil>"
// IsBuy returns true for buy actions
func (a OrderAction) IsBuy() bool {
return a == OrderActionBuy
}
// IsSell returns true for sell actions
func (a OrderAction) IsSell() bool {
return a == OrderActionSell
}
// Reverse returns the opposite action
func (a OrderAction) Reverse() OrderAction {
if a.IsSell() {
return OrderActionBuy
}
return OrderActionSell
}
// String is the stringer function
func (a OrderAction) String() string {
if a == OrderActionBuy {
return "buy"
} else if a == OrderActionSell {
return "sell"
}
return "error, unrecognized order action"
}
var orderActionMap = map[string]OrderAction{
"buy": OrderActionBuy,
"sell": OrderActionSell,
}
// OrderActionFromString is a convenience to convert from common strings to the corresponding OrderAction
func OrderActionFromString(s string) OrderAction {
return orderActionMap[s]
}
// OrderType represents a type of an order, example market, limit, etc.
type OrderType int8
// These are the available order types
const (
OrderTypeMarket OrderType = 0
OrderTypeLimit OrderType = 1
)
// IsMarket returns true for market orders
func (o OrderType) IsMarket() bool {
return o == OrderTypeMarket
}
// IsLimit returns true for limit orders
func (o OrderType) IsLimit() bool {
return o == OrderTypeLimit
}
// String is the stringer function
func (o OrderType) String() string {
if o == OrderTypeMarket {
return "market"
} else if o == OrderTypeLimit {
return "limit"
}
return "error, unrecognized order type"
}
var orderTypeMap = map[string]OrderType{
"market": OrderTypeMarket,
"limit": OrderTypeLimit,
}
// OrderTypeFromString is a convenience to convert from common strings to the corresponding OrderType
func OrderTypeFromString(s string) OrderType {
return orderTypeMap[s]
}
// Order represents an order in the orderbook
type Order struct {
Pair *TradingPair
OrderAction OrderAction
OrderType OrderType
Price *Number
Volume *Number
Timestamp *Timestamp
}
// String is the stringer function
func (o Order) String() string {
tsString := nilString
if o.Timestamp != nil {
tsString = fmt.Sprintf("%d", o.Timestamp.AsInt64())
}
return fmt.Sprintf("Order[pair=%s, action=%s, type=%s, price=%s, vol=%s, ts=%s]",
o.Pair,
o.OrderAction,
o.OrderType,
o.Price.AsString(),
o.Volume.AsString(),
tsString,
)
}
// OrderBook encapsulates the concept of an orderbook on a market
type OrderBook struct {
pair *TradingPair
asks []Order
bids []Order
}
// Pair returns trading pair
func (o OrderBook) Pair() *TradingPair {
return o.pair
}
// Asks returns the asks in an orderbook
func (o OrderBook) Asks() []Order {
return o.asks
}
// Bids returns the bids in an orderbook
func (o OrderBook) Bids() []Order {
return o.bids
}
// TopAsk returns the best ask in an orderbook
func (o OrderBook) TopAsk() *Order {
if len(o.Asks()) > 0 {
return &o.Asks()[0]
}
return nil
}
// TopBid returns the best bid in an orderbook
func (o OrderBook) TopBid() *Order {
if len(o.Bids()) > 0 {
return &o.Bids()[0]
}
return nil
}
// MakeOrderBook creates a new OrderBook from the asks and the bids
func MakeOrderBook(pair *TradingPair, asks []Order, bids []Order) *OrderBook {
return &OrderBook{
pair: pair,
asks: asks,
bids: bids,
}
}
// TransactionID is typed for the concept of a transaction ID of an order
type TransactionID string
// String is the stringer function
func (t *TransactionID) String() string {
return string(*t)
}
// MakeTransactionID is a factory method for convenience
func MakeTransactionID(s string) *TransactionID {
t := TransactionID(s)
return &t
}
// OpenOrder represents an open order for a trading account
type OpenOrder struct {
Order
ID string
StartTime *Timestamp
ExpireTime *Timestamp
VolumeExecuted *Number
}
// String is the stringer function
func (o OpenOrder) String() string {
expireTimeString := nilString
if o.ExpireTime != nil {
expireTimeString = fmt.Sprintf("%d", o.ExpireTime.AsInt64())
}
return fmt.Sprintf("OpenOrder[order=%s, ID=%s, startTime=%d, expireTime=%s, volumeExecuted=%s]",
o.Order.String(),
o.ID,
o.StartTime.AsInt64(),
expireTimeString,
o.VolumeExecuted.AsString(),
)
}
// CancelOrderResult is the result of a CancelOrder call
type CancelOrderResult int8
// These are the available types
const (
CancelResultCancelSuccessful CancelOrderResult = 0
CancelResultPending CancelOrderResult = 1
CancelResultFailed CancelOrderResult = 2
)
// String is the stringer function
func (r CancelOrderResult) String() string {
if r == CancelResultCancelSuccessful {
return "cancelled"
} else if r == CancelResultPending {
return "pending"
} else if r == CancelResultFailed {
return "failed"
}
return "error, unrecognized CancelOrderResult"
}
// Trade represents a trade on an exchange
type Trade struct {
Order
TransactionID *TransactionID
Cost *Number
Fee *Number
}
// TradesByTsID implements sort.Interface for []Trade based on Timestamp and TransactionID
type TradesByTsID []Trade
func (t TradesByTsID) Len() int {
return len(t)
}
func (t TradesByTsID) Swap(i int, j int) {
t[i], t[j] = t[j], t[i]
}
func (t TradesByTsID) Less(i int, j int) bool {
if t[i].Order.Timestamp.AsInt64() < t[j].Order.Timestamp.AsInt64() {
return true
} else if t[i].Order.Timestamp.AsInt64() > t[j].Order.Timestamp.AsInt64() {
return false
}
if t[i].TransactionID != nil && t[j].TransactionID != nil {
return t[i].TransactionID.String() < t[j].TransactionID.String()
}
if t[i].TransactionID != nil {
return false
}
return true
}
func (t Trade) String() string {
return fmt.Sprintf("Trade[txid: %s, ts: %s, pair: %s, action: %s, type: %s, counterPrice: %s, baseVolume: %s, counterCost: %s, fee: %s]",
utils.CheckedString(t.TransactionID),
utils.CheckedString(t.Timestamp),
*t.Pair,
t.OrderAction,
t.OrderType,
utils.CheckedString(t.Price),
utils.CheckedString(t.Volume),
utils.CheckedString(t.Cost),
utils.CheckedString(t.Fee),
)
}
// OrderConstraints describes constraints when placing orders on an excahnge
type OrderConstraints struct {
PricePrecision int8
VolumePrecision int8
MinBaseVolume Number
MinQuoteVolume *Number
}
// MakeOrderConstraints is a factory method for OrderConstraints
func MakeOrderConstraints(pricePrecision int8, volumePrecision int8, minBaseVolume float64) *OrderConstraints {
return &OrderConstraints{
PricePrecision: pricePrecision,
VolumePrecision: volumePrecision,
MinBaseVolume: *NumberFromFloat(minBaseVolume, volumePrecision),
MinQuoteVolume: nil,
}
}
// MakeOrderConstraintsWithCost is a factory method for OrderConstraints
func MakeOrderConstraintsWithCost(pricePrecision int8, volumePrecision int8, minBaseVolume float64, minQuoteVolume float64) *OrderConstraints {
return &OrderConstraints{
PricePrecision: pricePrecision,
VolumePrecision: volumePrecision,
MinBaseVolume: *NumberFromFloat(minBaseVolume, volumePrecision),
MinQuoteVolume: NumberFromFloat(minQuoteVolume, minQuoteVolumePrecision),
}
}
// MakeOrderConstraintsWithOverride is a factory method for OrderConstraints, oc is not a pointer because we want a copy since we modify it
func MakeOrderConstraintsWithOverride(oc OrderConstraints, override *OrderConstraintsOverride) *OrderConstraints {
if override.PricePrecision != nil {
oc.PricePrecision = *override.PricePrecision
}
if override.VolumePrecision != nil {
oc.VolumePrecision = *override.VolumePrecision
}
if override.MinBaseVolume != nil {
oc.MinBaseVolume = *override.MinBaseVolume
}
if override.MinQuoteVolume != nil {
oc.MinQuoteVolume = *override.MinQuoteVolume
}
return &oc
}
// MakeOrderConstraintsFromOverride is a factory method to convert an OrderConstraintsOverride to an OrderConstraints
func MakeOrderConstraintsFromOverride(override *OrderConstraintsOverride) *OrderConstraints {
return MakeOrderConstraintsWithOverride(OrderConstraints{}, override)
}
// OrderConstraints describes constraints when placing orders on an excahnge
func (o *OrderConstraints) String() string {
minQuoteVolumeStr := nilString
if o.MinQuoteVolume != nil {
minQuoteVolumeStr = o.MinQuoteVolume.AsString()
}
return fmt.Sprintf("OrderConstraints[PricePrecision: %d, VolumePrecision: %d, MinBaseVolume: %s, MinQuoteVolume: %s]",
o.PricePrecision, o.VolumePrecision, o.MinBaseVolume.AsString(), minQuoteVolumeStr)
}
// OrderConstraintsOverride describes an override for an OrderConstraint
type OrderConstraintsOverride struct {
PricePrecision *int8
VolumePrecision *int8
MinBaseVolume *Number
MinQuoteVolume **Number
}
// MakeOrderConstraintsOverride is a factory method
func MakeOrderConstraintsOverride(
pricePrecision *int8,
volumePrecision *int8,
minBaseVolume *Number,
minQuoteVolume **Number,
) *OrderConstraintsOverride {
return &OrderConstraintsOverride{
PricePrecision: pricePrecision,
VolumePrecision: volumePrecision,
MinBaseVolume: minBaseVolume,
MinQuoteVolume: minQuoteVolume,
}
}
// MakeOrderConstraintsOverrideFromConstraints is a factory method for OrderConstraintsOverride
func MakeOrderConstraintsOverrideFromConstraints(oc *OrderConstraints) *OrderConstraintsOverride {
return &OrderConstraintsOverride{
PricePrecision: &oc.PricePrecision,
VolumePrecision: &oc.VolumePrecision,
MinBaseVolume: &oc.MinBaseVolume,
MinQuoteVolume: &oc.MinQuoteVolume,
}
}
// IsComplete returns true if the override contains all values
func (override *OrderConstraintsOverride) IsComplete() bool {
if override.PricePrecision == nil {
return false
}
if override.VolumePrecision == nil {
return false
}
if override.MinBaseVolume == nil {
return false
}
if override.MinQuoteVolume == nil {
return false
}
return true
}
// Augment only updates values if updates are non-nil
func (override *OrderConstraintsOverride) Augment(updates *OrderConstraintsOverride) {
if updates.PricePrecision != nil {
override.PricePrecision = updates.PricePrecision
}
if updates.VolumePrecision != nil {
override.VolumePrecision = updates.VolumePrecision
}
if updates.MinBaseVolume != nil {
override.MinBaseVolume = updates.MinBaseVolume
}
if updates.MinQuoteVolume != nil {
override.MinQuoteVolume = updates.MinQuoteVolume
}
}