Skip to content

Commit 8405251

Browse files
authored
Merge pull request #607 from lyro41/fix-modify-order-endpoint
Fix Modify Order Endpoint : PUT /fapi/v1/order
2 parents 29b236e + a3f8064 commit 8405251

File tree

3 files changed

+162
-173
lines changed

3 files changed

+162
-173
lines changed

v2/futures/client.go

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type OrderExecutionType string
3939
// OrderStatusType define order status type
4040
type OrderStatusType string
4141

42+
// PriceMatchType define priceMatch type
43+
// Can't be passed together with price
44+
type PriceMatchType string
45+
4246
// SymbolType define symbol type
4347
type SymbolType string
4448

@@ -117,6 +121,16 @@ const (
117121
OrderStatusTypeNewInsurance OrderStatusType = "NEW_INSURANCE"
118122
OrderStatusTypeNewADL OrderStatusType = "NEW_ADL"
119123

124+
PriceMatchTypeOpponent PriceMatchType = "OPPONENT"
125+
PriceMatchTypeOpponent5 PriceMatchType = "OPPONENT_5"
126+
PriceMatchTypeOpponent10 PriceMatchType = "OPPONENT_10"
127+
PriceMatchTypeOpponent20 PriceMatchType = "OPPONENT_20"
128+
PriceMatchTypeQueue PriceMatchType = "QUEUE"
129+
PriceMatchTypeQueue5 PriceMatchType = "QUEUE_5"
130+
PriceMatchTypeQueue10 PriceMatchType = "QUEUE_10"
131+
PriceMatchTypeQueue20 PriceMatchType = "QUEUE_20"
132+
PriceMatchTypeNone PriceMatchType = "NONE"
133+
120134
SymbolTypeFuture SymbolType = "FUTURE"
121135

122136
WorkingTypeMarkPrice WorkingType = "MARK_PRICE"

v2/futures/order_service.go

+59-126
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ func (s *CreateOrderService) ClosePosition(closePosition bool) *CreateOrderServi
133133
}
134134

135135
func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {
136-
137136
r := &request{
138137
method: http.MethodPost,
139138
endpoint: endpoint,
@@ -241,24 +240,13 @@ type CreateOrderResponse struct {
241240
// ModifyOrderService create order
242241
type ModifyOrderService struct {
243242
c *Client
244-
symbol string
245243
orderID *int64
246244
origClientOrderID *string
245+
symbol string
247246
side SideType
248-
positionSide *PositionSideType
249-
orderType OrderType
250-
timeInForce *TimeInForceType
251247
quantity string
252-
reduceOnly *string
253248
price *string
254-
newClientOrderID *string
255-
stopPrice *string
256-
workingType *WorkingType
257-
activationPrice *string
258-
callbackRate *string
259-
priceProtect *string
260-
newOrderRespType NewOrderRespType
261-
closePosition *string
249+
priceMatch *PriceMatchType
262250
}
263251

264252
// Symbol set symbol
@@ -267,11 +255,13 @@ func (s *ModifyOrderService) Symbol(symbol string) *ModifyOrderService {
267255
return s
268256
}
269257

258+
// OrderID will prevail over OrigClientOrderID
270259
func (s *ModifyOrderService) OrderID(orderID int64) *ModifyOrderService {
271260
s.orderID = &orderID
272261
return s
273262
}
274263

264+
// OrigClientOrderID is not necessary if OrderID is provided
275265
func (s *ModifyOrderService) OrigClientOrderID(origClientOrderID string) *ModifyOrderService {
276266
s.origClientOrderID = &origClientOrderID
277267
return s
@@ -283,141 +273,46 @@ func (s *ModifyOrderService) Side(side SideType) *ModifyOrderService {
283273
return s
284274
}
285275

286-
// PositionSide set side
287-
func (s *ModifyOrderService) PositionSide(positionSide PositionSideType) *ModifyOrderService {
288-
s.positionSide = &positionSide
289-
return s
290-
}
291-
292-
// Type set type
293-
func (s *ModifyOrderService) Type(orderType OrderType) *ModifyOrderService {
294-
s.orderType = orderType
295-
return s
296-
}
297-
298-
// TimeInForce set timeInForce
299-
func (s *ModifyOrderService) TimeInForce(timeInForce TimeInForceType) *ModifyOrderService {
300-
s.timeInForce = &timeInForce
301-
return s
302-
}
303-
304276
// Quantity set quantity
305277
func (s *ModifyOrderService) Quantity(quantity string) *ModifyOrderService {
306278
s.quantity = quantity
307279
return s
308280
}
309281

310-
// ReduceOnly set reduceOnly
311-
func (s *ModifyOrderService) ReduceOnly(reduceOnly bool) *ModifyOrderService {
312-
reduceOnlyStr := strconv.FormatBool(reduceOnly)
313-
s.reduceOnly = &reduceOnlyStr
314-
return s
315-
}
316-
317282
// Price set price
318283
func (s *ModifyOrderService) Price(price string) *ModifyOrderService {
319284
s.price = &price
320285
return s
321286
}
322287

323-
// NewClientOrderID set newClientOrderID
324-
func (s *ModifyOrderService) NewClientOrderID(newClientOrderID string) *ModifyOrderService {
325-
s.newClientOrderID = &newClientOrderID
326-
return s
327-
}
328-
329-
// StopPrice set stopPrice
330-
func (s *ModifyOrderService) StopPrice(stopPrice string) *ModifyOrderService {
331-
s.stopPrice = &stopPrice
332-
return s
333-
}
334-
335-
// WorkingType set workingType
336-
func (s *ModifyOrderService) WorkingType(workingType WorkingType) *ModifyOrderService {
337-
s.workingType = &workingType
338-
return s
339-
}
340-
341-
// ActivationPrice set activationPrice
342-
func (s *ModifyOrderService) ActivationPrice(activationPrice string) *ModifyOrderService {
343-
s.activationPrice = &activationPrice
344-
return s
345-
}
346-
347-
// CallbackRate set callbackRate
348-
func (s *ModifyOrderService) CallbackRate(callbackRate string) *ModifyOrderService {
349-
s.callbackRate = &callbackRate
350-
return s
351-
}
352-
353-
// PriceProtect set priceProtect
354-
func (s *ModifyOrderService) PriceProtect(priceProtect bool) *ModifyOrderService {
355-
priceProtectStr := strconv.FormatBool(priceProtect)
356-
s.priceProtect = &priceProtectStr
357-
return s
358-
}
359-
360-
// NewOrderResponseType set newOrderResponseType
361-
func (s *ModifyOrderService) NewOrderResponseType(newOrderResponseType NewOrderRespType) *ModifyOrderService {
362-
s.newOrderRespType = newOrderResponseType
363-
return s
364-
}
365-
366-
// ClosePosition set closePosition
367-
func (s *ModifyOrderService) ClosePosition(closePosition bool) *ModifyOrderService {
368-
closePositionStr := strconv.FormatBool(closePosition)
369-
s.closePosition = &closePositionStr
288+
// PriceMatch set priceMatch
289+
func (s *ModifyOrderService) PriceMatch(priceMatch PriceMatchType) *ModifyOrderService {
290+
s.priceMatch = &priceMatch
370291
return s
371292
}
372293

373294
func (s *ModifyOrderService) modifyOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {
374-
375295
r := &request{
376296
method: http.MethodPut,
377297
endpoint: endpoint,
378298
secType: secTypeSigned,
379299
}
380300
m := params{
381-
"symbol": s.symbol,
382-
"side": s.side,
383-
"type": s.orderType,
384-
"newOrderRespType": s.newOrderRespType,
301+
"symbol": s.symbol,
302+
"side": s.side,
303+
"quantity": s.quantity,
385304
}
386-
if s.quantity != "" {
387-
m["quantity"] = s.quantity
388-
}
389-
if s.positionSide != nil {
390-
m["positionSide"] = *s.positionSide
391-
}
392-
if s.timeInForce != nil {
393-
m["timeInForce"] = *s.timeInForce
305+
if s.orderID != nil {
306+
m["orderId"] = *s.orderID
394307
}
395-
if s.reduceOnly != nil {
396-
m["reduceOnly"] = *s.reduceOnly
308+
if s.origClientOrderID != nil {
309+
m["origClientOrderId"] = *s.origClientOrderID
397310
}
398311
if s.price != nil {
399312
m["price"] = *s.price
400313
}
401-
if s.newClientOrderID != nil {
402-
m["newClientOrderId"] = *s.newClientOrderID
403-
}
404-
if s.stopPrice != nil {
405-
m["stopPrice"] = *s.stopPrice
406-
}
407-
if s.workingType != nil {
408-
m["workingType"] = *s.workingType
409-
}
410-
if s.priceProtect != nil {
411-
m["priceProtect"] = *s.priceProtect
412-
}
413-
if s.activationPrice != nil {
414-
m["activationPrice"] = *s.activationPrice
415-
}
416-
if s.callbackRate != nil {
417-
m["callbackRate"] = *s.callbackRate
418-
}
419-
if s.closePosition != nil {
420-
m["closePosition"] = *s.closePosition
314+
if s.priceMatch != nil {
315+
m["priceMatch"] = *s.priceMatch
421316
}
422317
r.setFormParams(m)
423318
data, header, err = s.c.callAPI(ctx, r, opts...)
@@ -427,13 +322,22 @@ func (s *ModifyOrderService) modifyOrder(ctx context.Context, endpoint string, o
427322
return data, header, nil
428323
}
429324

430-
// Do send request
431-
func (s *ModifyOrderService) Do(ctx context.Context, opts ...RequestOption) (res *Order, err error) {
325+
// Do send request:
326+
// - Either orderId or origClientOrderId must be sent, and the orderId will prevail if both are sent
327+
// - Either price or priceMatch must be sent. Sending both will fail the request
328+
// - When the new quantity or price doesn't satisfy PriceFilter / PercentPriceFilter / LotSizeFilter,
329+
// amendment will be rejected and the order will stay as it is
330+
// - However the order will be cancelled by the amendment in the following situations:
331+
// -- when the order is in partially filled status and the new quantity <= executedQty
332+
// -- when the order is TimeInForceTypeGTX and the new price will cause it to be executed immediately
333+
// - One order can only be modified for less than 10000 times
334+
// - Will set ModifyOrderResponse.SelfTradePreventionMode to "NONE"
335+
func (s *ModifyOrderService) Do(ctx context.Context, opts ...RequestOption) (res *ModifyOrderResponse, err error) {
432336
data, _, err := s.modifyOrder(ctx, "/fapi/v1/order", opts...)
433337
if err != nil {
434338
return nil, err
435339
}
436-
res = new(Order)
340+
res = new(ModifyOrderResponse)
437341
err = json.Unmarshal(data, res)
438342

439343
if err != nil {
@@ -442,6 +346,34 @@ func (s *ModifyOrderService) Do(ctx context.Context, opts ...RequestOption) (res
442346
return res, nil
443347
}
444348

349+
type ModifyOrderResponse struct {
350+
OrderID int64 `json:"orderId"`
351+
Symbol string `json:"symbol"`
352+
Pair string `json:"pair"`
353+
Status OrderStatusType `json:"status"`
354+
ClientOrderID string `json:"clientOrderId"`
355+
Price string `json:"price"`
356+
AveragePrice string `json:"avgPrice"`
357+
OriginalQuantity string `json:"origQty"`
358+
ExecutedQuantity string `json:"executedQty"`
359+
CumulativeQuantity string `json:"cumQty"`
360+
CumulativeBase string `json:"cumBase"`
361+
TimeInForce TimeInForceType `json:"timeInForce"`
362+
Type OrderType `json:"type"`
363+
ReduceOnly bool `json:"reduceOnly"`
364+
ClosePosition bool `json:"closePosition"`
365+
Side SideType `json:"side"`
366+
PositionSide PositionSideType `json:"positionSide"`
367+
StopPrice string `json:"stopPrice"`
368+
WorkingType WorkingType `json:"workingType"`
369+
PriceProtect bool `json:"priceProtect"` // if conditional order trigger is protected
370+
OriginalType OrderType `json:"origType"`
371+
PriceMatch PriceMatchType `json:"priceMatch"`
372+
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
373+
GoodTillDate int64 `json:"goodTillDate"` // order pre-set auto cancel time for TIF GTD order
374+
UpdateTime int64 `json:"updateTime"`
375+
}
376+
445377
// ListOpenOrdersService list opened orders
446378
type ListOpenOrdersService struct {
447379
c *Client
@@ -588,7 +520,7 @@ type Order struct {
588520
ReduceOnly bool `json:"reduceOnly"`
589521
OrigQuantity string `json:"origQty"`
590522
ExecutedQuantity string `json:"executedQty"`
591-
CumQuantity string `json:"cumQty"`
523+
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
592524
CumQuote string `json:"cumQuote"`
593525
Status OrderStatusType `json:"status"`
594526
TimeInForce TimeInForceType `json:"timeInForce"`
@@ -737,7 +669,7 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
737669
// CancelOrderResponse define response of canceling order
738670
type CancelOrderResponse struct {
739671
ClientOrderID string `json:"clientOrderId"`
740-
CumQuantity string `json:"cumQty"`
672+
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
741673
CumQuote string `json:"cumQuote"`
742674
ExecutedQuantity string `json:"executedQty"`
743675
OrderID int64 `json:"orderId"`
@@ -874,6 +806,7 @@ func (s *ListLiquidationOrdersService) Limit(limit int) *ListLiquidationOrdersSe
874806
}
875807

876808
// Do send request
809+
// Deprecated: use /fapi/v1/forceOrders instead
877810
func (s *ListLiquidationOrdersService) Do(ctx context.Context, opts ...RequestOption) (res []*LiquidationOrder, err error) {
878811
r := &request{
879812
method: http.MethodGet,

0 commit comments

Comments
 (0)