/
double_auction_market.go
142 lines (126 loc) · 3.09 KB
/
double_auction_market.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
package econerra
import (
"container/heap"
)
type doubleAuctionMarket struct {
bids orderMaxHeap
offers orderMinHeap
lastHigh Price
lastLow Price
high Price
low Price
lastVolume Size
volume Size
bid Price
ask Price
}
// NewDoubleAuctionMarket constructs a new market for a given good.
func NewDoubleAuctionMarket() Market {
m := &doubleAuctionMarket{}
m.Reset()
return m
}
func (m *doubleAuctionMarket) Bid() Price { return m.bid }
func (m *doubleAuctionMarket) Ask() Price { return m.ask }
func (m *doubleAuctionMarket) High() Price { return m.lastHigh }
func (m *doubleAuctionMarket) Low() Price { return m.lastLow }
func (m *doubleAuctionMarket) Volume() Size { return m.lastVolume }
// Post sends an order to the market. If this order results in a fill,
// the owner(s) will be notified. If not, the order will remain open in
// the market.
func (m *doubleAuctionMarket) Post(o *MarketOrder) {
if o.Size == 0 {
return
}
if o.Price <= 0 {
return
}
switch o.Side {
case Buy:
if len(m.offers) == 0 || o.Price < m.offers[0].Price {
heap.Push(&m.bids, o)
return
}
// Pop sell orders off the heap until we have filled the entire amount.
size := o.Size
for len(m.offers) > 0 && o.Price >= m.offers[0].Price && size > 0 {
if m.offers[0].Size <= size {
sell := heap.Pop(&m.offers).(*MarketOrder)
m.handleFill(o, sell, sell.Price, sell.Size)
size -= sell.Size
} else {
sell := m.offers[0]
m.handleFill(o, sell, sell.Price, size)
m.offers[0].Size -= size
size = 0
}
}
if size > 0 {
o.Size = size
heap.Push(&m.bids, o)
}
case Sell:
if len(m.bids) == 0 || o.Price > m.bids[0].Price {
heap.Push(&m.offers, o)
return
}
// Pop buy orders off the heap until we have filled the entire amount.
size := o.Size
for len(m.bids) > 0 && o.Price <= m.bids[0].Price && size > 0 {
if m.bids[0].Size <= size {
buy := heap.Pop(&m.bids).(*MarketOrder)
m.handleFill(buy, o, buy.Price, buy.Size)
size -= buy.Size
} else {
buy := m.bids[0]
m.handleFill(buy, o, buy.Price, size)
m.bids[0].Size -= size
size = 0
}
}
if size > 0 {
o.Size = size
heap.Push(&m.offers, o)
}
}
}
func (m *doubleAuctionMarket) handleFill(buy, sell *MarketOrder, price Price, size Size) {
buy.Owner.OnFill(Buy, price, size)
sell.Owner.OnFill(Sell, price, size)
if price > m.high {
m.high = price
}
if m.low == 0 || price < m.low {
m.low = price
}
m.volume += size
}
func (m *doubleAuctionMarket) Reset() {
m.lastLow = m.low
m.lastHigh = m.high
m.lastVolume = m.volume
m.high = 0
m.low = 0
m.volume = 0
// Clear out all the orders, sending unfilled notifications as needed.
for _, order := range m.bids {
order.Owner.OnUnfilled(Buy, order.Size)
}
for _, order := range m.offers {
order.Owner.OnUnfilled(Sell, order.Size)
}
if len(m.bids) > 0 {
m.bid = m.bids[0].Price
} else {
m.bid = 0
}
if len(m.offers) > 0 {
m.ask = m.offers[0].Price
} else {
m.ask = 0
}
m.bids = orderMaxHeap{}
m.offers = orderMinHeap{}
heap.Init(&m.bids)
heap.Init(&m.offers)
}