forked from aopoltorzhicky/go_kraken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactories.go
270 lines (238 loc) · 6.85 KB
/
factories.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
package websocket
import (
"fmt"
"strings"
)
type tickerFactory struct{}
func newTickerFactory() *tickerFactory {
return &tickerFactory{}
}
func (f *tickerFactory) Parse(data interface{}, pair string) (interface{}, error) {
result := TickerUpdate{
Pair: pair,
}
body, ok := data.(map[string]interface{})
if !ok {
return result, fmt.Errorf("Can't parse data %#v", data)
}
for k, v := range body {
value := v.([]interface{})
switch k {
case "a":
result.Ask = parseLevel(value)
case "b":
result.Bid = parseLevel(value)
case "c":
result.Close = parseValues(value)
case "v":
result.Volume = parseValues(value)
case "o":
result.Open = parseValues(value)
case "h":
result.High = parseValues(value)
case "l":
result.Low = parseValues(value)
case "t":
result.TradeVolume = parseValues(value)
case "p":
result.VolumeAveragePrice = parseValues(value)
}
}
return result, nil
}
type candlesFactory struct{}
func newCandlesFactory() *candlesFactory {
return &candlesFactory{}
}
func (f *candlesFactory) Parse(data interface{}, pair string) (interface{}, error) {
body, ok := data.([]interface{})
if !ok {
return CandleUpdate{Pair: pair}, fmt.Errorf("Can't parse data %#v", data)
}
return CandleUpdate{
Time: valToFloat64(body[0]),
EndTime: valToFloat64(body[1]),
Open: valToFloat64(body[2]),
High: valToFloat64(body[3]),
Low: valToFloat64(body[4]),
Close: valToFloat64(body[5]),
VolumeWAP: valToFloat64(body[6]),
Volume: valToFloat64(body[7]),
Count: int(body[8].(float64)),
Pair: pair,
}, nil
}
type tradesFactory struct{}
func newTradesFactory() *tradesFactory {
return &tradesFactory{}
}
func (f *tradesFactory) Parse(data interface{}, pair string) (interface{}, error) {
result := []TradeUpdate{}
body, ok := data.([]interface{})
if !ok {
return result, fmt.Errorf("Can't parse data %#v", data)
}
for _, item := range body {
entity := item.([]interface{})
trade := TradeUpdate{
Price: valToFloat64(entity[0]),
Volume: valToFloat64(entity[1]),
Time: valToFloat64(entity[2]),
Side: parseSide(entity[3].(string)),
OrderType: parseOrderType(entity[4].(string)),
Misc: entity[5].(string),
Pair: pair,
}
result = append(result, trade)
}
return result, nil
}
type spreadFactory struct{}
func newSpreadFactory() *spreadFactory {
return &spreadFactory{}
}
func (f *spreadFactory) Parse(data interface{}, pair string) (interface{}, error) {
body, ok := data.([]interface{})
if !ok {
return SpreadUpdate{Pair: pair}, fmt.Errorf("Can't parse data %#v", data)
}
return SpreadUpdate{
Bid: valToFloat64(body[0]),
Ask: valToFloat64(body[1]),
Time: valToFloat64(body[2]),
BidVolume: valToFloat64(body[3]),
AskVolume: valToFloat64(body[4]),
Pair: pair,
}, nil
}
type bookFactory struct{}
func newBookFactory() *bookFactory {
return &bookFactory{}
}
func (f *bookFactory) Parse(data interface{}, pair string) (interface{}, error) {
result := OrderBookUpdate{
Pair: pair,
}
body, ok := data.(map[string]interface{})
if !ok {
return result, fmt.Errorf("Can't parse data %#v", data)
}
for k, v := range body {
switch k {
case "c":
checkSum, ok := v.(string)
if !ok {
return nil, fmt.Errorf("[bookFactory] Invalid checkSum type: %v %T", v, v)
}
result.CheckSum = checkSum
default:
items, err := f.parseItems(v)
if err != nil {
return nil, err
}
result.IsSnapshot = len(k) == 2 && strings.HasSuffix(k, "s")
if strings.HasPrefix(k, "a") {
result.Asks = items
} else {
result.Bids = items
}
}
}
return result, nil
}
func (f *bookFactory) parseItems(value interface{}) ([]OrderBookItem, error) {
items := make([]OrderBookItem, 0)
updates, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("[bookFactory] Invalid items type: %v %T", value, value)
}
for _, item := range updates {
entity := item.([]interface{})
orderBookItem := OrderBookItem{
Price: valToFloat64(entity[0]),
Volume: valToFloat64(entity[1]),
Time: valToFloat64(entity[2]),
}
orderBookItem.Republish = (len(entity) == 4 && entity[3] == "r")
items = append(items, orderBookItem)
}
return items, nil
}
type ownTradesFactory struct{}
func newOwnTradesFactory() *ownTradesFactory {
return &ownTradesFactory{}
}
func (f *ownTradesFactory) Parse(data interface{}, pair string) (interface{}, error) {
upd := OwnTradesUpdate{
ChannelName: ChanOwnTrades,
Trades: make(map[string]OwnTrade),
}
body, ok := data.([]interface{})
if !ok {
return upd, fmt.Errorf("Can't parse data, expected slice, but got %#v", data)
}
for key, valueRaw := range body[0].(map[string]interface{}) {
value := valueRaw.(map[string]interface{})
upd.Trades[key] = OwnTrade{
Cost: valToFloat64(value["cost"]),
Fee: valToFloat64(value["fee"]),
Margin: valToFloat64(value["margin"]),
OrderID: value["ordertxid"].(string),
OrderType: value["ordertype"].(string),
Pair: value["pair"].(string),
PosTxID: value["postxid"].(string),
Price: valToFloat64(value["price"]),
Time: valToFloat64(value["time"]),
Type: value["type"].(string),
Vol: valToFloat64(value["vol"]),
}
}
return upd, nil
}
type openOrdersFactory struct{}
func newOpenOrdersFactory() *openOrdersFactory {
return &openOrdersFactory{}
}
func (f *openOrdersFactory) Parse(data interface{}, pair string) (interface{}, error) {
upd := OpenOrdersUpdate{
ChannelName: ChanOpenOrders,
Order: make(map[string]OpenOrder),
}
body, ok := data.([]interface{})
if !ok {
return upd, fmt.Errorf("Can't parse data %#v", data)
}
if len(body) != 2 {
return upd, fmt.Errorf("Can't parse data %#v", data)
}
for key, value := range body[0].(map[string]map[string]interface{}) {
upd.Order[key] = OpenOrder{
Cost: valToFloat64(value["cost"]),
Fee: valToFloat64(value["fee"]),
LimitPrice: valToFloat64(value["limitprice"]),
Misc: value["misc"].(string),
Oflags: value["oflags"].(string),
OpenTime: valToFloat64(value["opentm"]),
StartTime: valToFloat64(value["starttm"]),
ExpireTime: valToFloat64(value["expiretm"]),
Price: valToFloat64(value["price"]),
Refid: value["refid"].(string),
Status: value["status"].(string),
StopPrice: valToFloat64(value["stopprice"]),
UserRef: int(value["userref"].(float64)),
Vol: valToFloat64(value["vol"]),
VolExec: valToFloat64(value["vol_exec"]),
Descr: OpenOrderDescr{
Close: value["close"].(string),
Leverage: value["leverage"].(string),
Order: value["order"].(string),
Ordertype: value["ordertype"].(string),
Pair: value["pair"].(string),
Price: valToFloat64(value["price"]),
Price2: valToFloat64(value["price2"]),
Type: value["type"].(string),
},
}
}
return upd, nil
}