-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordernecromancer.go
157 lines (127 loc) · 4.11 KB
/
ordernecromancer.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
package main
import (
"sync"
"github.com/smira/go-statsd"
"github.com/shopspring/decimal"
"github.com/sinisterminister/coinfactory"
"github.com/spf13/viper"
log "github.com/sirupsen/logrus"
scribble "github.com/nanobox-io/golang-scribble"
)
type orderNecromancer struct {
db *scribble.Driver
mutex *sync.Mutex
}
var orderNecromancerOnce = sync.Once{}
var orderNecromancerInstance *orderNecromancer
func getOrderNecromancerInstance() *orderNecromancer {
orderNecromancerOnce.Do(func() {
db, err := scribble.New(viper.GetString("scribbledb.dir"), nil)
if err != nil {
log.WithError(err).Panic("cannot start scribble database")
}
orderNecromancerInstance = &orderNecromancer{
db: db,
mutex: &sync.Mutex{},
}
})
return orderNecromancerInstance
}
func (o *orderNecromancer) BuryRequest(req coinfactory.OrderRequest) (err error) {
// Lock the data for writing
o.mutex.Lock()
defer o.mutex.Unlock()
// Add it and save it to the order collection
orders := []coinfactory.OrderRequest{}
err = o.db.Read("deadorders", req.Symbol, &orders)
if err != nil {
log.Debug("could not load previous orders. starting new")
}
orders = append(orders, req)
err = o.db.Write("deadorders", req.Symbol, orders)
metrics.Incr("necromancer.bury", 1, statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side))
return
}
func (o *orderNecromancer) BuryOrder(order *coinfactory.Order) (err error) {
// First, create an order request
var req coinfactory.OrderRequest
orderID := order.GetStatus().OrderID
if orderID != 0 {
req = coinfactory.OrderRequest{
Symbol: order.Symbol,
Side: order.Side,
Price: order.Price,
Quantity: order.GetStatus().OriginalQuantity.Sub(order.GetStatus().ExecutedQuantity),
}
} else {
req = coinfactory.OrderRequest{
Symbol: order.Symbol,
Side: order.Side,
Price: order.Price,
Quantity: order.Quantity,
}
}
o.BuryRequest(req)
return
}
func (o *orderNecromancer) ResurrectOrders(symbol string, ask decimal.Decimal, bid decimal.Decimal) (orders []*coinfactory.Order) {
// Load the orders
orderReq := []coinfactory.OrderRequest{}
allOrders := []coinfactory.OrderRequest{}
savedOrders := []coinfactory.OrderRequest{}
o.mutex.Lock()
err := o.db.Read("deadorders", symbol, &allOrders)
if err != nil {
o.mutex.Unlock()
return
}
for _, req := range allOrders {
if (req.Side == "BUY" && req.Price.GreaterThanOrEqual(ask)) || (req.Side == "SELL" && req.Price.LessThanOrEqual(bid)) {
metrics.Incr("necromancer.resurrect.attempt", 1, statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side))
orderReq = append(orderReq, req)
continue
}
metrics.Gauge("necromancer.buried", int64(len(savedOrders)), statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side))
savedOrders = append(savedOrders, req)
}
err = o.db.Write("deadorders", symbol, savedOrders)
if err != nil {
log.WithError(err).Error("could not save dead orders after resurrection")
}
o.mutex.Unlock()
orderChan := make(chan *coinfactory.Order)
loopDoneChan := make(chan bool)
go func() {
for _, req := range orderReq {
// Attempt to place the order
order, err := cf.GetOrderManager().AttemptOrder(req)
if err != nil {
log.WithField("order", req).WithError(err).Warn("could not resurrect order")
metrics.Incr("necromancer.resurrect.failure", 1, statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side), statsd.StringTag("type", "error"))
o.BuryRequest(req)
continue
}
orderChan <- order
go func() {
<-order.GetDoneChan()
if order.GetStatus().Status == "CANCELED" {
metrics.Incr("necromancer.resurrect.failure", 1, statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side), statsd.StringTag("type", "canceled"))
o.BuryOrder(order)
return
}
metrics.Incr("necromancer.resurrect.success", 1, statsd.StringTag("symbol", req.Symbol), statsd.StringTag("side", req.Side))
}()
}
close(loopDoneChan)
}()
orderLoop:
for {
select {
case o := <-orderChan:
orders = append(orders, o)
case <-loopDoneChan:
break orderLoop
}
}
return
}