This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
vega_ext.go
80 lines (68 loc) · 1.98 KB
/
vega_ext.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
// vega_ext provides extra methods to the types generated by protobuf.
package vega
import (
fmt "fmt"
"strconv"
"time"
)
// Float64Fee tries to parse the Fee (string) into a float64.
// If parsing fails 0 is returned.
func (l *LiquidityProvision) Float64Fee() float64 {
v, err := strconv.ParseFloat(l.Fee, 64)
if err != nil {
return 0
}
return v
}
func (o *Order) IsLiquidityOrder() bool {
return len(o.LiquidityProvisionId) > 0
}
// Create sets the creation time (CreatedAt) to t and returns the
// updated order.
func (o *Order) Create(t time.Time) *Order {
o.CreatedAt = t.UnixNano()
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 == Order_TIME_IN_FORCE_GTC ||
o.TimeInForce == Order_TIME_IN_FORCE_GTT ||
o.TimeInForce == Order_TIME_IN_FORCE_GFN ||
o.TimeInForce == Order_TIME_IN_FORCE_GFA) &&
o.Type == Order_TYPE_LIMIT &&
o.Remaining > 0
}
func (o *Order) IsExpireable() bool {
return (o.TimeInForce == Order_TIME_IN_FORCE_GFN ||
o.TimeInForce == Order_TIME_IN_FORCE_GTT ||
o.TimeInForce == Order_TIME_IN_FORCE_GFA) &&
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 != Order_STATUS_ACTIVE && o.Status != Order_STATUS_PARKED
}
func (o *Order) HasTraded() bool {
return o.Size != o.Remaining
}
func (t *Trade) SetIDs(aggressive, passive *Order, idx int) {
t.Id = fmt.Sprintf("%s-%010d", aggressive.Id, idx)
if aggressive.Side == Side_SIDE_BUY {
t.BuyOrder = aggressive.Id
t.SellOrder = passive.Id
return
}
t.SellOrder = aggressive.Id
t.BuyOrder = passive.Id
}