-
Notifications
You must be signed in to change notification settings - Fork 155
/
orderside.go
218 lines (183 loc) · 4.67 KB
/
orderside.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
package orderbook
import (
"container/list"
"encoding/json"
"fmt"
"strings"
rbtx "github.com/emirpasic/gods/examples/redblacktreeextended"
rbt "github.com/emirpasic/gods/trees/redblacktree"
"github.com/shopspring/decimal"
)
// OrderSide implements facade to operations with order queue
type OrderSide struct {
priceTree *rbtx.RedBlackTreeExtended
prices map[string]*OrderQueue
volume decimal.Decimal
numOrders int
depth int
}
func rbtComparator(a, b interface{}) int {
return a.(decimal.Decimal).Cmp(b.(decimal.Decimal))
}
// NewOrderSide creates new OrderSide manager
func NewOrderSide() *OrderSide {
return &OrderSide{
priceTree: &rbtx.RedBlackTreeExtended{
Tree: rbt.NewWith(rbtComparator),
},
prices: map[string]*OrderQueue{},
volume: decimal.Zero,
}
}
// Len returns amount of orders
func (os *OrderSide) Len() int {
return os.numOrders
}
// Depth returns depth of market
func (os *OrderSide) Depth() int {
return os.depth
}
// Volume returns total amount of quantity in side
func (os *OrderSide) Volume() decimal.Decimal {
return os.volume
}
// Append appends order to definite price level
func (os *OrderSide) Append(o *Order) *list.Element {
price := o.Price()
strPrice := price.String()
priceQueue, ok := os.prices[strPrice]
if !ok {
priceQueue = NewOrderQueue(o.Price())
os.prices[strPrice] = priceQueue
os.priceTree.Put(price, priceQueue)
os.depth++
}
os.numOrders++
os.volume = os.volume.Add(o.Quantity())
return priceQueue.Append(o)
}
// Remove removes order from definite price level
func (os *OrderSide) Remove(e *list.Element) *Order {
price := e.Value.(*Order).Price()
strPrice := price.String()
priceQueue := os.prices[strPrice]
o := priceQueue.Remove(e)
if priceQueue.Len() == 0 {
delete(os.prices, strPrice)
os.priceTree.Remove(price)
os.depth--
}
os.numOrders--
os.volume = os.volume.Sub(o.Quantity())
return o
}
// MaxPriceQueue returns maximal level of price
func (os *OrderSide) MaxPriceQueue() *OrderQueue {
if os.depth > 0 {
if value, found := os.priceTree.GetMax(); found {
return value.(*OrderQueue)
}
}
return nil
}
// MinPriceQueue returns maximal level of price
func (os *OrderSide) MinPriceQueue() *OrderQueue {
if os.depth > 0 {
if value, found := os.priceTree.GetMin(); found {
return value.(*OrderQueue)
}
}
return nil
}
// LessThan returns nearest OrderQueue with price less than given
func (os *OrderSide) LessThan(price decimal.Decimal) *OrderQueue {
tree := os.priceTree.Tree
node := tree.Root
var floor *rbt.Node
for node != nil {
if tree.Comparator(price, node.Key) > 0 {
floor = node
node = node.Right
} else {
node = node.Left
}
}
if floor != nil {
return floor.Value.(*OrderQueue)
}
return nil
}
// GreaterThan returns nearest OrderQueue with price greater than given
func (os *OrderSide) GreaterThan(price decimal.Decimal) *OrderQueue {
tree := os.priceTree.Tree
node := tree.Root
var ceiling *rbt.Node
for node != nil {
if tree.Comparator(price, node.Key) < 0 {
ceiling = node
node = node.Left
} else {
node = node.Right
}
}
if ceiling != nil {
return ceiling.Value.(*OrderQueue)
}
return nil
}
// Orders returns all of *list.Element orders
func (os *OrderSide) Orders() (orders []*list.Element) {
for _, price := range os.prices {
iter := price.Head()
for iter != nil {
orders = append(orders, iter)
iter = iter.Next()
}
}
return
}
// String implements fmt.Stringer interface
func (os *OrderSide) String() string {
sb := strings.Builder{}
level := os.MaxPriceQueue()
for level != nil {
sb.WriteString(fmt.Sprintf("\n%s -> %s", level.Price(), level.Volume()))
level = os.LessThan(level.Price())
}
return sb.String()
}
// MarshalJSON implements json.Marshaler interface
func (os *OrderSide) MarshalJSON() ([]byte, error) {
return json.Marshal(
&struct {
NumOrders int `json:"numOrders"`
Depth int `json:"depth"`
Prices map[string]*OrderQueue `json:"prices"`
}{
NumOrders: os.numOrders,
Depth: os.depth,
Prices: os.prices,
},
)
}
// UnmarshalJSON implements json.Unmarshaler interface
func (os *OrderSide) UnmarshalJSON(data []byte) error {
obj := struct {
NumOrders int `json:"numOrders"`
Depth int `json:"depth"`
Prices map[string]*OrderQueue `json:"prices"`
}{}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
os.numOrders = obj.NumOrders
os.depth = obj.Depth
os.prices = obj.Prices
os.priceTree = &rbtx.RedBlackTreeExtended{
Tree: rbt.NewWith(rbtComparator),
}
for price, queue := range os.prices {
os.priceTree.Put(decimal.RequireFromString(price), queue)
}
return nil
}