-
Notifications
You must be signed in to change notification settings - Fork 6
/
orderbook.go
153 lines (141 loc) · 3.06 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
package deribit
import (
"fmt"
"github.com/MauriceGit/skiplist"
"github.com/frankrap/deribit-api/models"
. "github.com/matthew188/crex"
)
type DobItem struct {
Price float64
Amount float64
}
func (e DobItem) ExtractKey() float64 {
return e.Price
}
func (e DobItem) String() string {
return fmt.Sprintf("%.2f", e.Price)
}
type DepthOrderBook struct {
symbol string
asks skiplist.SkipList
bids skiplist.SkipList
}
func (d *DepthOrderBook) GetSymbol() string {
return d.symbol
}
func (d *DepthOrderBook) Update(data *models.OrderBookRawNotification) {
if data.PrevChangeID == 0 {
d.asks = skiplist.New()
d.bids = skiplist.New()
// 举例: ["411.8", "10", "1", "4"]
// 411.8为深度价格,10为此价格的合约张数,1为此价格的强平单个数,4为此价格的订单个数。
for _, ask := range data.Asks {
d.asks.Insert(DobItem{
Price: ask.Price,
Amount: ask.Amount,
})
}
for _, bid := range data.Bids {
d.bids.Insert(DobItem{
Price: bid.Price,
Amount: bid.Amount,
})
}
return
}
if data.PrevChangeID > 0 {
for _, ask := range data.Asks {
if ask.Action == "delete" {
d.asks.Delete(DobItem{
Price: ask.Price,
Amount: ask.Amount,
})
} else if ask.Action == "new" || ask.Action == "change" {
item := DobItem{
Price: ask.Price,
Amount: ask.Amount,
}
elem, ok := d.asks.Find(item)
if ok {
d.asks.ChangeValue(elem, item)
} else {
d.asks.Insert(item)
}
}
}
for _, bid := range data.Bids {
if bid.Action == "delete" {
d.bids.Delete(DobItem{
Price: bid.Price,
Amount: bid.Amount,
})
} else if bid.Action == "new" || bid.Action == "change" {
item := DobItem{
Price: bid.Price,
Amount: bid.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.Symbol = d.symbol
smallest := d.asks.GetSmallestNode()
if smallest != nil {
item := smallest.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := smallest
for count < depth {
node = d.asks.Next(node)
if node == smallest {
break
}
item := node.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}
largest := d.bids.GetLargestNode()
if largest != nil {
item := largest.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := largest
for count < depth {
node = d.bids.Prev(node)
if node == largest {
break
}
item := node.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}
return
}
func NewDepthOrderBook(symbol string) *DepthOrderBook {
return &DepthOrderBook{
symbol: symbol,
asks: skiplist.New(),
bids: skiplist.New(),
}
}