-
Notifications
You must be signed in to change notification settings - Fork 220
/
order.go
142 lines (125 loc) · 3.64 KB
/
order.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
package order
import (
"fmt"
"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
)
type Order struct {
ID int64
GID int64
CID int64
Symbol string
MTSCreated int64
MTSUpdated int64
Amount float64
AmountOrig float64
Type string
TypePrev string
MTSTif int64
Flags int64
Status string
Price float64
PriceAvg float64
PriceTrailing float64
PriceAuxLimit float64
Notify bool
Hidden bool
PlacedID int64
Routing string
Meta map[string]interface{}
}
// Snapshot is a collection of Orders that would usually be sent on
// inital connection.
type Snapshot struct {
Snapshot []*Order
}
// Update is an Order that gets sent out after every change to an order.
type Update Order
// New gets sent out after an Order was created successfully.
type New Order
// Cancel gets sent out after an Order was cancelled successfully.
type Cancel Order
// FromRaw takes the raw list of values as returned from the websocket
// service and tries to convert it into an Order.
func FromRaw(raw []interface{}) (o *Order, err error) {
if len(raw) < 32 {
return o, fmt.Errorf("data slice too short for order: %#v", raw)
}
o = &Order{
ID: convert.I64ValOrZero(raw[0]),
GID: convert.I64ValOrZero(raw[1]),
CID: convert.I64ValOrZero(raw[2]),
Symbol: convert.SValOrEmpty(raw[3]),
MTSCreated: convert.I64ValOrZero(raw[4]),
MTSUpdated: convert.I64ValOrZero(raw[5]),
Amount: convert.F64ValOrZero(raw[6]),
AmountOrig: convert.F64ValOrZero(raw[7]),
Type: convert.SValOrEmpty(raw[8]),
TypePrev: convert.SValOrEmpty(raw[9]),
MTSTif: convert.I64ValOrZero(raw[10]),
Flags: convert.I64ValOrZero(raw[12]),
Status: convert.SValOrEmpty(raw[13]),
Price: convert.F64ValOrZero(raw[16]),
PriceAvg: convert.F64ValOrZero(raw[17]),
PriceTrailing: convert.F64ValOrZero(raw[18]),
PriceAuxLimit: convert.F64ValOrZero(raw[19]),
Notify: convert.BValOrFalse(raw[23]),
Hidden: convert.BValOrFalse(raw[24]),
PlacedID: convert.I64ValOrZero(raw[25]),
Routing: convert.SValOrEmpty(raw[28]),
}
if meta, ok := raw[31].(map[string]interface{}); ok {
o.Meta = meta
}
return
}
// NewFromRaw reds "on" type message from data stream and
// maps it to order.New data structure
func NewFromRaw(raw []interface{}) (New, error) {
o, err := FromRaw(raw)
if err != nil {
return New{}, err
}
return New(*o), nil
}
// UpdateFromRaw reds "ou" type message from data stream and
// maps it to order.Update data structure
func UpdateFromRaw(raw []interface{}) (Update, error) {
o, err := FromRaw(raw)
if err != nil {
return Update{}, err
}
return Update(*o), nil
}
// CancelFromRaw reds "oc" type message from data stream and
// maps it to order.Cancel data structure
func CancelFromRaw(raw []interface{}) (Cancel, error) {
o, err := FromRaw(raw)
if err != nil {
return Cancel{}, err
}
return Cancel(*o), nil
}
// SnapshotFromRaw takes a raw list of values as returned from the websocket
// service and tries to convert it into an Snapshot.
func SnapshotFromRaw(raw []interface{}) (s *Snapshot, err error) {
if len(raw) == 0 {
return s, fmt.Errorf("data slice too short for order: %#v", raw)
}
os := make([]*Order, 0)
switch raw[0].(type) {
case []interface{}:
for _, v := range raw {
if l, ok := v.([]interface{}); ok {
o, err := FromRaw(l)
if err != nil {
return s, err
}
os = append(os, o)
}
}
default:
return s, fmt.Errorf("not an order snapshot")
}
s = &Snapshot{Snapshot: os}
return
}