-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_engine.go
196 lines (159 loc) · 4.36 KB
/
match_engine.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package orderbook
import (
"context"
"errors"
"time"
pool "github.com/jolestar/go-commons-pool"
)
// MatchEngine represents a match engine for a stock symbol
type MatchEngine struct {
buyPrices *PriceList
sellPrices *PriceList
cancelledOrders map[string]struct{}
executionCh chan Execution
}
// NewMatchEngine returns new match engine
func NewMatchEngine(poolSize, orderQueueSize int) *MatchEngine {
ctx := context.WithValue(context.Background(), "order_queue_size", uint64(orderQueueSize))
poolConfig := pool.NewDefaultPoolConfig()
poolConfig.MaxTotal = poolSize
poolConfig.MaxIdle = -1
poolConfig.MinIdle = -1
orderQueuePool := pool.NewObjectPool(ctx, &OrderQueueObjectFactory{}, poolConfig)
pool.Prefill(ctx, orderQueuePool, poolSize)
return &MatchEngine{
buyPrices: NewPriceList(orderQueuePool),
sellPrices: NewPriceList(orderQueuePool),
cancelledOrders: make(map[string]struct{}, 1000),
executionCh: make(chan Execution, 1e5),
}
}
// Execution ...
func (engine *MatchEngine) Execution() <-chan Execution {
return engine.executionCh
}
// CancelOrder cancels order
func (engine *MatchEngine) CancelOrder(orderID string) error {
engine.cancelledOrders[orderID] = struct{}{}
return nil
}
// ProcessOrder processes buy or sell order
func (engine *MatchEngine) ProcessOrder(order *Order) error {
switch order.Side {
case Buy:
return engine.processBuyOrder(order)
case Sell:
return engine.processSellOrder(order)
default:
return errors.New("Order side is not correct")
}
}
func (engine *MatchEngine) processBuyOrder(buyOrder *Order) error {
engine.sellPrices.Ascend(func(item *PriceItem) bool {
currSellPrice, orderList := item.Price, item.Orders
if buyOrder.Price < currSellPrice {
return false
}
for orderList.Len() > 0 && buyOrder.Quantity > 0 {
sellOrder := orderList.Get()
if sellOrder == nil {
break
}
if _, ok := engine.cancelledOrders[sellOrder.ID]; ok {
delete(engine.cancelledOrders, sellOrder.ID)
continue
}
if sellOrder.Quantity >= buyOrder.Quantity {
sellOrder.Quantity -= buyOrder.Quantity
engine.executionCh <- Execution{
BuyOrderID: buyOrder.ID,
SellOrderID: sellOrder.ID,
Quantity: buyOrder.Quantity,
Price: currSellPrice,
Timestamp: time.Now().UnixNano(),
}
buyOrder.Quantity = 0
if sellOrder.Quantity > 0 {
if !orderList.Put(sellOrder) {
// TODO: don't expect this error could happen,
// should we panic ?
break
}
}
} else {
buyOrder.Quantity -= sellOrder.Quantity
engine.executionCh <- Execution{
BuyOrderID: buyOrder.ID,
SellOrderID: sellOrder.ID,
Quantity: sellOrder.Quantity,
Price: currSellPrice,
Timestamp: time.Now().UnixNano(),
}
}
}
if orderList.Len() == 0 {
engine.sellPrices.Delete(currSellPrice)
}
if buyOrder.Quantity == 0 {
return false
}
return true
})
if buyOrder.Quantity > 0 {
item := engine.buyPrices.GetItem(buyOrder.Price)
if !item.Orders.Put(buyOrder) {
panic("buy order queue is full")
}
}
return nil
}
func (engine *MatchEngine) processSellOrder(sellOrder *Order) error {
engine.buyPrices.Descend(func(item *PriceItem) bool {
currBuyPrice, orderList := item.Price, item.Orders
if sellOrder.Price > currBuyPrice {
return false
}
for orderList.Len() > 0 && sellOrder.Quantity > 0 {
buyOrder := orderList.Get()
if buyOrder == nil {
break
}
if _, ok := engine.cancelledOrders[buyOrder.ID]; ok {
delete(engine.cancelledOrders, buyOrder.ID)
continue
}
if buyOrder.Quantity >= sellOrder.Quantity {
buyOrder.Quantity -= sellOrder.Quantity
sellOrder.Quantity = 0
if buyOrder.Quantity > 0 {
if !orderList.Put(buyOrder) {
break
}
}
} else {
sellOrder.Quantity -= buyOrder.Quantity
engine.executionCh <- Execution{
BuyOrderID: buyOrder.ID,
SellOrderID: sellOrder.ID,
Quantity: sellOrder.Quantity,
Price: currBuyPrice,
Timestamp: time.Now().UnixNano(),
}
}
}
if orderList.Len() == 0 {
engine.buyPrices.Delete(currBuyPrice)
}
if sellOrder.Quantity == 0 {
return false
}
return true
})
if sellOrder.Quantity > 0 {
item := engine.sellPrices.GetItem(sellOrder.Price)
if !item.Orders.Put(sellOrder) {
panic("sell order queue is full")
}
}
return nil
}