-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderbook.go
165 lines (149 loc) · 3.5 KB
/
orderbook.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mutex
import "strconv"
import "bytes"
type OrderBook struct {
IsBuy bool
Last int
Price int
Depth int
TotalDepth int
FirstLevel *Level
LastLevel *Level
}
func (ob *OrderBook) String() string {
var buffer bytes.Buffer
if ob.IsBuy {
buffer.WriteString("BUY ")
} else {
buffer.WriteString("SELL ")
}
for current_level := ob.FirstLevel; current_level != nil; current_level = current_level.NextLevel {
buffer.WriteString(strconv.Itoa(current_level.Price))
buffer.WriteString(" ")
}
return buffer.String()
}
func (ob *OrderBook) Level(price int) *Level {
// Check for no levels
if ob.FirstLevel == nil {
l := &Level{
Price: price,
}
ob.FirstLevel = l
ob.LastLevel = l
return l
} else {
// Find the next level
for current_level := ob.FirstLevel; current_level != nil; current_level = current_level.NextLevel {
if current_level.Price == price {
return current_level
}
if (ob.IsBuy && current_level.Price < price) || (!ob.IsBuy && current_level.Price > price) {
l := &Level{
Price: price,
}
if current_level.PrevLevel == nil {
// New first level
ob.FirstLevel = l
l.NextLevel = current_level
current_level.PrevLevel = l
} else {
// New middle level
l.NextLevel = current_level
l.PrevLevel = current_level.PrevLevel
current_level.PrevLevel.NextLevel = l
current_level.PrevLevel = l
}
return l
}
}
// If we get here, we need to add to the end of levels
l := &Level{
Price: price,
}
l.PrevLevel = ob.LastLevel
ob.LastLevel.NextLevel = l
ob.LastLevel = l
return l
}
}
func (ob *OrderBook) RemoveLevel(price int) {
l := ob.Level(price)
if l.FirstOrder != nil {
return
}
// Only order
if ob.FirstLevel == l && ob.LastLevel == l {
ob.FirstLevel = nil
ob.LastLevel = nil
} else if ob.FirstLevel == l { // First Order
ob.FirstLevel = ob.FirstLevel.NextLevel
ob.FirstLevel.PrevLevel = nil
} else if ob.LastLevel == l { // Last Order
ob.LastLevel = ob.LastLevel.PrevLevel
ob.LastLevel.NextLevel = nil
} else { // Middle Level
l.NextLevel.PrevLevel = l.PrevLevel
l.PrevLevel.NextLevel = l.NextLevel
}
}
func (ob *OrderBook) AddOrder(o *Order) {
if o.Qty > 0 {
o.Open = true
}
l := ob.Level(o.Price)
l.AddOrder(o)
ob.TotalDepth += o.Qty
ob.UpdateFirstStats()
}
func (ob *OrderBook) RemoveOrder(o *Order) {
l := ob.Level(o.Price)
did_remove := l.RemoveOrder(o)
if did_remove {
ob.TotalDepth -= o.Qty
}
if l.Qty == 0 && l.FirstOrder == nil {
ob.RemoveLevel(l.Price)
}
ob.UpdateFirstStats()
}
func (ob *OrderBook) UpdateFirstStats() {
if ob.FirstLevel != nil {
ob.Depth = ob.FirstLevel.Qty
ob.Price = ob.FirstLevel.Price
} else {
ob.Depth = 0
ob.Price = 0
}
}
func (ob *OrderBook) Crosses(resting int, incoming int) bool {
return (ob.IsBuy && incoming <= resting) || (!ob.IsBuy && incoming >= resting)
}
func (ob *OrderBook) ExecuteOrder(o *Order) {
if ob.FirstLevel == nil {
return
}
if !ob.Crosses(ob.Price, o.Price) {
return
}
for l := ob.FirstLevel; l != nil && ob.Crosses(l.Price, o.Price); l = l.NextLevel {
for lo := l.FirstOrder; lo != nil; lo = lo.NextOrder {
fill := lo.Order.ApplyIncoming(o)
if fill != nil {
l.Qty -= fill.Qty
ob.Last = fill.Price
if lo.Order.Open == false {
l.RemoveFilledFirstOrder(lo.Order, fill.Qty)
ob.TotalDepth -= fill.Qty
}
if o.Open == false {
break
}
}
}
if l.Qty == 0 && l.FirstOrder == nil {
ob.RemoveLevel(l.Price)
}
}
ob.UpdateFirstStats()
}