-
Notifications
You must be signed in to change notification settings - Fork 59
/
limitorder.go
57 lines (46 loc) · 985 Bytes
/
limitorder.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
package hftorderbook
// Limit price orders combined as a FIFO queue
type LimitOrder struct {
Price float64
orders *ordersQueue
totalVolume float64
}
func NewLimitOrder(price float64) LimitOrder {
q := NewOrdersQueue()
return LimitOrder{
Price: price,
orders: &q,
}
}
func (this *LimitOrder) TotalVolume() float64 {
return this.totalVolume
}
func (this *LimitOrder) Size() int {
return this.orders.Size()
}
func (this *LimitOrder) Enqueue(o *Order) {
this.orders.Enqueue(o)
o.Limit = this
this.totalVolume += o.Volume
}
func (this *LimitOrder) Dequeue() *Order {
if this.orders.IsEmpty() {
return nil
}
o := this.orders.Dequeue()
this.totalVolume -= o.Volume
return o
}
func (this *LimitOrder) Delete(o *Order) {
if o.Limit != this {
panic("order does not belong to the limit")
}
this.orders.Delete(o)
o.Limit = nil
this.totalVolume -= o.Volume
}
func (this *LimitOrder) Clear() {
q := NewOrdersQueue()
this.orders = &q
this.totalVolume = 0
}