-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.go
77 lines (72 loc) · 1.58 KB
/
level.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
package mutex
type Level struct {
Price int
Qty int
NextLevel *Level
PrevLevel *Level
FirstOrder *LevelOrder
LastOrder *LevelOrder
}
type LevelOrder struct {
Order *Order
Level *Level
NextOrder *LevelOrder
PrevOrder *LevelOrder
}
func (l *Level) AddOrder(o *Order) {
lo := &LevelOrder{
Order: o,
Level: l,
}
if l.LastOrder == nil {
// We have no orders on this level yet
l.FirstOrder = lo
l.LastOrder = lo
} else {
l.LastOrder.NextOrder = lo
lo.PrevOrder = l.LastOrder
l.LastOrder = lo
}
l.Qty += o.Qty
}
func (l *Level) RemoveFilledFirstOrder(o *Order, filled int) {
if l.FirstOrder.Order != o {
panic("Attempted to remove wrong first level")
}
l.FirstOrder = l.FirstOrder.NextOrder
if l.FirstOrder != nil {
l.FirstOrder.PrevOrder = nil
}
}
func (l *Level) RemoveOrder(o *Order) bool {
if l.LastOrder == nil {
return false
}
if l.LastOrder.Order == o {
// Special case handling the last order
if l.LastOrder.PrevOrder == nil {
// If this is the only order on the level
l.LastOrder = nil
l.FirstOrder = nil
} else {
l.LastOrder.PrevOrder.NextOrder = nil
l.LastOrder = l.LastOrder.PrevOrder
}
} else {
for lo := l.LastOrder.PrevOrder; lo != nil; lo = lo.PrevOrder {
if lo.Order == o {
if l.FirstOrder == lo {
l.FirstOrder = lo.NextOrder
// Special case for first order
} else {
// Handle a middle order by removing me from links on either side
lo.NextOrder.PrevOrder = lo.PrevOrder
lo.PrevOrder.NextOrder = lo.NextOrder
}
break
}
}
}
l.Qty -= o.Qty
return true
}