-
Notifications
You must be signed in to change notification settings - Fork 19
/
matching.go
936 lines (844 loc) · 31 KB
/
matching.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
// 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/libs/stringer"
proto "code.vegaprotocol.io/vega/protos/vega"
)
type Order struct {
ID string
MarketID string
Party string
Side Side
Price *num.Uint
OriginalPrice *num.Uint
Size uint64
Remaining uint64
TimeInForce OrderTimeInForce
Type OrderType
CreatedAt int64
Status OrderStatus
ExpiresAt int64
Reference string
Reason OrderError
UpdatedAt int64
Version uint64
BatchID uint64
PeggedOrder *PeggedOrder
PostOnly bool
ReduceOnly bool
extraRemaining uint64
IcebergOrder *IcebergOrder
}
func (o *Order) ReduceOnlyAdjustRemaining(extraSize uint64) {
if !o.ReduceOnly {
panic("order.ReduceOnlyAdjustSize shall be call only on reduce-only orders")
}
o.extraRemaining = extraSize
o.Remaining -= extraSize
}
func (o *Order) ClearUpExtraRemaining() {
// ignore for non reduce only
if !o.ReduceOnly {
return
}
o.Remaining += o.extraRemaining
o.extraRemaining = 0
}
// TrueRemaining is the full remaining size of an order. If this is an iceberg order
// it will return the visible peak + the hidden volume.
func (o *Order) TrueRemaining() uint64 {
rem := o.Remaining
if o.IcebergOrder != nil {
rem += o.IcebergOrder.ReservedRemaining
}
return rem
}
// IcebergNeedsRefresh returns whether the given iceberg order's visible peak has
// dropped below the minimum visible size, and there is hidden volume available to
// restore it.
func (o *Order) IcebergNeedsRefresh() bool {
if o.IcebergOrder == nil {
// not an iceberg
return false
}
if o.IcebergOrder.ReservedRemaining == 0 {
// nothing to refresh with
return false
}
if o.Remaining >= o.IcebergOrder.MinimumVisibleSize {
// not under the minimum
return false
}
return true
}
// SetIcebergPeaks will restore the given iceberg orders visible size with
// some of its hidden volume.
func (o *Order) SetIcebergPeaks() {
if o.IcebergOrder == nil {
return
}
if o.Remaining > o.IcebergOrder.PeakSize && o.IcebergOrder.ReservedRemaining == 0 {
// iceberg is at full volume and so set its visible amount to its peak size
peak := num.MinV(o.Remaining, o.IcebergOrder.PeakSize)
o.IcebergOrder.ReservedRemaining = o.Remaining - peak
o.Remaining = peak
return
}
// calculate the refill amount
refill := o.IcebergOrder.PeakSize - o.Remaining
refill = num.MinV(refill, o.IcebergOrder.ReservedRemaining)
o.Remaining += refill
o.IcebergOrder.ReservedRemaining -= refill
}
func (o Order) IntoSubmission() *OrderSubmission {
sub := &OrderSubmission{
MarketID: o.MarketID,
Size: o.Size,
Side: o.Side,
TimeInForce: o.TimeInForce,
ExpiresAt: o.ExpiresAt,
Type: o.Type,
Reference: o.Reference,
PostOnly: o.PostOnly,
ReduceOnly: o.ReduceOnly,
}
if o.IcebergOrder != nil {
sub.IcebergOrder = &IcebergOrder{
PeakSize: o.IcebergOrder.PeakSize,
MinimumVisibleSize: o.IcebergOrder.MinimumVisibleSize,
}
}
if o.Price != nil {
sub.Price = o.Price.Clone()
}
if o.PeggedOrder != nil {
sub.PeggedOrder = o.PeggedOrder.Clone()
}
return sub
}
func (o Order) Clone() *Order {
cpy := o
if o.Price != nil {
cpy.Price = o.Price.Clone()
} else {
cpy.Price = num.UintZero()
}
// this isn't really needed, to original order is about to be replaced, or the original price is getting reassinged
// but in case something goes wrong, we don't want a pointer to this field in 2 places
if o.OriginalPrice != nil {
cpy.OriginalPrice = o.OriginalPrice.Clone()
}
if o.PeggedOrder != nil {
cpy.PeggedOrder = o.PeggedOrder.Clone()
}
if o.IcebergOrder != nil {
cpy.IcebergOrder = o.IcebergOrder.Clone()
}
return &cpy
}
func (o Order) String() string {
return fmt.Sprintf(
"ID(%s) marketID(%s) party(%s) side(%s) price(%s) size(%v) remaining(%v) timeInForce(%s) type(%s) status(%s) reference(%s) reason(%s) version(%v) batchID(%v) createdAt(%v) updatedAt(%v) expiresAt(%v) originalPrice(%s) peggedOrder(%s) postOnly(%v) reduceOnly(%v) iceberg(%s)",
o.ID,
o.MarketID,
o.Party,
o.Side.String(),
o.Price.String(),
o.Size,
o.Remaining,
o.TimeInForce.String(),
o.Type.String(),
o.Status.String(),
o.Reference,
o.Reason.String(),
o.Version,
o.BatchID,
o.CreatedAt,
o.UpdatedAt,
o.ExpiresAt,
stringer.UintPointerToString(o.OriginalPrice),
stringer.ReflectPointerToString(o.PeggedOrder),
o.PostOnly,
o.ReduceOnly,
stringer.ReflectPointerToString(o.IcebergOrder),
)
}
type Orders []*Order
func (o Orders) IntoProto() []*proto.Order {
out := make([]*proto.Order, 0, len(o))
for _, v := range o {
out = append(out, v.IntoProto())
}
return out
}
func (o *Order) IntoProto() *proto.Order {
var pegged *proto.PeggedOrder
if o.PeggedOrder != nil {
pegged = o.PeggedOrder.IntoProto()
}
var reason *OrderError
if o.Reason != OrderErrorUnspecified {
reason = ptr.From(o.Reason)
}
var iceberg *proto.IcebergOrder
if o.IcebergOrder != nil {
iceberg = o.IcebergOrder.IntoProto()
}
return &proto.Order{
Id: o.ID,
MarketId: o.MarketID,
PartyId: o.Party,
Side: o.Side,
Price: num.UintToString(o.Price),
Size: o.Size,
Remaining: o.Remaining,
TimeInForce: o.TimeInForce,
Type: o.Type,
CreatedAt: o.CreatedAt,
Status: o.Status,
ExpiresAt: o.ExpiresAt,
Reference: o.Reference,
Reason: reason,
UpdatedAt: o.UpdatedAt,
Version: o.Version,
BatchId: o.BatchID,
PeggedOrder: pegged,
PostOnly: o.PostOnly,
ReduceOnly: o.ReduceOnly,
IcebergOrder: iceberg,
}
}
func OrderFromProto(o *proto.Order) (*Order, error) {
var iceberg *IcebergOrder
if o.IcebergOrder != nil {
var err error
iceberg, err = NewIcebergOrderFromProto(o.IcebergOrder)
if err != nil {
return nil, err
}
}
var pegged *PeggedOrder
if o.PeggedOrder != nil {
var err error
pegged, err = NewPeggedOrderFromProto(o.PeggedOrder)
if err != nil {
return nil, err
}
}
price := num.UintZero()
if len(o.Price) > 0 {
var overflowed bool
price, overflowed = num.UintFromString(o.Price, 10)
if overflowed {
return nil, errors.New("invalid price")
}
}
reason := OrderErrorUnspecified
if o.Reason != nil {
reason = *o.Reason
}
return &Order{
ID: o.Id,
MarketID: o.MarketId,
Party: o.PartyId,
Side: o.Side,
Price: price,
Size: o.Size,
Remaining: o.Remaining,
TimeInForce: o.TimeInForce,
Type: o.Type,
CreatedAt: o.CreatedAt,
Status: o.Status,
ExpiresAt: o.ExpiresAt,
Reference: o.Reference,
Reason: reason,
UpdatedAt: o.UpdatedAt,
Version: o.Version,
BatchID: o.BatchId,
PeggedOrder: pegged,
PostOnly: o.PostOnly,
ReduceOnly: o.ReduceOnly,
IcebergOrder: iceberg,
}, nil
}
// Create sets the creation time (CreatedAt) to t and returns the
// updated order.
func (o *Order) Create(t int64) *Order {
o.CreatedAt = t
return o
}
// Update sets the modification time (UpdatedAt) to t and returns the
// updated order.
func (o *Order) Update(t time.Time) *Order {
o.UpdatedAt = t.UnixNano()
return o
}
// IsPersistent returns true if the order is persistent.
// A persistent order is a Limit type order that might be
// matched in the future.
func (o *Order) IsPersistent() bool {
return (o.TimeInForce == OrderTimeInForceGTC ||
o.TimeInForce == OrderTimeInForceGTT ||
o.TimeInForce == OrderTimeInForceGFN ||
o.TimeInForce == OrderTimeInForceGFA) &&
o.Type == OrderTypeLimit &&
o.Remaining > 0
}
func (o *Order) IsExpireable() bool {
return (o.TimeInForce == OrderTimeInForceGFN ||
o.TimeInForce == OrderTimeInForceGTT ||
o.TimeInForce == OrderTimeInForceGFA) &&
o.ExpiresAt > 0
}
// IsFinished returns true if an order
// is in any state different to ACTIVE and PARKED
// Basically any order which is never gonna
// trade anymore.
func (o *Order) IsFinished() bool {
return o.Status != OrderStatusActive && o.Status != OrderStatusParked
}
func (o *Order) HasTraded() bool {
return o.Size != o.Remaining
}
func (o *Order) applyOrderAmendmentSizeIceberg(delta int64) {
// handle increase in size
if delta > 0 {
o.Size += uint64(delta)
o.IcebergOrder.ReservedRemaining += uint64(delta)
return
}
// handle decrease in size
dec := uint64(-delta)
o.Size -= dec
if o.IcebergOrder.ReservedRemaining >= dec {
o.IcebergOrder.ReservedRemaining -= dec
return
}
diff := dec - o.IcebergOrder.ReservedRemaining
if o.Remaining > diff {
o.Remaining -= dec - o.IcebergOrder.ReservedRemaining
} else {
o.Remaining = 0
}
o.IcebergOrder.ReservedRemaining = 0
}
// applyOrderAmendmentSizeDelta update the orders size/remaining fields based on the size an direction of the given delta.
func (o *Order) applyOrderAmendmentSizeDelta(delta int64) {
if o.IcebergOrder != nil {
o.applyOrderAmendmentSizeIceberg(delta)
return
}
// handle size increase
if delta > 0 {
o.Size += uint64(delta)
o.Remaining += uint64(delta)
return
}
// handle size decrease
dec := uint64(-delta)
o.Size -= dec
if o.Remaining > dec {
o.Remaining -= dec
} else {
o.Remaining = 0
}
}
// ApplyOrderAmendment assumes the amendment have been validated before.
func (o *Order) ApplyOrderAmendment(amendment *OrderAmendment, updatedAtNano int64, priceFactor *num.Uint) (order *Order, err error) {
order = o.Clone()
order.UpdatedAt = updatedAtNano
order.Version++
if o.PeggedOrder != nil {
order.PeggedOrder = &PeggedOrder{
Reference: o.PeggedOrder.Reference,
Offset: o.PeggedOrder.Offset,
}
}
var amendPrice *num.Uint
if amendment.Price != nil {
amendPrice = amendment.Price.Clone()
amendPrice.Mul(amendPrice, priceFactor)
}
// apply price changes
if amendment.Price != nil && o.Price.NEQ(amendPrice) {
order.Price = amendPrice.Clone()
order.OriginalPrice = amendment.Price.Clone()
}
// apply size changes
if delta := amendment.SizeDelta; delta != 0 {
order.applyOrderAmendmentSizeDelta(delta)
}
// apply tif
if amendment.TimeInForce != OrderTimeInForceUnspecified {
order.TimeInForce = amendment.TimeInForce
if amendment.TimeInForce != OrderTimeInForceGTT {
order.ExpiresAt = 0
}
}
if amendment.ExpiresAt != nil {
order.ExpiresAt = *amendment.ExpiresAt
}
// apply pegged order values
if order.PeggedOrder != nil {
if amendment.PeggedOffset != nil {
order.PeggedOrder.Offset = amendment.PeggedOffset.Clone()
}
if amendment.PeggedReference != PeggedReferenceUnspecified {
order.PeggedOrder.Reference = amendment.PeggedReference
}
if verr := order.ValidatePeggedOrder(); verr != OrderErrorUnspecified {
err = verr
}
}
return order, err
}
func (order *Order) ValidatePeggedOrder() OrderError {
if order.Type != OrderTypeLimit {
// All pegged orders must be LIMIT orders
return ErrPeggedOrderMustBeLimitOrder
}
if order.TimeInForce != OrderTimeInForceGTT && order.TimeInForce != OrderTimeInForceGTC && order.TimeInForce != OrderTimeInForceGFN {
// Pegged orders can only be GTC or GTT
return ErrPeggedOrderMustBeGTTOrGTC
}
if order.PeggedOrder.Reference == PeggedReferenceUnspecified {
// We must specify a valid reference
return ErrPeggedOrderWithoutReferencePrice
}
if order.Side == SideBuy {
switch order.PeggedOrder.Reference {
case PeggedReferenceBestAsk:
return ErrPeggedOrderBuyCannotReferenceBestAskPrice
case PeggedReferenceMid:
if order.PeggedOrder.Offset.IsZero() {
return ErrPeggedOrderOffsetMustBeGreaterThanZero
}
}
} else {
switch order.PeggedOrder.Reference {
case PeggedReferenceBestBid:
return ErrPeggedOrderSellCannotReferenceBestBidPrice
case PeggedReferenceMid:
if order.PeggedOrder.Offset.IsZero() {
return ErrPeggedOrderOffsetMustBeGreaterThanZero
}
}
}
return OrderErrorUnspecified
}
type PeggedOrder struct {
Reference PeggedReference
Offset *num.Uint
}
func (p PeggedOrder) Clone() *PeggedOrder {
cpy := p
return &cpy
}
func NewPeggedOrderFromProto(p *proto.PeggedOrder) (*PeggedOrder, error) {
if p == nil {
return nil, nil
}
offset, overflowed := num.UintFromString(p.Offset, 10)
if overflowed {
return nil, errors.New("invalid offset")
}
return &PeggedOrder{
Reference: p.Reference,
Offset: offset,
}, nil
}
func (p PeggedOrder) IntoProto() *proto.PeggedOrder {
return &proto.PeggedOrder{
Reference: p.Reference,
Offset: p.Offset.String(),
}
}
func (p PeggedOrder) String() string {
return fmt.Sprintf(
"reference(%s) offset(%s)",
p.Reference.String(),
stringer.UintPointerToString(p.Offset),
)
}
type IcebergOrder struct {
ReservedRemaining uint64
PeakSize uint64
MinimumVisibleSize uint64
}
func (i IcebergOrder) Clone() *IcebergOrder {
cpy := i
return &cpy
}
func NewIcebergOrderFromProto(i *proto.IcebergOrder) (*IcebergOrder, error) {
if i == nil {
return nil, nil
}
return &IcebergOrder{
ReservedRemaining: i.ReservedRemaining,
PeakSize: i.PeakSize,
MinimumVisibleSize: i.MinimumVisibleSize,
}, nil
}
func (i IcebergOrder) IntoProto() *proto.IcebergOrder {
return &proto.IcebergOrder{
ReservedRemaining: i.ReservedRemaining,
PeakSize: i.PeakSize,
MinimumVisibleSize: i.MinimumVisibleSize,
}
}
func (i IcebergOrder) String() string {
return fmt.Sprintf(
"reserved-remaining(%d) initial-peak-size(%d) minimum-peak-size(%d)",
i.ReservedRemaining,
i.PeakSize,
i.MinimumVisibleSize,
)
}
type OrderConfirmation struct {
Order *Order
Trades []*Trade
PassiveOrdersAffected []*Order
}
func (o *OrderConfirmation) IntoProto() *proto.OrderConfirmation {
return &proto.OrderConfirmation{
Order: o.Order.IntoProto(),
Trades: Trades(o.Trades).IntoProto(),
PassiveOrdersAffected: Orders(o.PassiveOrdersAffected).IntoProto(),
}
}
func (o OrderConfirmation) TradedValue() *num.Uint {
total := num.UintZero()
for _, t := range o.Trades {
size := num.NewUint(t.Size)
total.AddSum(size.Mul(size, t.Price))
}
return total
}
type OrderCancellationConfirmation struct {
Order *Order
}
func (o *OrderCancellationConfirmation) IntoProto() *proto.OrderCancellationConfirmation {
return &proto.OrderCancellationConfirmation{
Order: o.Order.IntoProto(),
}
}
type Trade struct {
ID string
MarketID string
Price *num.Uint
MarketPrice *num.Uint
Size uint64
Buyer string
Seller string
Aggressor Side
BuyOrder string
SellOrder string
Timestamp int64
Type TradeType
BuyerFee *Fee
SellerFee *Fee
BuyerAuctionBatch uint64
SellerAuctionBatch uint64
}
func (t *Trade) SetIDs(tradeID string, aggressive, passive *Order) {
t.ID = tradeID
if aggressive.Side == SideBuy {
t.BuyOrder = aggressive.ID
t.SellOrder = passive.ID
return
}
t.SellOrder = aggressive.ID
t.BuyOrder = passive.ID
}
func (t *Trade) IntoProto() *proto.Trade {
var buyerFee, sellerFee *proto.Fee
if t.BuyerFee != nil {
buyerFee = t.BuyerFee.IntoProto()
}
if t.SellerFee != nil {
sellerFee = t.SellerFee.IntoProto()
}
return &proto.Trade{
Id: t.ID,
MarketId: t.MarketID,
Price: num.UintToString(t.Price),
Size: t.Size,
Buyer: t.Buyer,
Seller: t.Seller,
Aggressor: t.Aggressor,
BuyOrder: t.BuyOrder,
SellOrder: t.SellOrder,
Timestamp: t.Timestamp,
Type: t.Type,
BuyerFee: buyerFee,
SellerFee: sellerFee,
BuyerAuctionBatch: t.BuyerAuctionBatch,
SellerAuctionBatch: t.SellerAuctionBatch,
}
}
func (t Trade) String() string {
return fmt.Sprintf(
"ID(%s) marketID(%s) price(%s) marketPrice(%s) size(%v) buyer(%s) seller(%s) aggressor(%s) buyOrder(%s) sellOrder(%s) timestamp(%v) type(%s) buyerAuctionBatch(%v) sellerAuctionBatch(%v) buyerFee(%s) sellerFee(%s)",
t.ID,
t.MarketID,
stringer.UintPointerToString(t.Price),
stringer.UintPointerToString(t.MarketPrice),
t.Size,
t.Buyer,
t.Seller,
t.Aggressor.String(),
t.BuyOrder,
t.SellOrder,
t.Timestamp,
t.Type.String(),
t.BuyerAuctionBatch,
t.SellerAuctionBatch,
stringer.ReflectPointerToString(t.SellerFee),
stringer.ReflectPointerToString(t.BuyerFee),
)
}
type Trades []*Trade
func (t Trades) IntoProto() []*proto.Trade {
out := make([]*proto.Trade, 0, len(t))
for _, v := range t {
out = append(out, v.IntoProto())
}
return out
}
type TradeType = proto.Trade_Type
const (
// Default value, always invalid.
TradeTypeUnspecified TradeType = proto.Trade_TYPE_UNSPECIFIED
// Normal trading between two parties.
TradeTypeDefault TradeType = proto.Trade_TYPE_DEFAULT
// Trading initiated by the network with another party on the book,
// which helps to zero-out the positions of one or more distressed parties.
TradeTypeNetworkCloseOutGood TradeType = proto.Trade_TYPE_NETWORK_CLOSE_OUT_GOOD
// Trading initiated by the network with another party off the book,
// with a distressed party in order to zero-out the position of the party.
TradeTypeNetworkCloseOutBad TradeType = proto.Trade_TYPE_NETWORK_CLOSE_OUT_BAD
)
type PeggedReference = proto.PeggedReference
const (
// Default value for PeggedReference, no reference given.
PeggedReferenceUnspecified PeggedReference = proto.PeggedReference_PEGGED_REFERENCE_UNSPECIFIED
// Mid price reference.
PeggedReferenceMid PeggedReference = proto.PeggedReference_PEGGED_REFERENCE_MID
// Best bid price reference.
PeggedReferenceBestBid PeggedReference = proto.PeggedReference_PEGGED_REFERENCE_BEST_BID
// Best ask price reference.
PeggedReferenceBestAsk PeggedReference = proto.PeggedReference_PEGGED_REFERENCE_BEST_ASK
)
type OrderStatus = proto.Order_Status
const (
// Default value, always invalid.
OrderStatusUnspecified OrderStatus = proto.Order_STATUS_UNSPECIFIED
// Used for active unfilled or partially filled orders.
OrderStatusActive OrderStatus = proto.Order_STATUS_ACTIVE
// Used for expired GTT orders.
OrderStatusExpired OrderStatus = proto.Order_STATUS_EXPIRED
// Used for orders cancelled by the party that created the order.
OrderStatusCancelled OrderStatus = proto.Order_STATUS_CANCELLED
// Used for unfilled FOK or IOC orders, and for orders that were stopped by the network.
OrderStatusStopped OrderStatus = proto.Order_STATUS_STOPPED
// Used for closed fully filled orders.
OrderStatusFilled OrderStatus = proto.Order_STATUS_FILLED
// Used for orders when not enough collateral was available to fill the margin requirements.
OrderStatusRejected OrderStatus = proto.Order_STATUS_REJECTED
// Used for closed partially filled IOC orders.
OrderStatusPartiallyFilled OrderStatus = proto.Order_STATUS_PARTIALLY_FILLED
// Order has been removed from the order book and has been parked, this applies to pegged orders only.
OrderStatusParked OrderStatus = proto.Order_STATUS_PARKED
)
type Side = proto.Side
const (
// Default value, always invalid.
SideUnspecified Side = proto.Side_SIDE_UNSPECIFIED
// Buy order.
SideBuy Side = proto.Side_SIDE_BUY
// Sell order.
SideSell Side = proto.Side_SIDE_SELL
)
type OrderType = proto.Order_Type
const (
// Default value, always invalid.
OrderTypeUnspecified OrderType = proto.Order_TYPE_UNSPECIFIED
// Used for Limit orders.
OrderTypeLimit OrderType = proto.Order_TYPE_LIMIT
// Used for Market orders.
OrderTypeMarket OrderType = proto.Order_TYPE_MARKET
// Used for orders where the initiating party is the network (with distressed traders).
OrderTypeNetwork OrderType = proto.Order_TYPE_NETWORK
)
type OrderTimeInForce = proto.Order_TimeInForce
const (
// Default value for TimeInForce, can be valid for an amend.
OrderTimeInForceUnspecified OrderTimeInForce = proto.Order_TIME_IN_FORCE_UNSPECIFIED
// Good until cancelled.
OrderTimeInForceGTC OrderTimeInForce = proto.Order_TIME_IN_FORCE_GTC
// Good until specified time.
OrderTimeInForceGTT OrderTimeInForce = proto.Order_TIME_IN_FORCE_GTT
// Immediate or cancel.
OrderTimeInForceIOC OrderTimeInForce = proto.Order_TIME_IN_FORCE_IOC
// Fill or kill.
OrderTimeInForceFOK OrderTimeInForce = proto.Order_TIME_IN_FORCE_FOK
// Good for auction.
OrderTimeInForceGFA OrderTimeInForce = proto.Order_TIME_IN_FORCE_GFA
// Good for normal.
OrderTimeInForceGFN OrderTimeInForce = proto.Order_TIME_IN_FORCE_GFN
)
type OrderError = proto.OrderError
const (
// Default value, no error reported.
OrderErrorUnspecified OrderError = proto.OrderError_ORDER_ERROR_UNSPECIFIED
// Order was submitted for a market that does not exist.
OrderErrorInvalidMarketID OrderError = proto.OrderError_ORDER_ERROR_INVALID_MARKET_ID
// Order was submitted with an invalid identifier.
OrderErrorInvalidOrderID OrderError = proto.OrderError_ORDER_ERROR_INVALID_ORDER_ID
// Order was amended with a sequence number that was not previous version + 1.
OrderErrorOutOfSequence OrderError = proto.OrderError_ORDER_ERROR_OUT_OF_SEQUENCE
// Order was amended with an invalid remaining size (e.g. remaining greater than total size).
OrderErrorInvalidRemainingSize OrderError = proto.OrderError_ORDER_ERROR_INVALID_REMAINING_SIZE
// Node was unable to get Vega (blockchain) time.
OrderErrorTimeFailure OrderError = proto.OrderError_ORDER_ERROR_TIME_FAILURE
// Failed to remove an order from the book.
OrderErrorRemovalFailure OrderError = proto.OrderError_ORDER_ERROR_REMOVAL_FAILURE
// An order with `TimeInForce.TIME_IN_FORCE_GTT` was submitted or amended
// with an expiration that was badly formatted or otherwise invalid.
OrderErrorInvalidExpirationDatetime OrderError = proto.OrderError_ORDER_ERROR_INVALID_EXPIRATION_DATETIME
// Order was submitted or amended with an invalid reference field.
OrderErrorInvalidOrderReference OrderError = proto.OrderError_ORDER_ERROR_INVALID_ORDER_REFERENCE
// Order amend was submitted for an order field that cannot not be amended (e.g. order identifier).
OrderErrorEditNotAllowed OrderError = proto.OrderError_ORDER_ERROR_EDIT_NOT_ALLOWED
// Amend failure because amend details do not match original order.
OrderErrorAmendFailure OrderError = proto.OrderError_ORDER_ERROR_AMEND_FAILURE
// Order not found in an order book or store.
OrderErrorNotFound OrderError = proto.OrderError_ORDER_ERROR_NOT_FOUND
// Order was submitted with an invalid or missing party identifier.
OrderErrorInvalidParty OrderError = proto.OrderError_ORDER_ERROR_INVALID_PARTY_ID
// Order was submitted for a market that has closed.
OrderErrorMarketClosed OrderError = proto.OrderError_ORDER_ERROR_MARKET_CLOSED
// Order was submitted, but the party did not have enough collateral to cover the order.
OrderErrorMarginCheckFailed OrderError = proto.OrderError_ORDER_ERROR_MARGIN_CHECK_FAILED
// Order was submitted, but the party did not have an account for this asset.
OrderErrorMissingGeneralAccount OrderError = proto.OrderError_ORDER_ERROR_MISSING_GENERAL_ACCOUNT
// Unspecified internal error.
OrderErrorInternalError OrderError = proto.OrderError_ORDER_ERROR_INTERNAL_ERROR
// Order was submitted with an invalid or missing size (e.g. 0).
OrderErrorInvalidSize OrderError = proto.OrderError_ORDER_ERROR_INVALID_SIZE
// Order was submitted with an invalid persistence for its type.
OrderErrorInvalidPersistance OrderError = proto.OrderError_ORDER_ERROR_INVALID_PERSISTENCE
// Order was submitted with an invalid type field.
OrderErrorInvalidType OrderError = proto.OrderError_ORDER_ERROR_INVALID_TYPE
// Order was stopped as it would have traded with another order submitted from the same party.
OrderErrorSelfTrading OrderError = proto.OrderError_ORDER_ERROR_SELF_TRADING
// Order was submitted, but the party did not have enough collateral to cover the fees for the order.
OrderErrorInsufficientFundsToPayFees OrderError = proto.OrderError_ORDER_ERROR_INSUFFICIENT_FUNDS_TO_PAY_FEES
// Order was submitted with an incorrect or invalid market type.
OrderErrorIncorrectMarketType OrderError = proto.OrderError_ORDER_ERROR_INCORRECT_MARKET_TYPE
// Order was submitted with invalid time in force.
OrderErrorInvalidTimeInForce OrderError = proto.OrderError_ORDER_ERROR_INVALID_TIME_IN_FORCE
// A GFN order has got to the market when it is in auction mode.
OrderErrorCannotSendGFNOrderDuringAnAuction OrderError = proto.OrderError_ORDER_ERROR_CANNOT_SEND_GFN_ORDER_DURING_AN_AUCTION
// A GFA order has got to the market when it is in continuous trading mode.
OrderErrorGFAOrderDuringContinuousTrading OrderError = proto.OrderError_ORDER_ERROR_CANNOT_SEND_GFA_ORDER_DURING_CONTINUOUS_TRADING
// Attempt to amend order to GTT without ExpiryAt.
OrderErrorCannotAmendToGTTWithoutExpiryAt OrderError = proto.OrderError_ORDER_ERROR_CANNOT_AMEND_TO_GTT_WITHOUT_EXPIRYAT
// Attempt to amend ExpiryAt to a value before CreatedAt.
OrderErrorExpiryAtBeforeCreatedAt OrderError = proto.OrderError_ORDER_ERROR_EXPIRYAT_BEFORE_CREATEDAT
// Attempt to amend to GTC without an ExpiryAt value.
OrderErrorCannotHaveGTCAndExpiryAt OrderError = proto.OrderError_ORDER_ERROR_CANNOT_HAVE_GTC_AND_EXPIRYAT
// Amending to FOK or IOC is invalid.
OrderErrorCannotAmendToFOKOrIOC OrderError = proto.OrderError_ORDER_ERROR_CANNOT_AMEND_TO_FOK_OR_IOC
// Amending to GFA or GFN is invalid.
OrderErrorCannotAmendToGFAOrGFN OrderError = proto.OrderError_ORDER_ERROR_CANNOT_AMEND_TO_GFA_OR_GFN
// Amending from GFA or GFN is invalid.
OrderErrorCannotAmendFromGFAOrGFN OrderError = proto.OrderError_ORDER_ERROR_CANNOT_AMEND_FROM_GFA_OR_GFN
// IOC orders are not allowed during auction.
OrderErrorCannotSendIOCOrderDuringAuction OrderError = proto.OrderError_ORDER_ERROR_CANNOT_SEND_IOC_ORDER_DURING_AUCTION
// FOK orders are not allowed during auction.
OrderErrorCannotSendFOKOrderDurinAuction OrderError = proto.OrderError_ORDER_ERROR_CANNOT_SEND_FOK_ORDER_DURING_AUCTION
// Pegged orders must be LIMIT orders.
OrderErrorMustBeLimitOrder OrderError = proto.OrderError_ORDER_ERROR_MUST_BE_LIMIT_ORDER
// Pegged orders can only have TIF GTC or GTT.
OrderErrorMustBeGTTOrGTC OrderError = proto.OrderError_ORDER_ERROR_MUST_BE_GTT_OR_GTC
// Pegged order must have a reference price.
OrderErrorWithoutReferencePrice OrderError = proto.OrderError_ORDER_ERROR_WITHOUT_REFERENCE_PRICE
// Buy pegged order cannot reference best ask price.
OrderErrorBuyCannotReferenceBestAskPrice OrderError = proto.OrderError_ORDER_ERROR_BUY_CANNOT_REFERENCE_BEST_ASK_PRICE
// Pegged order offset must be >= 0.
OrderErrorOffsetMustBeGreaterOrEqualToZero OrderError = proto.OrderError_ORDER_ERROR_OFFSET_MUST_BE_GREATER_OR_EQUAL_TO_ZERO
// Sell pegged order cannot reference best bid price.
OrderErrorSellCannotReferenceBestBidPrice OrderError = proto.OrderError_ORDER_ERROR_SELL_CANNOT_REFERENCE_BEST_BID_PRICE
// Pegged order offset must be > zero.
OrderErrorOffsetMustBeGreaterThanZero OrderError = proto.OrderError_ORDER_ERROR_OFFSET_MUST_BE_GREATER_THAN_ZERO
// The party has an insufficient balance, or does not have
// a general account to submit the order (no deposits made
// for the required asset).
OrderErrorInsufficientAssetBalance OrderError = proto.OrderError_ORDER_ERROR_INSUFFICIENT_ASSET_BALANCE
// Cannot amend a non pegged orders details.
OrderErrorCannotAmendPeggedOrderDetailsOnNonPeggedOrder OrderError = proto.OrderError_ORDER_ERROR_CANNOT_AMEND_PEGGED_ORDER_DETAILS_ON_NON_PEGGED_ORDER
// We are unable to re-price a pegged order because a market price is unavailable.
OrderErrorUnableToRepricePeggedOrder OrderError = proto.OrderError_ORDER_ERROR_UNABLE_TO_REPRICE_PEGGED_ORDER
// It is not possible to amend the price of an existing pegged order.
OrderErrorUnableToAmendPriceOnPeggedOrder OrderError = proto.OrderError_ORDER_ERROR_UNABLE_TO_AMEND_PRICE_ON_PEGGED_ORDER
// An FOK, IOC, or GFN order was rejected because it resulted in trades outside the price bounds.
OrderErrorNonPersistentOrderOutOfPriceBounds OrderError = proto.OrderError_ORDER_ERROR_NON_PERSISTENT_ORDER_OUT_OF_PRICE_BOUNDS
// Unable to submit pegged order, temporarily too many pegged orders across all markets.
OrderErrorTooManyPeggedOrders OrderError = proto.OrderError_ORDER_ERROR_TOO_MANY_PEGGED_ORDERS
OrderErrorPostOnlyOrderWouldTrade OrderError = proto.OrderError_ORDER_ERROR_POST_ONLY_ORDER_WOULD_TRADE
OrderErrorReduceOnlyOrderWouldNotReducePosition OrderError = proto.OrderError_ORDER_ERROR_REDUCE_ONLY_ORDER_WOULD_NOT_REDUCE_POSITION
)
var (
ErrInvalidMarketID = OrderErrorInvalidMarketID
ErrInvalidOrderID = OrderErrorInvalidOrderID
ErrOrderOutOfSequence = OrderErrorOutOfSequence
ErrInvalidRemainingSize = OrderErrorInvalidRemainingSize
ErrOrderRemovalFailure = OrderErrorRemovalFailure
ErrInvalidExpirationDatetime = OrderErrorInvalidExpirationDatetime
ErrEditNotAllowed = OrderErrorEditNotAllowed
ErrOrderAmendFailure = OrderErrorAmendFailure
ErrOrderNotFound = OrderErrorNotFound
ErrInvalidPartyID = OrderErrorInvalidParty
ErrInvalidSize = OrderErrorInvalidSize
ErrInvalidPersistence = OrderErrorInvalidPersistance
ErrInvalidType = OrderErrorInvalidType
ErrInvalidTimeInForce = OrderErrorInvalidTimeInForce
ErrPeggedOrderMustBeLimitOrder = OrderErrorMustBeLimitOrder
ErrPeggedOrderMustBeGTTOrGTC = OrderErrorMustBeGTTOrGTC
ErrPeggedOrderWithoutReferencePrice = OrderErrorWithoutReferencePrice
ErrPeggedOrderBuyCannotReferenceBestAskPrice = OrderErrorBuyCannotReferenceBestAskPrice
ErrPeggedOrderOffsetMustBeGreaterOrEqualToZero = OrderErrorOffsetMustBeGreaterOrEqualToZero
ErrPeggedOrderSellCannotReferenceBestBidPrice = OrderErrorSellCannotReferenceBestBidPrice
ErrPeggedOrderOffsetMustBeGreaterThanZero = OrderErrorOffsetMustBeGreaterThanZero
ErrTooManyPeggedOrders = OrderErrorTooManyPeggedOrders
ErrPostOnlyOrderWouldTrade = OrderErrorPostOnlyOrderWouldTrade
ErrReduceOnlyOrderWouldNotReducePosition = OrderErrorReduceOnlyOrderWouldNotReducePosition
)
func IsOrderError(err error) (OrderError, bool) {
oerr, ok := err.(OrderError)
return oerr, ok
}
func IsStoppingOrder(o OrderError) bool {
return o == OrderErrorNonPersistentOrderOutOfPriceBounds ||
o == ErrPostOnlyOrderWouldTrade ||
o == ErrReduceOnlyOrderWouldNotReducePosition
}