-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathorderbook.go
184 lines (155 loc) · 3.51 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
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
package bitmex
import (
"sort"
"strconv"
"sync"
"time"
)
// OrderBookL2 contains order book l2
type OrderBookL2 struct {
ID int64 `json:"id"`
Price float64 `json:"price"`
Side string `json:"side"`
Size int64 `json:"size"`
Symbol string `json:"symbol"`
}
func (o *OrderBookL2) Key() string {
// return fmt.Sprintf("%s-%s-%d", o.Symbol, o.Side, o.ID)
return strconv.FormatInt(o.ID, 10)
}
type OrderBookData []*OrderBookL2
type OrderBookDataL2 struct {
RawData []OrderBookL2
Timestamp time.Time
}
func (o *OrderBookDataL2) OrderBook() (ob OrderBook) {
for _, v := range o.RawData {
switch v.Side {
case "Buy":
ob.Bids = append(ob.Bids, Item{
Price: v.Price,
Amount: float64(v.Size),
})
case "Sell":
ob.Asks = append(ob.Asks, Item{
Price: v.Price,
Amount: float64(v.Size),
})
}
}
sort.Slice(ob.Bids, func(i, j int) bool {
return ob.Bids[i].Price > ob.Bids[j].Price
})
sort.Slice(ob.Asks, func(i, j int) bool {
return ob.Asks[i].Price < ob.Asks[j].Price
})
ob.Timestamp = o.Timestamp
return
}
// OrderBook10 contains order book 10
type OrderBook10 struct {
Bids [][]float64 `json:"bids"`
Asks [][]float64 `json:"asks"`
Timestamp time.Time `json:"timestamp"`
Symbol string `json:"symbol"`
}
// Item stores the amount and price values
type Item struct {
Amount float64 `json:"amount"`
Price float64 `json:"price"`
}
type OrderBook struct {
Bids []Item `json:"bids"`
Asks []Item `json:"asks"`
Timestamp time.Time `json:"timestamp"`
}
func (ob *OrderBook) Valid() bool {
return len(ob.Bids) > 0 && len(ob.Asks) > 0
}
func (ob *OrderBook) Bid() float64 {
if len(ob.Bids) < 1 {
return 0.0
}
return ob.Bids[0].Price
}
func (ob *OrderBook) Ask() float64 {
if len(ob.Asks) < 1 {
return 0.0
}
return ob.Asks[0].Price
}
type OrderBookLocal struct {
ob map[string]*OrderBookL2
m sync.Mutex
}
func NewOrderBookLocal() *OrderBookLocal {
o := &OrderBookLocal{
ob: make(map[string]*OrderBookL2),
}
return o
}
func (o *OrderBookLocal) GetOrderbookL2() (ob OrderBookDataL2) {
ob.RawData = make([]OrderBookL2, 0, len(o.ob))
for _, v := range o.ob {
ob.RawData = append(ob.RawData, *v)
}
ob.Timestamp = time.Now()
return
}
func (o *OrderBookLocal) GetOrderbook() (ob OrderBook) {
//ob.Symbol = "XBTUSD"
for _, v := range o.ob {
switch v.Side {
case "Buy":
ob.Bids = append(ob.Bids, Item{
Price: v.Price,
Amount: float64(v.Size),
})
case "Sell":
ob.Asks = append(ob.Asks, Item{
Price: v.Price,
Amount: float64(v.Size),
})
}
}
sort.Slice(ob.Bids, func(i, j int) bool {
return ob.Bids[i].Price > ob.Bids[j].Price
})
sort.Slice(ob.Asks, func(i, j int) bool {
return ob.Asks[i].Price < ob.Asks[j].Price
})
ob.Timestamp = time.Now()
return
}
func (o *OrderBookLocal) LoadSnapshot(newOrderbook []*OrderBookL2) error {
o.m.Lock()
defer o.m.Unlock()
o.ob = make(map[string]*OrderBookL2)
for _, v := range newOrderbook {
o.ob[v.Key()] = v
}
return nil
}
func (o *OrderBookLocal) Update(orderbook []*OrderBookL2, action string) {
o.m.Lock()
defer o.m.Unlock()
switch action {
case bitmexActionUpdateData:
for _, elem := range orderbook {
if v, ok := o.ob[elem.Key()]; ok {
// price is same while id is same
// v.Price = elem.Price
v.Size = elem.Size
v.Side = elem.Side
}
}
case bitmexActionDeleteData:
for _, v := range orderbook {
delete(o.ob, v.Key())
}
case bitmexActionInsertData:
for _, v := range orderbook {
o.ob[v.Key()] = v
}
}
}