forked from rainhq/signalr
/
model.go
239 lines (197 loc) · 5.62 KB
/
model.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package bittrex
import (
"fmt"
"sort"
"strconv"
"time"
"github.com/shopspring/decimal"
)
type OrderBook struct {
MarketSymbol string
Depth int
Sequence int
Bids OrderBookEntries
Asks OrderBookEntries
}
func (b *OrderBook) Apply(delta *OrderBook) *OrderBookDelta {
bids, bidsDiff := b.Bids.Apply(delta.Bids, decimal.Decimal.LessThanOrEqual)
asks, asksDiff := b.Asks.Apply(delta.Asks, decimal.Decimal.GreaterThanOrEqual)
b.Sequence, b.Bids, b.Asks = delta.Sequence, bids, asks
return &OrderBookDelta{
MarketSymbol: b.MarketSymbol,
Sequence: b.Sequence,
Bids: bidsDiff,
Asks: asksDiff,
}
}
type OrderBookEntries []OrderBookEntry
func (e OrderBookEntries) String() string {
return fmt.Sprintf("%+v", []OrderBookEntry(e))
}
func (e OrderBookEntries) OrderBookDeltaEntries(action Action) OrderBookDeltaEntries {
res := make(OrderBookDeltaEntries, len(e))
for i, v := range e {
res[i] = v.OrderBookDiffEntry(action)
}
return res
}
func (e OrderBookEntries) SearchRate(rate decimal.Decimal, f func(l, r decimal.Decimal) bool) int {
return sort.Search(len(e), func(i int) bool {
return f(e[i].Rate, rate)
})
}
func (e OrderBookEntries) Apply(deltas OrderBookEntries, cmp func(l, r decimal.Decimal) bool) (OrderBookEntries, OrderBookDeltaEntries) {
res := make(OrderBookEntries, 0, len(e))
diffs := make(OrderBookDeltaEntries, 0, len(deltas))
for _, delta := range deltas {
n := len(e)
j := e.SearchRate(delta.Rate, cmp)
res = append(res, e[:j]...)
if j == n {
e = e[j:]
break
}
var action Action
switch {
case delta.Quantity.IsZero():
action = DeleteAction
e = e[j+1:]
case delta.Rate.Equal(e[j].Rate):
action = UpdateAction
res = append(res, delta)
e = e[j+1:]
default:
action = AddAction
res = append(res, delta)
e = e[j:]
}
diffs = append(diffs, delta.OrderBookDiffEntry(action))
}
res = append(res, e...)
// add remaining deltas
deltas = deltas[len(diffs):]
res = append(res, deltas...)
diffs = append(diffs, deltas.OrderBookDeltaEntries(AddAction)...)
return res, diffs
}
type OrderBookEntry struct {
Rate decimal.Decimal
Quantity decimal.Decimal
}
func (e OrderBookEntry) OrderBookDiffEntry(action Action) OrderBookDeltaEntry {
return OrderBookDeltaEntry{
Action: action,
Rate: e.Rate,
Quantity: e.Quantity,
}
}
type OrderBookDelta struct {
MarketSymbol string
Depth int
Sequence int
Bids OrderBookDeltaEntries
Asks OrderBookDeltaEntries
}
type OrderBookDeltaEntries []OrderBookDeltaEntry
func (e OrderBookDeltaEntries) SearchRate(rate decimal.Decimal, cmp func(l, r decimal.Decimal) bool) int {
return sort.Search(len(e), func(i int) bool {
return cmp(e[i].Rate, rate)
})
}
func (e OrderBookDeltaEntries) String() string {
return fmt.Sprintf("%+v", []OrderBookDeltaEntry(e))
}
type OrderBookDeltaEntry struct {
Action Action
Rate decimal.Decimal
Quantity decimal.Decimal
}
type Action string
const (
AddAction = Action("add")
UpdateAction = Action("update")
DeleteAction = Action("delete")
)
type Orders struct {
Sequence int
Data []*Order
}
func (o *Orders) Apply(sequence int, order *Order) (*OrderDelta, error) {
n := len(o.Data)
i := o.SearchOrder(order)
switch {
case i == n && order.Status == OrderStatusClosed:
return nil, &OrderNotFoundError{ID: order.ID}
case i == n:
o.Data = append(o.Data, order)
return &OrderDelta{Sequence: sequence, Action: AddAction, Order: order}, nil
case order.Status == OrderStatusClosed:
o.Data = append(o.Data[i:], o.Data[i+1:]...)
return &OrderDelta{Sequence: sequence, Action: DeleteAction, Order: order}, nil
default:
o.Data[i] = order
return &OrderDelta{Sequence: sequence, Action: UpdateAction, Order: order}, nil
}
}
func (o *Orders) SearchOrder(order *Order) int {
n := len(o.Data)
i := sort.Search(n, func(i int) bool {
return o.Data[i].CreatedAt.Before(order.CreatedAt)
})
if i == n {
return n
}
for j, v := range o.Data[i:] {
switch {
case v.ID == order.ID:
return i + j
case v.CreatedAt.After(order.CreatedAt):
break
}
}
return n
}
type OrderDelta struct {
Sequence int
Action Action
Order *Order
}
type Order struct {
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
MarketSymbol string `json:"marketSymbol"`
Side string `json:"direction"`
Type string `json:"type"`
Quantity decimal.NullDecimal `json:"quantity"`
Ceiling decimal.NullDecimal `json:"ceiling"`
Limit decimal.NullDecimal `json:"limit"`
Option string `json:"timeInForce"`
FillQuantity decimal.Decimal `json:"fillQuantity"`
Commission decimal.Decimal `json:"commission"`
Proceeds decimal.Decimal `json:"proceeds"`
Status OrderStatus `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ClosedAt time.Time `json:"closedAt"`
}
type OrderStatus string
const (
OrderStatusOpen = OrderStatus("OPEN")
OrderStatusClosed = OrderStatus("CLOSED")
)
func (s *OrderStatus) UnmarshalJSON(data []byte) error {
v, err := strconv.Unquote(string(data))
if err != nil {
return fmt.Errorf("invalid value")
}
if v != string(OrderStatusOpen) && v != string(OrderStatusClosed) {
return fmt.Errorf("invalid OrderStatus: %q", v)
}
*s = OrderStatus(v)
return nil
}
type Balance struct {
CurrencySymbol string `json:"currencySymbol"`
Total decimal.Decimal `json:"decimal"`
Available decimal.Decimal `json:"available"`
}