-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
v5_order_service.go
386 lines (325 loc) · 12.7 KB
/
v5_order_service.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package bybit
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
)
// V5OrderServiceI :
type V5OrderServiceI interface {
CreateOrder(V5CreateOrderParam) (*V5CreateOrderResponse, error)
AmendOrder(V5AmendOrderParam) (*V5AmendOrderResponse, error)
CancelOrder(V5CancelOrderParam) (*V5CancelOrderResponse, error)
GetOpenOrders(V5GetOpenOrdersParam) (*V5GetOrdersResponse, error)
CancelAllOrders(V5CancelAllOrdersParam) (*V5CancelAllOrdersResponse, error)
GetHistoryOrders(V5GetHistoryOrdersParam) (*V5GetOrdersResponse, error)
}
// V5OrderService :
type V5OrderService struct {
client *Client
}
// V5CreateOrderParam :
type V5CreateOrderParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
Side Side `json:"side"`
OrderType OrderType `json:"orderType"`
Qty string `json:"qty"`
IsLeverage *IsLeverage `json:"isLeverage,omitempty"`
Price *string `json:"price,omitempty"`
TriggerDirection *TriggerDirection `json:"triggerDirection,omitempty"`
OrderFilter *OrderFilter `json:"orderFilter,omitempty"` // If not passed, Order by default
TriggerPrice *string `json:"triggerPrice,omitempty"`
TriggerBy *TriggerBy `json:"triggerBy,omitempty"`
OrderIv *string `json:"orderIv,omitempty"` // option only.
TimeInForce *TimeInForce `json:"timeInForce,omitempty"` // If not passed, GTC is used by default
PositionIdx *PositionIdx `json:"positionIdx,omitempty"` // Under hedge-mode, this param is required
OrderLinkID *string `json:"orderLinkId,omitempty"`
TakeProfit *string `json:"takeProfit,omitempty"`
StopLoss *string `json:"stopLoss,omitempty"`
TpTriggerBy *TriggerBy `json:"tpTriggerBy,omitempty"`
SlTriggerBy *TriggerBy `json:"slTriggerBy,omitempty"`
ReduceOnly *bool `json:"reduce_only,omitempty"`
CloseOnTrigger *bool `json:"closeOnTrigger,omitempty"`
SmpType *string `json:"smpType,omitempty"`
MarketMakerProtection *bool `json:"mmp,omitempty"` // option only
TpSlMode *TpSlMode `json:"tpslMode,omitempty"`
TpLimitPrice *string `json:"tpLimitPrice,omitempty"`
SlLimitPrice *string `json:"slLimitPrice,omitempty"`
TpOrderType *OrderType `json:"tpOrderType,omitempty"`
SlOrderType *OrderType `json:"slOrderType,omitempty"`
MarketUnit *MarketUnit `json:"marketUnit,omitempty"` // The unit for qty when create Spot market orders for UTA account.
}
// V5CreateOrderResponse :
type V5CreateOrderResponse struct {
CommonV5Response `json:",inline"`
Result V5CreateOrderResult `json:"result"`
}
// V5CreateOrderResult :
type V5CreateOrderResult struct {
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
}
// CreateOrder :
func (s *V5OrderService) CreateOrder(param V5CreateOrderParam) (*V5CreateOrderResponse, error) {
var res V5CreateOrderResponse
body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}
if err := s.client.postV5JSON("/v5/order/create", body, &res); err != nil {
return &res, err
}
return &res, nil
}
// V5AmendOrderParam :
type V5AmendOrderParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
OrderID *string `json:"orderId,omitempty"`
OrderLinkID *string `json:"orderLinkId,omitempty"`
OrderIv *string `json:"orderIv,omitempty"`
TriggerPrice *string `json:"triggerPrice,omitempty"`
Qty *string `json:"qty,omitempty"`
Price *string `json:"price,omitempty"`
TakeProfit *string `json:"takeProfit,omitempty"`
StopLoss *string `json:"stopLoss,omitempty"`
TpTriggerBy *TriggerBy `json:"tpTriggerBy,omitempty"`
SlTriggerBy *TriggerBy `json:"slTriggerBy,omitempty"`
TriggerBy *TriggerBy `json:"triggerBy,omitempty"`
}
func (p V5AmendOrderParam) validate() error {
if p.OrderID == nil && p.OrderLinkID == nil {
return fmt.Errorf("orderId or orderLinkId must be passed")
}
if p.Category != CategoryV5Option && p.OrderIv != nil {
return fmt.Errorf("orderIv is for option only")
}
return nil
}
// V5AmendOrderResponse :
type V5AmendOrderResponse struct {
CommonV5Response `json:",inline"`
Result V5AmendOrderResult `json:"result"`
}
// V5AmendOrderResult :
type V5AmendOrderResult struct {
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
}
// AmendOrder :
func (s *V5OrderService) AmendOrder(param V5AmendOrderParam) (*V5AmendOrderResponse, error) {
var res V5AmendOrderResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}
if err := s.client.postV5JSON("/v5/order/amend", body, &res); err != nil {
return &res, err
}
return &res, nil
}
// V5CancelOrderParam :
type V5CancelOrderParam struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
OrderID *string `json:"orderId,omitempty"`
OrderLinkID *string `json:"orderLinkId,omitempty"`
OrderFilter *OrderFilter `json:"orderFilter,omitempty"` // If not passed, Order by default
}
// V5CancelOrderResponse :
type V5CancelOrderResponse struct {
CommonV5Response `json:",inline"`
Result V5CancelOrderResult `json:"result"`
}
// V5CancelOrderResult :
type V5CancelOrderResult struct {
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
}
// CancelOrder :
func (s *V5OrderService) CancelOrder(param V5CancelOrderParam) (*V5CancelOrderResponse, error) {
var res V5CancelOrderResponse
if param.OrderID == nil && param.OrderLinkID == nil {
return nil, fmt.Errorf("either OrderID or OrderLinkID needed")
}
body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}
if err := s.client.postV5JSON("/v5/order/cancel", body, &res); err != nil {
return &res, err
}
return &res, nil
}
// V5GetOpenOrdersParam :
type V5GetOpenOrdersParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"`
SettleCoin *Coin `url:"settleCoin,omitempty"`
OrderID *string `url:"orderId,omitempty"`
OrderLinkID *string `url:"orderLinkId,omitempty"`
OpenOnly *int `url:"openOnly,omitempty"`
OrderFilter *OrderFilter `url:"orderFilter,omitempty"` // If not passed, Order by default
Limit *int `url:"limit,omitempty"`
Cursor *string `url:"cursor,omitempty"`
}
// V5GetGetHistoryOrderssParam :
type V5GetHistoryOrdersParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"`
OrderID *string `url:"orderId,omitempty"`
OrderLinkID *string `url:"orderLinkId,omitempty"`
OrderFilter *OrderFilter `url:"orderFilter,omitempty"` // If not passed, Order by default
OrderStatus *OrderStatus `url:"orderStatus,omitempty"`
StartTime *int `url:"startTime,omitempty"`
EndTime *int `url:"endTime,omitempty"`
Limit *int `url:"limit,omitempty"`
Cursor *string `url:"cursor,omitempty"`
}
// V5GetOrdersResponse :
type V5GetOrdersResponse struct {
CommonV5Response `json:",inline"`
Result V5GetOrdersResult `json:"result"`
}
// V5GetOrdersResult :
type V5GetOrdersResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List []V5GetOrder `json:"list"`
}
type V5GetOrder struct {
Symbol SymbolV5 `json:"symbol"`
OrderType OrderType `json:"orderType"`
OrderLinkID string `json:"orderLinkId"`
OrderID string `json:"orderId"`
CancelType string `json:"cancelType"`
AvgPrice string `json:"avgPrice"`
StopOrderType string `json:"stopOrderType"`
LastPriceOnCreated string `json:"lastPriceOnCreated"`
OrderStatus OrderStatus `json:"orderStatus"`
TakeProfit string `json:"takeProfit"`
CumExecValue string `json:"cumExecValue"`
TriggerDirection int `json:"triggerDirection"`
IsLeverage string `json:"isLeverage"`
RejectReason string `json:"rejectReason"`
Price string `json:"price"`
OrderIv string `json:"orderIv"`
CreatedTime string `json:"createdTime"`
TpTriggerBy string `json:"tpTriggerBy"`
PositionIdx int `json:"positionIdx"`
TimeInForce TimeInForce `json:"timeInForce"`
LeavesValue string `json:"leavesValue"`
UpdatedTime string `json:"updatedTime"`
Side Side `json:"side"`
TriggerPrice string `json:"triggerPrice"`
CumExecFee string `json:"cumExecFee"`
LeavesQty string `json:"leavesQty"`
SlTriggerBy string `json:"slTriggerBy"`
CloseOnTrigger bool `json:"closeOnTrigger"`
CumExecQty string `json:"cumExecQty"`
ReduceOnly bool `json:"reduceOnly"`
Qty string `json:"qty"`
StopLoss string `json:"stopLoss"`
TriggerBy TriggerBy `json:"triggerBy"`
}
// GetOpenOrders :
func (s *V5OrderService) GetOpenOrders(param V5GetOpenOrdersParam) (*V5GetOrdersResponse, error) {
var res V5GetOrdersResponse
if param.Category == "" {
return nil, fmt.Errorf("category needed")
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/order/realtime", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetHistoryOrders :
func (s *V5OrderService) GetHistoryOrders(param V5GetHistoryOrdersParam) (*V5GetOrdersResponse, error) {
var res V5GetOrdersResponse
if param.Category == "" {
return nil, fmt.Errorf("category needed")
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/order/history", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5CancelAllOrdersParam :
// If you pass multiple of these params, the system will process one of param, which priority is symbol > baseCoin > settleCoin.
type V5CancelAllOrdersParam struct {
Category CategoryV5 `json:"category"`
Symbol *SymbolV5 `json:"symbol,omitempty"`
BaseCoin *Coin `json:"baseCoin,omitempty"`
SettleCoin *Coin `json:"settleCoin,omitempty"`
OrderFilter *OrderFilter `json:"orderFilter,omitempty"` // If not passed, Order by default
}
func (p V5CancelAllOrdersParam) validate() error {
if p.Category == CategoryV5Linear || p.Category == CategoryV5Inverse {
if p.Symbol == nil && p.BaseCoin == nil && p.SettleCoin == nil {
return fmt.Errorf("symbol or baseCoin or settleCoin is needed for linear and inverse")
}
}
if p.Category != CategoryV5Spot && p.OrderFilter != nil {
return fmt.Errorf("orderFilter is for spot only")
}
return nil
}
// V5CancelAllOrdersResponse :
type V5CancelAllOrdersResponse struct {
CommonV5Response `json:",inline"`
Result V5CancelAllOrdersResult `json:"result"`
}
// V5CancelAllOrdersResult :
type V5CancelAllOrdersResult struct {
LinearInverseOption *V5CancelAllOrdersLinearInverseOptionResult
Spot *V5CancelAllOrdersSpotResult
}
// UnmarshalJSON :
func (r *V5CancelAllOrdersResult) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &r.LinearInverseOption); err != nil {
return err
}
if err := json.Unmarshal(data, &r.Spot); err != nil {
return err
}
return nil
}
// V5CancelAllOrdersLinearInverseOptionResult :
type V5CancelAllOrdersLinearInverseOptionResult struct {
List []struct {
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
} `json:"list"`
}
// V5CancelAllOrdersSpotResult :
type V5CancelAllOrdersSpotResult struct {
Success string `json:"success"` // 1: success, 0: fail
}
// CancelAllOrders :
func (s *V5OrderService) CancelAllOrders(param V5CancelAllOrdersParam) (*V5CancelAllOrdersResponse, error) {
var res V5CancelAllOrdersResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
body, err := json.Marshal(param)
if err != nil {
return &res, fmt.Errorf("json marshal: %w", err)
}
if err := s.client.postV5JSON("/v5/order/cancel-all", body, &res); err != nil {
return &res, err
}
return &res, nil
}