-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
convert.go
342 lines (279 loc) · 8.79 KB
/
convert.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package max
import (
"fmt"
"strings"
"time"
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
v3 "github.com/c9s/bbgo/pkg/exchange/max/maxapi/v3"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func toGlobalCurrency(currency string) string {
return strings.ToUpper(currency)
}
func toLocalCurrency(currency string) string {
return strings.ToLower(currency)
}
func toLocalSymbol(symbol string) string {
return strings.ToLower(symbol)
}
func toGlobalSymbol(symbol string) string {
return strings.ToUpper(symbol)
}
func toLocalSideType(side types.SideType) string {
return strings.ToLower(string(side))
}
func toGlobalSideType(v string) types.SideType {
switch strings.ToLower(v) {
case "bid", "buy":
return types.SideTypeBuy
case "ask", "sell":
return types.SideTypeSell
case "self-trade":
return types.SideTypeSelf
}
return types.SideType(v)
}
func toGlobalRewards(maxRewards []max.Reward) ([]types.Reward, error) {
// convert to global reward
var rewards []types.Reward
for _, r := range maxRewards {
// ignore "accepted"
if r.State != "done" {
continue
}
reward, err := r.Reward()
if err != nil {
return nil, err
}
rewards = append(rewards, *reward)
}
return rewards, nil
}
func toGlobalOrderStatus(
orderState max.OrderState, executedVolume, remainingVolume fixedpoint.Value,
) types.OrderStatus {
switch orderState {
case max.OrderStateCancel:
return types.OrderStatusCanceled
case max.OrderStateFinalizing, max.OrderStateDone:
if executedVolume.IsZero() {
return types.OrderStatusCanceled
} else if remainingVolume.IsZero() {
return types.OrderStatusFilled
}
return types.OrderStatusFilled
case max.OrderStateWait:
if executedVolume.Sign() > 0 && remainingVolume.Sign() > 0 {
return types.OrderStatusPartiallyFilled
}
return types.OrderStatusNew
case max.OrderStateConvert:
if executedVolume.Sign() > 0 && remainingVolume.Sign() > 0 {
return types.OrderStatusPartiallyFilled
}
return types.OrderStatusNew
case max.OrderStateFailed:
return types.OrderStatusRejected
}
log.Errorf("can not convert MAX exchange order status, unknown order state: %q", orderState)
return types.OrderStatus(orderState)
}
func toGlobalOrderType(orderType max.OrderType) types.OrderType {
switch orderType {
case max.OrderTypeLimit:
return types.OrderTypeLimit
case max.OrderTypeMarket:
return types.OrderTypeMarket
case max.OrderTypeStopLimit:
return types.OrderTypeStopLimit
case max.OrderTypeStopMarket:
return types.OrderTypeStopMarket
case max.OrderTypeIOCLimit:
return types.OrderTypeLimit
case max.OrderTypePostOnly:
return types.OrderTypeLimitMaker
}
log.Errorf("order convert error, unknown order type: %v", orderType)
return types.OrderType(orderType)
}
func toLocalOrderType(orderType types.OrderType) (max.OrderType, error) {
switch orderType {
case types.OrderTypeStopLimit:
return max.OrderTypeStopLimit, nil
case types.OrderTypeStopMarket:
return max.OrderTypeStopMarket, nil
case types.OrderTypeLimitMaker:
return max.OrderTypePostOnly, nil
case types.OrderTypeLimit:
return max.OrderTypeLimit, nil
case types.OrderTypeMarket:
return max.OrderTypeMarket, nil
}
return "", fmt.Errorf("order type %s not supported", orderType)
}
func toGlobalOrders(maxOrders []max.Order) (orders []types.Order, err error) {
for _, localOrder := range maxOrders {
o, err := toGlobalOrder(localOrder)
if err != nil {
log.WithError(err).Error("order convert error")
} else {
orders = append(orders, *o)
}
}
return orders, err
}
func toGlobalOrder(maxOrder max.Order) (*types.Order, error) {
executedVolume := maxOrder.ExecutedVolume
remainingVolume := maxOrder.RemainingVolume
isMargin := maxOrder.WalletType == max.WalletTypeMargin
return &types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: maxOrder.ClientOID,
Symbol: toGlobalSymbol(maxOrder.Market),
Side: toGlobalSideType(maxOrder.Side),
Type: toGlobalOrderType(maxOrder.OrderType),
Quantity: maxOrder.Volume,
Price: maxOrder.Price,
TimeInForce: types.TimeInForceGTC, // MAX only supports GTC
GroupID: maxOrder.GroupID,
},
Exchange: types.ExchangeMax,
IsWorking: maxOrder.State == max.OrderStateWait,
OrderID: maxOrder.ID,
Status: toGlobalOrderStatus(maxOrder.State, executedVolume, remainingVolume),
OriginalStatus: string(maxOrder.State),
ExecutedQuantity: executedVolume,
CreationTime: types.Time(maxOrder.CreatedAt.Time()),
UpdateTime: types.Time(maxOrder.UpdatedAt.Time()),
IsMargin: isMargin,
IsIsolated: false, // isolated margin is not supported
}, nil
}
func toGlobalTradeV3(t v3.Trade) ([]types.Trade, error) {
var trades []types.Trade
isMargin := t.WalletType == max.WalletTypeMargin
side := toGlobalSideType(t.Side)
fee := fixedpoint.Zero
if t.Fee != nil {
fee = *t.Fee
}
trade := types.Trade{
ID: t.ID,
OrderID: t.OrderID,
Price: t.Price,
Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax,
Quantity: t.Volume,
Side: side,
IsBuyer: t.IsBuyer(),
IsMaker: t.IsMaker(),
Fee: fee,
FeeProcessing: t.Fee == nil,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
FeeDiscounted: t.FeeDiscounted,
QuoteQuantity: t.Funds,
Time: types.Time(t.CreatedAt),
IsMargin: isMargin,
IsIsolated: false,
IsFutures: false,
}
if t.Side == "self-trade" {
trade.Side = types.SideTypeSell
// create trade for bid
bidTrade := trade
bidTrade.Side = types.SideTypeBuy
bidTrade.OrderID = t.SelfTradeBidOrderID
bidTrade.Fee = t.SelfTradeBidFee
bidTrade.FeeCurrency = toGlobalCurrency(t.SelfTradeBidFeeCurrency)
bidTrade.FeeDiscounted = t.SelfTradeBidFeeDiscounted
bidTrade.IsBuyer = !trade.IsBuyer
bidTrade.IsMaker = !trade.IsMaker
trades = append(trades, bidTrade)
}
trades = append(trades, trade)
return trades, nil
}
func toGlobalTradeV2(t max.Trade) (*types.Trade, error) {
isMargin := t.WalletType == max.WalletTypeMargin
side := toGlobalSideType(t.Side)
return &types.Trade{
ID: t.ID,
OrderID: t.OrderID,
Price: t.Price,
Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax,
Quantity: t.Volume,
Side: side,
IsBuyer: t.IsBuyer(),
IsMaker: t.IsMaker(),
Fee: t.Fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: t.Funds,
Time: types.Time(t.CreatedAt),
IsMargin: isMargin,
IsIsolated: false,
IsFutures: false,
}, nil
}
func toGlobalDepositStatus(a max.DepositState) types.DepositStatus {
switch a {
case max.DepositStateSubmitting, max.DepositStateSubmitted, max.DepositStatePending, max.DepositStateChecking:
return types.DepositPending
case max.DepositStateRejected:
return types.DepositRejected
case max.DepositStateCancelled:
return types.DepositCancelled
case max.DepositStateAccepted:
return types.DepositSuccess
}
// other states goes to this
// max.DepositStateSuspect, max.DepositStateSuspended
log.Warnf("unsupported deposit state %q from max exchange", a)
return types.DepositStatus(a)
}
func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
// skip trade ID that is the same. however this should not happen
var side = toGlobalSideType(t.Side)
return &types.Trade{
ID: t.ID,
OrderID: t.OrderID,
Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax,
Price: t.Price,
Quantity: t.Volume,
Side: side,
IsBuyer: side == types.SideTypeBuy,
IsMaker: t.Maker,
Fee: t.Fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
FeeDiscounted: t.FeeDiscounted,
QuoteQuantity: t.Funds,
Time: types.Time(t.Timestamp.Time()),
}, nil
}
func convertWebSocketOrderUpdate(u max.OrderUpdate) (*types.Order, error) {
timeInForce := types.TimeInForceGTC
if u.OrderType == max.OrderTypeIOCLimit {
timeInForce = types.TimeInForceIOC
}
return &types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: u.ClientOID,
Symbol: toGlobalSymbol(u.Market),
Side: toGlobalSideType(u.Side),
Type: toGlobalOrderType(u.OrderType),
Quantity: u.Volume,
Price: u.Price,
StopPrice: u.StopPrice,
TimeInForce: timeInForce, // MAX only supports GTC
GroupID: u.GroupID,
},
Exchange: types.ExchangeMax,
OrderID: u.ID,
Status: toGlobalOrderStatus(u.State, u.ExecutedVolume, u.RemainingVolume),
ExecutedQuantity: u.ExecutedVolume,
CreationTime: types.Time(time.Unix(0, u.CreatedAtMs*int64(time.Millisecond))),
UpdateTime: types.Time(time.Unix(0, u.UpdateTime*int64(time.Millisecond))),
}, nil
}