-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket.go
89 lines (78 loc) · 1.54 KB
/
market.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
package mutex
import "time"
import "errors"
type Market struct {
// Orders
orders []*Order
buyBook OrderBook
sellBook OrderBook
// last
lastId int
}
func (m *Market) PlaceOrder(o *Order) {
m.prepOrder(o)
// Check for crosses
m.applyOrder(o)
// Add to books if needed
m.addToBooks(o)
// One day will fire off execution and ticker events
}
func (m *Market) CancelOrder(id int) error {
o, err := m.Order(id)
if err != nil {
return err
}
if o.Direction == "buy" {
m.buyBook.RemoveOrder(o)
} else if o.Direction == "sell" {
m.sellBook.RemoveOrder(o)
}
o.Open = false
return nil
}
func (m *Market) Quote() {
// Grab from totals, (should be fast)
}
func (m *Market) Orderbook() {
// Summaries
}
func (m *Market) Order(id int) (*Order, error) {
if id <= 0 {
return nil, errors.New("Order id must be greater than 0")
}
if id > len(m.orders) {
return nil, errors.New("Order does not exist")
}
order := m.orders[id-1]
return order, nil
}
func (m *Market) prepOrder(o *Order) {
m.lastId += 1
o.Id = m.lastId
o.Ts = time.Now()
o.OriginalQty = o.Qty
o.TotalFilled = 0
o.Ok = true
o.Open = true
m.orders = append(m.orders, o)
}
func (m *Market) applyOrder(o *Order) {
if o.Direction == "buy" {
m.sellBook.ExecuteOrder(o)
} else if o.Direction == "sell" {
m.buyBook.ExecuteOrder(o)
}
}
func (m *Market) addToBooks(o *Order) {
if o.OrderType == "immediate-or-cancel" {
return
}
if o.Qty <= 0 {
return
}
if o.Direction == "buy" {
m.buyBook.AddOrder(o)
} else if o.Direction == "sell" {
m.sellBook.AddOrder(o)
}
}