-
Notifications
You must be signed in to change notification settings - Fork 22
/
stop_orders.go
289 lines (238 loc) · 7.2 KB
/
stop_orders.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package stoporders
import (
"log"
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
v1 "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
"golang.org/x/exp/maps"
)
type Pool struct {
log *logging.Logger
// map partyId * map orderId * StopOrder
orders map[string]map[string]*types.StopOrder
// useful to find back a party from an order
orderToParty map[string]string
priced *PricedStopOrders
trailing *TrailingStopOrders
}
func New(log *logging.Logger) *Pool {
return &Pool{
log: log,
orders: map[string]map[string]*types.StopOrder{},
orderToParty: map[string]string{},
priced: NewPricedStopOrders(),
trailing: NewTrailingStopOrders(),
}
}
func NewFromProto(log *logging.Logger, p *v1.StopOrders) *Pool {
pool := New(log)
for _, porder := range p.StopOrders {
order := types.NewStopOrderFromProto(porder)
if party, ok := pool.orders[order.Party]; ok {
if _, ok := party[order.ID]; ok {
pool.log.Panic("stop order already exists", logging.String("id", order.ID))
}
} else {
pool.orders[order.Party] = map[string]*types.StopOrder{}
}
pool.orders[order.Party][order.ID] = order
pool.orderToParty[order.ID] = order.Party
}
pool.priced = NewPricedStopOrdersFromProto(p.PricedStopOrders)
pool.trailing = NewTrailingStopOrdersFromProto(p.TrailingStopOrders)
return pool
}
func (p *Pool) ToProto() *v1.StopOrders {
out := &v1.StopOrders{}
for _, v := range p.orders {
for _, order := range v {
out.StopOrders = append(out.StopOrders, order.ToProtoEvent())
}
}
sort.Slice(out.StopOrders, func(i, j int) bool {
return out.StopOrders[i].StopOrder.Id < out.StopOrders[j].StopOrder.Id
})
out.PricedStopOrders = p.priced.ToProto()
out.TrailingStopOrders = p.trailing.ToProto()
return out
}
func (p *Pool) GetStopOrderCount() uint64 {
return uint64(len(p.orderToParty))
}
func (p *Pool) Settled() []*types.StopOrder {
out := []*types.StopOrder{}
for _, v := range p.orders {
for _, so := range v {
so.Status = types.StopOrderStatusStopped
out = append(out, so)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
return out
}
func (p *Pool) PriceUpdated(newPrice *num.Uint) (triggered, cancelled []*types.StopOrder) {
// first update prices and get triggered orders
ids := append(
p.priced.PriceUpdated(newPrice.Clone()),
p.trailing.PriceUpdated(newPrice.Clone())...,
)
// first get all the orders which got triggered
for _, v := range ids {
pid, ok := p.orderToParty[v]
if !ok {
log.Panic("order in tree but not in pool", logging.String("order-id", v))
}
// not needed anymore
delete(p.orderToParty, v)
orders, ok := p.orders[pid]
if !ok {
p.log.Panic("party was expected to have orders but have none",
logging.String("party-id", pid), logging.String("order-id", v))
}
// now we are down to the actual order
sorder, ok := orders[v]
if !ok {
p.log.Panic("party was expected to have an order",
logging.String("party-id", pid), logging.String("order-id", v))
}
sorder.Status = types.StopOrderStatusTriggered
triggered = append(triggered, sorder)
// now we can cleanup
delete(orders, v)
if len(orders) <= 0 {
// we can remove the trader altogether
delete(p.orders, pid)
}
}
// now we get all the OCO oposit to them as they shall
// be cancelled as well
for _, v := range triggered[:] {
if len(v.OCOLinkID) <= 0 {
continue
}
res, err := p.removeWithOCO(v.Party, v.OCOLinkID, false)
if err != nil || len(res) <= 0 {
// that should never happen, this mean for some
// reason that the other side of the OCO has been
// remove and left the pool in a bad state
p.log.Panic("other side of the oco missing from the pool",
logging.Error(err),
logging.PartyID(v.Party),
logging.OrderID(v.OCOLinkID))
}
// only one order returned here
res[0].Status = types.StopOrderStatusStopped
cancelled = append(cancelled, res[0])
}
return triggered, cancelled
}
func (p *Pool) Insert(order *types.StopOrder) {
if party, ok := p.orders[order.Party]; ok {
if _, ok := party[order.ID]; ok {
p.log.Panic("stop order already exists", logging.String("id", order.ID))
}
} else {
p.orders[order.Party] = map[string]*types.StopOrder{}
}
p.orders[order.Party][order.ID] = order
p.orderToParty[order.ID] = order.Party
switch {
case order.Trigger.IsPrice():
p.priced.Insert(order.ID, order.Trigger.Price().Clone(), order.Trigger.Direction)
case order.Trigger.IsTrailingPercenOffset():
p.trailing.Insert(order.ID, order.Trigger.TrailingPercentOffset(), order.Trigger.Direction)
}
}
func (p *Pool) Cancel(
partyID string,
orderID string, // if empty remove all
) ([]*types.StopOrder, error) {
orders, err := p.removeWithOCO(partyID, orderID, true)
if err == nil {
for _, v := range orders {
v.Status = types.StopOrderStatusCancelled
}
}
return orders, err
}
func (p *Pool) removeWithOCO(
partyID string,
orderID string,
withOCO bool, // not always necessary in case we are
) ([]*types.StopOrder, error) {
partyOrders, ok := p.orders[partyID]
if !ok {
// return an error only when trying to find a specific stop order
if len(orderID) > 0 {
return nil, ErrStopOrderNotFound
}
// this party have no stop orders, move on
return nil, nil
}
// remove a single one and maybe OCO
if len(orderID) > 0 {
order, ok := partyOrders[orderID]
if !ok {
return nil, ErrStopOrderNotFound
}
orders := []*types.StopOrder{order}
if withOCO && len(order.OCOLinkID) > 0 {
orders = append(orders, partyOrders[order.OCOLinkID])
}
p.remove(orders)
return orders, nil
}
orders := maps.Values(partyOrders)
sort.Slice(orders, func(i, j int) bool { return orders[i].ID < orders[j].ID })
p.remove(orders)
return orders, nil
}
func (p *Pool) remove(orders []*types.StopOrder) {
for _, order := range orders {
delete(p.orderToParty, order.ID)
delete(p.orders[order.Party], order.ID)
if len(p.orders[order.Party]) <= 0 {
// no need of this entry anymore
delete(p.orders, order.Party)
}
switch {
case order.Trigger.IsPrice():
p.priced.Remove(order.ID)
case order.Trigger.IsTrailingPercenOffset():
p.trailing.Remove(order.ID)
}
}
}
func (p *Pool) RemoveExpired(orderIDs []string) []*types.StopOrder {
ordersM := map[string]*types.StopOrder{}
// first find all orders and add them to the map
for _, id := range orderIDs {
order := p.orders[p.orderToParty[id]][id]
order.Status = types.StopOrderStatusExpired
ordersM[id] = order
// once an order is removed, we also remove it's OCO link
if len(order.OCOLinkID) > 0 {
// first check if it's not been removed already
if _, ok := p.orderToParty[order.OCOLinkID]; ok {
// is the OCO link already mapped
if _, ok := ordersM[order.OCOLinkID]; !ok {
ordersM[order.OCOLinkID] = p.orders[p.orderToParty[id]][order.OCOLinkID]
ordersM[order.OCOLinkID].Status = types.StopOrderStatusExpired
}
}
}
}
orders := maps.Values(ordersM)
sort.Slice(orders, func(i, j int) bool { return orders[i].ID < orders[j].ID })
p.remove(orders)
return orders
}
func (p *Pool) CountForParty(party string) uint64 {
orders, ok := p.orders[party]
if !ok {
return 0
}
return uint64(len(orders))
}