-
Notifications
You must be signed in to change notification settings - Fork 19
/
stop_orders.go
495 lines (439 loc) · 15.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"errors"
"fmt"
"time"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
type StopOrderExpiryStrategy = vega.StopOrder_ExpiryStrategy
const (
// Never valid.
StopOrderExpiryStrategyUnspecified StopOrderExpiryStrategy = vega.StopOrder_EXPIRY_STRATEGY_UNSPECIFIED
// The stop order should be cancelled if the expiry time is reached.
StopOrderExpiryStrategyCancels = vega.StopOrder_EXPIRY_STRATEGY_CANCELS
// The order should be submitted if the expiry time is reached.
StopOrderExpiryStrategySubmit = vega.StopOrder_EXPIRY_STRATEGY_SUBMIT
)
type StopOrderTriggerDirection = vega.StopOrder_TriggerDirection
const (
// Never valid.
StopOrderTriggerDirectionUnspecified StopOrderTriggerDirection = vega.StopOrder_TRIGGER_DIRECTION_UNSPECIFIED
// The stop order is triggered once the price falls below a certain level.
StopOrderTriggerDirectionFallsBelow = vega.StopOrder_TRIGGER_DIRECTION_FALLS_BELOW
// The stop order is triggered once the price rises above a certain level.
StopOrderTriggerDirectionRisesAbove = vega.StopOrder_TRIGGER_DIRECTION_RISES_ABOVE
)
type StopOrderStatus = vega.StopOrder_Status
const (
// Never valid.
StopOrderStatusUnspecified StopOrderStatus = vega.StopOrder_STATUS_UNSPECIFIED
// Pending to be executed once the trigger is breached.
StopOrderStatusPending = vega.StopOrder_STATUS_PENDING
// Cancelled by the user.
StopOrderStatusCancelled = vega.StopOrder_STATUS_CANCELLED
// Stopped by the network, e.g: OCO other side has been triggered.
StopOrderStatusStopped = vega.StopOrder_STATUS_STOPPED
// Stop order has been triggered and generated an order.
StopOrderStatusTriggered = vega.StopOrder_STATUS_TRIGGERED
// Stop order has expired.
StopOrderStatusExpired = vega.StopOrder_STATUS_EXPIRED
// Stop order was rejected at submission.
StopOrderStatusRejected = vega.StopOrder_STATUS_REJECTED
)
type StopOrderSizeOverrideSetting = vega.StopOrder_SizeOverrideSetting
const (
// Never valid.
StopOrderSizeOverrideSettingUnspecified StopOrderSizeOverrideSetting = vega.StopOrder_SIZE_OVERRIDE_SETTING_UNSPECIFIED
// No size override is used.
StopOrderSizeOverrideSettingNone = vega.StopOrder_SIZE_OVERRIDE_SETTING_NONE
// Use the position size of the trader to override the order size.
StopOrderSizeOverrideSettingPosition = vega.StopOrder_SIZE_OVERRIDE_SETTING_POSITION
)
type StopOrderRejectionReason = vega.StopOrder_RejectionReason
const (
// Never valid.
StopOrderRejectionUnspecified StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_UNSPECIFIED
StopOrderRejectionTradingNotAllowed StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_TRADING_NOT_ALLOWED
StopOrderRejectionExpiryInThePast StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_EXPIRY_IN_THE_PAST
StopOrderRejectionMustBeReduceOnly StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_MUST_BE_REDUCE_ONLY
StopOrderRejectionMaxStopOrdersPerPartyReached StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_MAX_STOP_ORDERS_PER_PARTY_REACHED
StopOrderRejectionNotAllowedWithoutAPosition StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_STOP_ORDER_NOT_ALLOWED_WITHOUT_A_POSITION
StopOrderRejectionNotClosingThePosition StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_STOP_ORDER_NOT_CLOSING_THE_POSITION
StopOrderRejectionLinkedPercentageInvalid StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_STOP_ORDER_LINKED_PERCENTAGE_INVALID
StopOrderRejectionNotAllowedDuringOpeningAuction StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_STOP_ORDER_NOT_ALLOWED_DURING_OPENING_AUCTION
StopOrderRejectionOCONotAllowedSameExpiryTime StopOrderRejectionReason = vega.StopOrder_REJECTION_REASON_STOP_ORDER_CANNOT_MATCH_OCO_EXPIRY_TIMES
)
type StopOrderExpiry struct {
ExpiresAt *time.Time
ExpiryStrategy *StopOrderExpiryStrategy
}
func (s StopOrderExpiry) String() string {
return fmt.Sprintf(
"expiresAt(%v) expiryStrategy(%v)",
s.ExpiresAt,
s.ExpiryStrategy,
)
}
func (s *StopOrderExpiry) Expires() bool {
return s.ExpiresAt != nil
}
type StopOrderSizeOverrideValue struct {
PercentageSize num.Decimal
}
type StopOrderTrigger struct {
Direction StopOrderTriggerDirection
price *num.Uint
trailingPercentOffset num.Decimal
}
func NewPriceStopOrderTrigger(
direction StopOrderTriggerDirection,
price *num.Uint,
) *StopOrderTrigger {
return &StopOrderTrigger{
Direction: direction,
price: price,
}
}
func NewTrailingStopOrderTrigger(
direction StopOrderTriggerDirection,
trailingPercentOffset num.Decimal,
) *StopOrderTrigger {
return &StopOrderTrigger{
Direction: direction,
trailingPercentOffset: trailingPercentOffset,
}
}
func (s StopOrderTrigger) String() string {
return fmt.Sprintf(
"price(%v) trailingPercentOffset(%v)",
s.price,
s.trailingPercentOffset,
)
}
func (s *StopOrderTrigger) IsPrice() bool {
return s.price != nil
}
func (s *StopOrderTrigger) IsTrailingPercentOffset() bool {
return s.price == nil
}
func (s *StopOrderTrigger) Price() *num.Uint {
if s.price == nil {
panic("invalid use of price trigger")
}
return s.price.Clone()
}
func (s *StopOrderTrigger) TrailingPercentOffset() num.Decimal {
if s.price != nil {
panic("invalid use of trailing percent offset trigger")
}
return s.trailingPercentOffset
}
type StopOrderSetup struct {
OrderSubmission *OrderSubmission
Expiry *StopOrderExpiry
Trigger *StopOrderTrigger
SizeOverrideSetting StopOrderSizeOverrideSetting
SizeOverrideValue *StopOrderSizeOverrideValue
}
func (s StopOrderSetup) String() string {
return fmt.Sprintf(
"orderSubmission(%v) expiry(%v) trigger(%v)",
s.OrderSubmission.String(),
s.Expiry.String(),
s.Trigger.String(),
)
}
func StopOrderSetupFromProto(
psetup *commandspb.StopOrderSetup,
direction StopOrderTriggerDirection,
) (*StopOrderSetup, error) {
orderSubmission, err := NewOrderSubmissionFromProto(psetup.OrderSubmission)
if err != nil {
return nil, err
}
trigger := &StopOrderTrigger{
Direction: direction,
}
switch t := psetup.Trigger.(type) {
case *commandspb.StopOrderSetup_Price:
var overflow bool
// checking here, but seeing that the payload have been validated down
// the line there's little to no changes this is invalid
if trigger.price, overflow = num.UintFromString(t.Price, 10); overflow {
return nil, errors.New("invalid trigger price")
}
case *commandspb.StopOrderSetup_TrailingPercentOffset:
var err error
// same stuff here
if trigger.trailingPercentOffset, err = num.DecimalFromString(t.TrailingPercentOffset); err != nil {
return nil, err
}
}
expiry := &StopOrderExpiry{}
if psetup.ExpiresAt != nil {
expiry.ExpiresAt = ptr.From(time.Unix(0, *psetup.ExpiresAt))
expiry.ExpiryStrategy = psetup.ExpiryStrategy
}
var sizeOverrideValue StopOrderSizeOverrideValue
var sizeOverrideSetting StopOrderSizeOverrideSetting = vega.StopOrder_SIZE_OVERRIDE_SETTING_UNSPECIFIED
if psetup.SizeOverrideValue != nil {
value, err := num.DecimalFromString(psetup.SizeOverrideValue.Percentage)
if err != nil {
return nil, err
}
sizeOverrideValue = StopOrderSizeOverrideValue{PercentageSize: value}
}
if psetup.SizeOverrideSetting != nil {
sizeOverrideSetting = *psetup.SizeOverrideSetting
}
return &StopOrderSetup{
OrderSubmission: orderSubmission,
Expiry: expiry,
Trigger: trigger,
SizeOverrideSetting: sizeOverrideSetting,
SizeOverrideValue: &sizeOverrideValue,
}, nil
}
type StopOrdersSubmission struct {
RisesAbove *StopOrderSetup
FallsBelow *StopOrderSetup
}
func NewStopOrderSubmissionFromProto(psubmission *commandspb.StopOrdersSubmission) (*StopOrdersSubmission, error) {
var (
fallsBelow, risesAbove *StopOrderSetup
err error
)
if psubmission.FallsBelow != nil {
if fallsBelow, err = StopOrderSetupFromProto(psubmission.FallsBelow, StopOrderTriggerDirectionFallsBelow); err != nil {
return nil, err
}
}
if psubmission.RisesAbove != nil {
if risesAbove, err = StopOrderSetupFromProto(psubmission.RisesAbove, StopOrderTriggerDirectionRisesAbove); err != nil {
return nil, err
}
}
return &StopOrdersSubmission{
FallsBelow: fallsBelow,
RisesAbove: risesAbove,
}, nil
}
func (s *StopOrdersSubmission) IntoStopOrders(
party, fallsBelowID, risesAboveID string,
now time.Time,
) (fallsBelow, risesAbove *StopOrder) {
if s.RisesAbove != nil {
risesAbove = &StopOrder{
ID: risesAboveID,
Party: party,
Market: s.RisesAbove.OrderSubmission.MarketID,
OrderSubmission: s.RisesAbove.OrderSubmission,
OCOLinkID: fallsBelowID,
Expiry: s.RisesAbove.Expiry,
Trigger: s.RisesAbove.Trigger,
Status: StopOrderStatusPending,
CreatedAt: now,
UpdatedAt: now,
SizeOverrideSetting: s.RisesAbove.SizeOverrideSetting,
SizeOverrideValue: s.RisesAbove.SizeOverrideValue,
}
}
if s.FallsBelow != nil {
fallsBelow = &StopOrder{
ID: fallsBelowID,
Party: party,
Market: s.FallsBelow.OrderSubmission.MarketID,
OrderSubmission: s.FallsBelow.OrderSubmission,
OCOLinkID: risesAboveID,
Expiry: s.FallsBelow.Expiry,
Trigger: s.FallsBelow.Trigger,
Status: StopOrderStatusPending,
CreatedAt: now,
UpdatedAt: now,
SizeOverrideSetting: s.FallsBelow.SizeOverrideSetting,
SizeOverrideValue: s.FallsBelow.SizeOverrideValue,
}
}
return fallsBelow, risesAbove
}
func (s StopOrdersSubmission) String() string {
rises, falls := "nil", "nil"
if s.RisesAbove != nil {
rises = s.RisesAbove.String()
}
if s.FallsBelow != nil {
falls = s.FallsBelow.String()
}
return fmt.Sprintf(
"risesAbove(%v) fallsBelow(%v)",
rises,
falls,
)
}
type StopOrder struct {
ID string
Party string
Market string
OrderSubmission *OrderSubmission
OrderID string
OCOLinkID string
Expiry *StopOrderExpiry
Trigger *StopOrderTrigger
Status StopOrderStatus
CreatedAt time.Time
UpdatedAt time.Time
RejectionReason *StopOrderRejectionReason
SizeOverrideSetting StopOrderSizeOverrideSetting
SizeOverrideValue *StopOrderSizeOverrideValue
}
func (s *StopOrder) String() string {
rejectionReason := "nil"
if s.RejectionReason != nil {
rejectionReason = s.RejectionReason.String()
}
return fmt.Sprintf(
"id(%v) party(%v) market(%v) orderSubmission(%v) orderId(%v) ocoLink(%v) expiry(%v) trigger(%v) status(%v) createdAt(%v) updatedAt(%v) rejectionReason(%v)",
s.ID,
s.Party,
s.Market,
s.OrderSubmission.String(),
s.OrderID,
s.OCOLinkID,
s.Expiry.String(),
s.Trigger.String(),
s.Status,
s.CreatedAt,
s.UpdatedAt,
rejectionReason,
)
}
func NewStopOrderFromProto(p *eventspb.StopOrderEvent) *StopOrder {
sub, err := NewOrderSubmissionFromProto(p.Submission)
if err != nil {
panic("submission should always be valid here")
}
trigger := &StopOrderTrigger{
Direction: p.StopOrder.TriggerDirection,
}
switch t := p.StopOrder.Trigger.(type) {
case *vega.StopOrder_Price:
var overflow bool
// checking here, but seeing that the payload have been validated down
// the line there's little to no changes this is invalid
if trigger.price, overflow = num.UintFromString(t.Price, 10); overflow {
panic("invalid trigger price")
}
case *vega.StopOrder_TrailingPercentOffset:
var err error
// same stuff here
if trigger.trailingPercentOffset, err = num.DecimalFromString(t.TrailingPercentOffset); err != nil {
panic(err)
}
}
expiry := &StopOrderExpiry{}
if p.StopOrder.ExpiresAt != nil {
expiry.ExpiresAt = ptr.From(time.Unix(0, *p.StopOrder.ExpiresAt))
expiry.ExpiryStrategy = p.StopOrder.ExpiryStrategy
}
var sizeOverride *StopOrderSizeOverrideValue
if p.StopOrder.SizeOverrideSetting == StopOrderSizeOverrideSettingPosition {
value, err := num.DecimalFromString(p.StopOrder.SizeOverrideValue.GetPercentage())
if err != nil {
panic(err)
}
sizeOverride = &StopOrderSizeOverrideValue{PercentageSize: value}
}
return &StopOrder{
ID: p.StopOrder.Id,
Party: p.StopOrder.PartyId,
Market: p.StopOrder.MarketId,
OrderID: p.StopOrder.OrderId,
OCOLinkID: ptr.UnBox(p.StopOrder.OcoLinkId),
Status: p.StopOrder.Status,
CreatedAt: time.Unix(0, p.StopOrder.CreatedAt),
UpdatedAt: time.Unix(0, ptr.UnBox(p.StopOrder.UpdatedAt)),
OrderSubmission: sub,
Trigger: trigger,
Expiry: expiry,
RejectionReason: p.StopOrder.RejectionReason,
SizeOverrideSetting: p.StopOrder.SizeOverrideSetting,
SizeOverrideValue: sizeOverride,
}
}
func (s *StopOrder) ToProtoEvent() *eventspb.StopOrderEvent {
var updatedAt *int64
if s.UpdatedAt != (time.Time{}) {
updatedAt = ptr.From(s.UpdatedAt.UnixNano())
}
var ocoLinkID *string
if len(s.OCOLinkID) > 0 {
ocoLinkID = ptr.From(s.OCOLinkID)
}
ev := &eventspb.StopOrderEvent{
Submission: s.OrderSubmission.IntoProto(),
StopOrder: &vega.StopOrder{
Id: s.ID,
PartyId: s.Party,
MarketId: s.Market,
OrderId: s.OrderID,
OcoLinkId: ocoLinkID,
Status: s.Status,
CreatedAt: s.CreatedAt.UnixNano(),
UpdatedAt: updatedAt,
TriggerDirection: s.Trigger.Direction,
RejectionReason: s.RejectionReason,
},
}
if s.Expiry.Expires() {
ev.StopOrder.ExpiresAt = ptr.From(s.Expiry.ExpiresAt.UnixNano())
ev.StopOrder.ExpiryStrategy = s.Expiry.ExpiryStrategy
}
switch {
case s.Trigger.IsPrice():
ev.StopOrder.Trigger = &vega.StopOrder_Price{
Price: s.Trigger.Price().String(),
}
case s.Trigger.IsTrailingPercentOffset():
ev.StopOrder.Trigger = &vega.StopOrder_TrailingPercentOffset{
TrailingPercentOffset: s.Trigger.TrailingPercentOffset().String(),
}
}
return ev
}
type StopOrdersCancellation struct {
MarketID string
OrderID string
}
func NewStopOrderCancellationFromProto(
soc *commandspb.StopOrdersCancellation,
) *StopOrdersCancellation {
return &StopOrdersCancellation{
MarketID: ptr.UnBox(soc.MarketId),
OrderID: ptr.UnBox(soc.StopOrderId),
}
}
func (s StopOrdersCancellation) String() string {
return fmt.Sprintf(
"marketID(%v) orderID(%v)",
s.MarketID,
s.OrderID,
)
}