Skip to content

Commit 29b236e

Browse files
authored
Make future order ModifyService (#605)
* Make future order ModifyService * add Service for FuturesApi Order Modifier
1 parent d2b44ab commit 29b236e

File tree

3 files changed

+281
-0
lines changed

3 files changed

+281
-0
lines changed

v2/futures/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@ func (c *Client) NewCreateOrderService() *CreateOrderService {
446446
return &CreateOrderService{c: c}
447447
}
448448

449+
// NewModifyOrderService init creating order service
450+
func (c *Client) NewModifyOrderService() *ModifyOrderService {
451+
return &ModifyOrderService{c: c}
452+
}
453+
449454
// NewCreateBatchOrdersService init creating batch order service
450455
func (c *Client) NewCreateBatchOrdersService() *CreateBatchOrdersService {
451456
return &CreateBatchOrdersService{c: c}

v2/futures/order_service.go

+204
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,210 @@ type CreateOrderResponse struct {
238238
RateLimitOrder1m string `json:"rateLimitOrder1m,omitempty"` //
239239
}
240240

241+
// ModifyOrderService create order
242+
type ModifyOrderService struct {
243+
c *Client
244+
symbol string
245+
orderID *int64
246+
origClientOrderID *string
247+
side SideType
248+
positionSide *PositionSideType
249+
orderType OrderType
250+
timeInForce *TimeInForceType
251+
quantity string
252+
reduceOnly *string
253+
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
262+
}
263+
264+
// Symbol set symbol
265+
func (s *ModifyOrderService) Symbol(symbol string) *ModifyOrderService {
266+
s.symbol = symbol
267+
return s
268+
}
269+
270+
func (s *ModifyOrderService) OrderID(orderID int64) *ModifyOrderService {
271+
s.orderID = &orderID
272+
return s
273+
}
274+
275+
func (s *ModifyOrderService) OrigClientOrderID(origClientOrderID string) *ModifyOrderService {
276+
s.origClientOrderID = &origClientOrderID
277+
return s
278+
}
279+
280+
// Side set side
281+
func (s *ModifyOrderService) Side(side SideType) *ModifyOrderService {
282+
s.side = side
283+
return s
284+
}
285+
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+
304+
// Quantity set quantity
305+
func (s *ModifyOrderService) Quantity(quantity string) *ModifyOrderService {
306+
s.quantity = quantity
307+
return s
308+
}
309+
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+
317+
// Price set price
318+
func (s *ModifyOrderService) Price(price string) *ModifyOrderService {
319+
s.price = &price
320+
return s
321+
}
322+
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
370+
return s
371+
}
372+
373+
func (s *ModifyOrderService) modifyOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {
374+
375+
r := &request{
376+
method: http.MethodPut,
377+
endpoint: endpoint,
378+
secType: secTypeSigned,
379+
}
380+
m := params{
381+
"symbol": s.symbol,
382+
"side": s.side,
383+
"type": s.orderType,
384+
"newOrderRespType": s.newOrderRespType,
385+
}
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
394+
}
395+
if s.reduceOnly != nil {
396+
m["reduceOnly"] = *s.reduceOnly
397+
}
398+
if s.price != nil {
399+
m["price"] = *s.price
400+
}
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
421+
}
422+
r.setFormParams(m)
423+
data, header, err = s.c.callAPI(ctx, r, opts...)
424+
if err != nil {
425+
return []byte{}, &http.Header{}, err
426+
}
427+
return data, header, nil
428+
}
429+
430+
// Do send request
431+
func (s *ModifyOrderService) Do(ctx context.Context, opts ...RequestOption) (res *Order, err error) {
432+
data, _, err := s.modifyOrder(ctx, "/fapi/v1/order", opts...)
433+
if err != nil {
434+
return nil, err
435+
}
436+
res = new(Order)
437+
err = json.Unmarshal(data, res)
438+
439+
if err != nil {
440+
return nil, err
441+
}
442+
return res, nil
443+
}
444+
241445
// ListOpenOrdersService list opened orders
242446
type ListOpenOrdersService struct {
243447
c *Client

v2/futures/order_service_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,78 @@ func (s *orderServiceTestSuite) TestListOrders() {
436436
s.assertOrderEqual(e, orders[0])
437437
}
438438

439+
func (s *orderServiceTestSuite) TestModifyOrder() {
440+
data := []byte(`{
441+
"clientOrderId": "myOrder1",
442+
"cumQty": "0",
443+
"cumQuote": "0",
444+
"executedQty": "0",
445+
"orderId": 283194212,
446+
"origQty": "11",
447+
"price": "8301",
448+
"reduceOnly": false,
449+
"side": "BUY",
450+
"status": "CANCELED",
451+
"stopPrice": "8300",
452+
"symbol": "BTCUSDT",
453+
"timeInForce": "GTC",
454+
"type": "TAKE_PROFIT",
455+
"updateTime": 1571110484038,
456+
"workingType": "CONTRACT_PRICE",
457+
"activatePrice": "10000",
458+
"priceRate":"0.1",
459+
"positionSide":"BOTH",
460+
"priceProtect": false
461+
}`)
462+
s.mockDo(data, nil)
463+
defer s.assertDo()
464+
465+
symbol := "BTCUSDT"
466+
orderID := int64(28)
467+
origClientOrderID := "myOrder1"
468+
price := "8301"
469+
side := SideTypeSell
470+
s.assertReq(func(r *request) {
471+
e := newSignedRequest().setFormParams(params{
472+
"symbol": symbol,
473+
"orderId": orderID,
474+
"origClientOrderId": origClientOrderID,
475+
"price": price,
476+
"side": side,
477+
})
478+
s.assertRequestEqual(e, r)
479+
})
480+
481+
res, err := s.client.NewModifyOrderService().Symbol(symbol).Side(side).
482+
OrderID(orderID).OrigClientOrderID(origClientOrderID).Price(price).
483+
Do(newContext())
484+
r := s.r()
485+
r.NoError(err)
486+
e := &Order{
487+
ClientOrderID: origClientOrderID,
488+
CumQuantity: "0",
489+
CumQuote: "0",
490+
ExecutedQuantity: "0",
491+
OrderID: 283194212,
492+
OrigQuantity: "11",
493+
Price: "8301",
494+
ReduceOnly: false,
495+
Side: SideTypeBuy,
496+
Status: OrderStatusTypeCanceled,
497+
StopPrice: "8300",
498+
Symbol: symbol,
499+
TimeInForce: TimeInForceTypeGTC,
500+
Type: OrderTypeTakeProfit,
501+
UpdateTime: 1571110484038,
502+
WorkingType: WorkingTypeContractPrice,
503+
ActivatePrice: "10000",
504+
PriceRate: "0.1",
505+
PositionSide: "BOTH",
506+
PriceProtect: false,
507+
}
508+
s.assertOrderEqual(e, res)
509+
}
510+
439511
func (s *orderServiceTestSuite) TestCancelOrder() {
440512
data := []byte(`{
441513
"clientOrderId": "myOrder1",

0 commit comments

Comments
 (0)