-
Notifications
You must be signed in to change notification settings - Fork 11
/
depth.go
89 lines (74 loc) · 1.51 KB
/
depth.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
package swap
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/pkg/errors"
)
type (
DepthHighFreqChannel struct {
contractCode string
size int
}
Depth struct {
Asks [][2]float64
Bids [][2]float64
Ch string
Event string
ID int64
MrID int64
TS int64
Version int
}
DepthSize int
)
const (
DepthSize20 DepthSize = 20
DepthSize150 DepthSize = 150
)
func NewDepthHighFreq(symbol string, size DepthSize) exchange.Channel {
return &DepthHighFreqChannel{
contractCode: symbol,
size: int(size),
}
}
func (ch *DepthHighFreqChannel) String() string {
return fmt.Sprintf("market.%s.depth.size_%d.high_freq", ch.contractCode, ch.size)
}
func ParseDepth(raw json.RawMessage) (interface{}, error) {
var d Depth
if err := json.Unmarshal(raw, &d); err != nil {
return nil, err
}
fields := strings.Split(d.Ch, ".")
if len(fields) < 2 {
return nil, errors.Errorf("invalid ch %s", d.Ch)
}
sym, err := ParseSymbol(fields[1])
if err != nil {
return nil, err
}
bids := make([]exchange.OrderElem, len(d.Bids))
asks := make([]exchange.OrderElem, len(d.Asks))
for i, b := range d.Bids {
bids[i] = exchange.OrderElem{
Price: b[0],
Amount: b[1],
}
}
for i, a := range d.Asks {
asks[i] = exchange.OrderElem{
Price: a[0],
Amount: a[1],
}
}
return &exchange.OrderBook{
Symbol: sym,
Bids: bids,
Asks: asks,
Created: time.Unix(d.TS/1000, d.TS%1000*1e6),
Raw: &d,
}, nil
}