/
orderbook.go
157 lines (144 loc) · 3.34 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
package okex
import (
"fmt"
"github.com/MauriceGit/skiplist"
"strconv"
"time"
)
const (
ActionDepthL2Partial = "partial"
ActionDepthL2Update = "update"
)
type Item struct {
Price float64
Amount float64
}
func (e Item) ExtractKey() float64 {
return e.Price
}
func (e Item) String() string {
return fmt.Sprintf("%.8f", e.Price)
}
type OrderBook struct {
InstrumentID string `json:"instrument_id"`
Asks []Item `json:"asks"`
Bids []Item `json:"bids"`
}
type DepthOrderBook struct {
instrumentID string // BTC-USD-SWAP
asks skiplist.SkipList
bids skiplist.SkipList
}
func (d *DepthOrderBook) GetInstrumentID() string {
return d.instrumentID
}
func (d *DepthOrderBook) Update(action string, data *WSDepthL2Tbt) {
if action == ActionDepthL2Partial {
d.asks = skiplist.NewSeedEps(time.Now().UTC().UnixNano(), 0.00000001)
d.bids = skiplist.NewSeedEps(time.Now().UTC().UnixNano(), 0.00000001)
//d.asks = skiplist.New()
//d.bids = skiplist.New()
// 举例: ["411.8", "10", "1", "4"]
// 411.8为深度价格,10为此价格的合约张数,1为此价格的强平单个数,4为此价格的订单个数。
for _, ask := range data.Asks {
price, _ := strconv.ParseFloat(ask[0], 64)
amount, _ := strconv.ParseFloat(ask[1], 64)
d.asks.Insert(Item{
Price: price,
Amount: amount,
})
}
for _, bid := range data.Bids {
price, _ := strconv.ParseFloat(bid[0], 64)
amount, _ := strconv.ParseFloat(bid[1], 64)
d.bids.Insert(Item{
Price: price,
Amount: amount,
})
}
return
}
if action == ActionDepthL2Update {
for _, ask := range data.Asks {
price, _ := strconv.ParseFloat(ask[0], 64)
amount, _ := strconv.ParseFloat(ask[1], 64)
if amount == 0 {
d.asks.Delete(Item{
Price: price,
Amount: amount,
})
} else {
item := Item{
Price: price,
Amount: amount,
}
elem, ok := d.asks.Find(item)
if ok {
d.asks.ChangeValue(elem, item)
} else {
d.asks.Insert(item)
}
}
}
for _, bid := range data.Bids {
price, _ := strconv.ParseFloat(bid[0], 64)
amount, _ := strconv.ParseFloat(bid[1], 64)
if amount == 0 {
d.bids.Delete(Item{
Price: price,
Amount: amount,
})
} else {
item := Item{
Price: price,
Amount: amount,
}
elem, ok := d.bids.Find(item)
if ok {
d.bids.ChangeValue(elem, item)
} else {
d.bids.Insert(item)
}
}
}
}
}
func (d *DepthOrderBook) GetOrderBook(depth int) (result OrderBook) {
result.InstrumentID = d.instrumentID
smallest := d.asks.GetSmallestNode()
if smallest != nil {
result.Asks = append(result.Asks, smallest.GetValue().(Item))
count := 1
node := smallest
for count < depth {
node = d.asks.Next(node)
if node == nil {
break
}
result.Asks = append(result.Asks, node.GetValue().(Item))
count++
}
}
largest := d.bids.GetLargestNode()
if largest != nil {
result.Bids = append(result.Bids, largest.GetValue().(Item))
count := 1
node := largest
for count < depth {
node = d.bids.Prev(node)
if node == nil {
break
}
result.Bids = append(result.Bids, node.GetValue().(Item))
count++
}
}
return
}
func NewDepthOrderBook(instrumentID string) *DepthOrderBook {
return &DepthOrderBook{
instrumentID: instrumentID,
asks: skiplist.New(),
bids: skiplist.New(),
}
}