-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathorderbook.go
156 lines (138 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
package bexchange
import "fmt"
const MAX_PRICE = 10000000
type PricePoint struct {
orderHead *Order
orderTail *Order
}
func (pp *PricePoint) Insert(o *Order) {
if pp.orderHead == nil {
pp.orderHead = o
pp.orderTail = o
} else {
pp.orderTail.next = o
pp.orderTail = o
}
}
type OrderStatus int
const (
OS_NEW OrderStatus = iota
OS_OPEN
OS_PARTIAL
OS_FILLED
OS_CANCELLED
)
type Order struct {
id uint64
isBuy bool
price uint32
amount uint32
status OrderStatus
next *Order
}
func (o *Order) String() string {
return fmt.Sprintf("\nOrder{id:%v,isBuy:%v,price:%v,amount:%v}",
o.id, o.isBuy, o.price, o.amount)
}
func NewOrder(id uint64, isBuy bool, price uint32, amount uint32) *Order {
return &Order{id: id, isBuy: isBuy, price: price, amount: amount,
status: OS_NEW}
}
type OrderBook struct {
// These are more estimates than reportable values
ask uint32
bid uint32
orderIndex map[uint64]*Order
prices [MAX_PRICE]*PricePoint
actions chan<- *Action
}
func NewOrderBook(actions chan<- *Action) *OrderBook {
ob := new(OrderBook)
ob.bid = 0
ob.ask = MAX_PRICE
for i := range ob.prices {
ob.prices[i] = new(PricePoint)
}
ob.actions = actions
ob.orderIndex = make(map[uint64]*Order)
return ob
}
func (ob *OrderBook) AddOrder(o *Order) {
// Try to fill immediately
if o.isBuy {
ob.actions <- NewBuyAction(o)
ob.FillBuy(o)
} else {
ob.actions <- NewSellAction(o)
ob.FillSell(o)
}
// Into the book
if o.amount > 0 {
ob.openOrder(o)
}
}
func (ob *OrderBook) openOrder(o *Order) {
pp := ob.prices[o.price]
pp.Insert(o)
o.status = OS_OPEN
if o.isBuy && o.price > ob.bid {
ob.bid = o.price
} else if !o.isBuy && o.price < ob.ask {
ob.ask = o.price
}
ob.orderIndex[o.id] = o
}
func (ob *OrderBook) FillBuy(o *Order) {
for ob.ask < o.price && o.amount > 0 {
pp := ob.prices[ob.ask]
ppOrderHead := pp.orderHead
for ppOrderHead != nil {
ob.fill(o, ppOrderHead)
ppOrderHead = ppOrderHead.next
pp.orderHead = ppOrderHead
}
ob.ask++
}
}
func (ob *OrderBook) FillSell(o *Order) {
for ob.bid >= o.price && o.amount > 0 {
pp := ob.prices[ob.bid]
ppOrderHead := pp.orderHead
for ppOrderHead != nil {
ob.fill(o, ppOrderHead)
ppOrderHead = ppOrderHead.next
pp.orderHead = ppOrderHead
}
ob.bid--
}
}
func (ob *OrderBook) fill(o, ppOrderHead *Order) {
if ppOrderHead.amount >= o.amount {
ob.actions <- NewFilledAction(o, ppOrderHead)
ppOrderHead.amount -= o.amount
o.amount = 0
o.status = OS_FILLED
return
} else {
// Partial fill
if ppOrderHead.amount > 0 {
ob.actions <- NewPartialFilledAction(o, ppOrderHead)
o.amount -= ppOrderHead.amount
o.status = OS_PARTIAL
ppOrderHead.amount = 0
}
}
}
func (ob *OrderBook) CancelOrder(id uint64) {
ob.actions <- NewCancelAction(id)
if o, ok := ob.orderIndex[id]; ok {
// If this is the last order at a particular price point
// we need to update the bid/ask...right? Maybe not?
o.amount = 0
o.status = OS_CANCELLED
}
ob.actions <- NewCancelledAction(id)
}
func (ob *OrderBook) Done() {
ob.actions <- NewDoneAction()
}