-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.go
80 lines (74 loc) · 1.8 KB
/
order.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
package mutex
import (
"errors"
"strings"
"time"
)
type Order struct {
Ok bool `json:"ok"`
Symbol string `json:"symbol"`
Venue string `json:"venue"`
Direction string `json:"direction"`
OriginalQty int `json:"originalQty"`
Qty int `json:"qty"`
TotalFilled int `json:"totalFilled"`
Price int `json:"price"`
OrderType string `json:"orderType"`
Id int `json:"id"`
Account string `json:"account"`
Ts time.Time `json:"ts"`
Fills []*OrderFill `json:"fills"`
Open bool `json:"open"`
}
type OrderFill struct {
Price int
Qty int
Ts time.Time
}
func (o *Order) ApplyIncoming(incoming *Order) *OrderFill {
fillQty := 0
if o.Qty < incoming.Qty {
fillQty = o.Qty
} else {
fillQty = incoming.Qty
}
fill := &OrderFill{
Qty: fillQty,
Price: o.Price,
Ts: time.Now(),
}
o.Fill(fill)
incoming.Fill(fill)
return fill
}
func (o *Order) Fill(fill *OrderFill) {
o.Qty -= fill.Qty
o.TotalFilled += fill.Qty
if o.Qty == 0 {
o.Open = false
}
o.Fills = append(o.Fills, fill)
}
func (o *Order) Validate() error {
if o.Qty <= 0 {
return errors.New("Qty cannot be zero or less.")
}
if o.Qty >= 10000 {
return errors.New("Qty cannot be more than 10000.")
}
if o.Price <= 0 {
return errors.New("Price cannot be zero or less.")
}
if o.Price >= 10000 {
return errors.New("Price cannot be more than $10,000 (1000000 cents).")
}
o.OrderType = strings.ToLower(o.OrderType)
if o.OrderType != "limit" {
return errors.New("The only supported order type is 'limit'")
}
o.Direction = strings.ToLower(o.Direction)
if o.Direction != "buy" && o.Direction != "sell" {
return errors.New("Direction must be buy or sell")
}
return nil
}