Skip to content

Commit

Permalink
Merge pull request #607 from lyro41/fix-modify-order-endpoint
Browse files Browse the repository at this point in the history
Fix Modify Order Endpoint : PUT /fapi/v1/order
  • Loading branch information
xyq-c-cpp authored Aug 13, 2024
2 parents 29b236e + a3f8064 commit 8405251
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 173 deletions.
14 changes: 14 additions & 0 deletions v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type OrderExecutionType string
// OrderStatusType define order status type
type OrderStatusType string

// PriceMatchType define priceMatch type
// Can't be passed together with price
type PriceMatchType string

// SymbolType define symbol type
type SymbolType string

Expand Down Expand Up @@ -117,6 +121,16 @@ const (
OrderStatusTypeNewInsurance OrderStatusType = "NEW_INSURANCE"
OrderStatusTypeNewADL OrderStatusType = "NEW_ADL"

PriceMatchTypeOpponent PriceMatchType = "OPPONENT"
PriceMatchTypeOpponent5 PriceMatchType = "OPPONENT_5"
PriceMatchTypeOpponent10 PriceMatchType = "OPPONENT_10"
PriceMatchTypeOpponent20 PriceMatchType = "OPPONENT_20"
PriceMatchTypeQueue PriceMatchType = "QUEUE"
PriceMatchTypeQueue5 PriceMatchType = "QUEUE_5"
PriceMatchTypeQueue10 PriceMatchType = "QUEUE_10"
PriceMatchTypeQueue20 PriceMatchType = "QUEUE_20"
PriceMatchTypeNone PriceMatchType = "NONE"

SymbolTypeFuture SymbolType = "FUTURE"

WorkingTypeMarkPrice WorkingType = "MARK_PRICE"
Expand Down
185 changes: 59 additions & 126 deletions v2/futures/order_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (s *CreateOrderService) ClosePosition(closePosition bool) *CreateOrderServi
}

func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {

r := &request{
method: http.MethodPost,
endpoint: endpoint,
Expand Down Expand Up @@ -241,24 +240,13 @@ type CreateOrderResponse struct {
// ModifyOrderService create order
type ModifyOrderService struct {
c *Client
symbol string
orderID *int64
origClientOrderID *string
symbol string
side SideType
positionSide *PositionSideType
orderType OrderType
timeInForce *TimeInForceType
quantity string
reduceOnly *string
price *string
newClientOrderID *string
stopPrice *string
workingType *WorkingType
activationPrice *string
callbackRate *string
priceProtect *string
newOrderRespType NewOrderRespType
closePosition *string
priceMatch *PriceMatchType
}

// Symbol set symbol
Expand All @@ -267,11 +255,13 @@ func (s *ModifyOrderService) Symbol(symbol string) *ModifyOrderService {
return s
}

// OrderID will prevail over OrigClientOrderID
func (s *ModifyOrderService) OrderID(orderID int64) *ModifyOrderService {
s.orderID = &orderID
return s
}

// OrigClientOrderID is not necessary if OrderID is provided
func (s *ModifyOrderService) OrigClientOrderID(origClientOrderID string) *ModifyOrderService {
s.origClientOrderID = &origClientOrderID
return s
Expand All @@ -283,141 +273,46 @@ func (s *ModifyOrderService) Side(side SideType) *ModifyOrderService {
return s
}

// PositionSide set side
func (s *ModifyOrderService) PositionSide(positionSide PositionSideType) *ModifyOrderService {
s.positionSide = &positionSide
return s
}

// Type set type
func (s *ModifyOrderService) Type(orderType OrderType) *ModifyOrderService {
s.orderType = orderType
return s
}

// TimeInForce set timeInForce
func (s *ModifyOrderService) TimeInForce(timeInForce TimeInForceType) *ModifyOrderService {
s.timeInForce = &timeInForce
return s
}

// Quantity set quantity
func (s *ModifyOrderService) Quantity(quantity string) *ModifyOrderService {
s.quantity = quantity
return s
}

// ReduceOnly set reduceOnly
func (s *ModifyOrderService) ReduceOnly(reduceOnly bool) *ModifyOrderService {
reduceOnlyStr := strconv.FormatBool(reduceOnly)
s.reduceOnly = &reduceOnlyStr
return s
}

// Price set price
func (s *ModifyOrderService) Price(price string) *ModifyOrderService {
s.price = &price
return s
}

// NewClientOrderID set newClientOrderID
func (s *ModifyOrderService) NewClientOrderID(newClientOrderID string) *ModifyOrderService {
s.newClientOrderID = &newClientOrderID
return s
}

// StopPrice set stopPrice
func (s *ModifyOrderService) StopPrice(stopPrice string) *ModifyOrderService {
s.stopPrice = &stopPrice
return s
}

// WorkingType set workingType
func (s *ModifyOrderService) WorkingType(workingType WorkingType) *ModifyOrderService {
s.workingType = &workingType
return s
}

// ActivationPrice set activationPrice
func (s *ModifyOrderService) ActivationPrice(activationPrice string) *ModifyOrderService {
s.activationPrice = &activationPrice
return s
}

// CallbackRate set callbackRate
func (s *ModifyOrderService) CallbackRate(callbackRate string) *ModifyOrderService {
s.callbackRate = &callbackRate
return s
}

// PriceProtect set priceProtect
func (s *ModifyOrderService) PriceProtect(priceProtect bool) *ModifyOrderService {
priceProtectStr := strconv.FormatBool(priceProtect)
s.priceProtect = &priceProtectStr
return s
}

// NewOrderResponseType set newOrderResponseType
func (s *ModifyOrderService) NewOrderResponseType(newOrderResponseType NewOrderRespType) *ModifyOrderService {
s.newOrderRespType = newOrderResponseType
return s
}

// ClosePosition set closePosition
func (s *ModifyOrderService) ClosePosition(closePosition bool) *ModifyOrderService {
closePositionStr := strconv.FormatBool(closePosition)
s.closePosition = &closePositionStr
// PriceMatch set priceMatch
func (s *ModifyOrderService) PriceMatch(priceMatch PriceMatchType) *ModifyOrderService {
s.priceMatch = &priceMatch
return s
}

func (s *ModifyOrderService) modifyOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {

r := &request{
method: http.MethodPut,
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
"side": s.side,
"type": s.orderType,
"newOrderRespType": s.newOrderRespType,
"symbol": s.symbol,
"side": s.side,
"quantity": s.quantity,
}
if s.quantity != "" {
m["quantity"] = s.quantity
}
if s.positionSide != nil {
m["positionSide"] = *s.positionSide
}
if s.timeInForce != nil {
m["timeInForce"] = *s.timeInForce
if s.orderID != nil {
m["orderId"] = *s.orderID
}
if s.reduceOnly != nil {
m["reduceOnly"] = *s.reduceOnly
if s.origClientOrderID != nil {
m["origClientOrderId"] = *s.origClientOrderID
}
if s.price != nil {
m["price"] = *s.price
}
if s.newClientOrderID != nil {
m["newClientOrderId"] = *s.newClientOrderID
}
if s.stopPrice != nil {
m["stopPrice"] = *s.stopPrice
}
if s.workingType != nil {
m["workingType"] = *s.workingType
}
if s.priceProtect != nil {
m["priceProtect"] = *s.priceProtect
}
if s.activationPrice != nil {
m["activationPrice"] = *s.activationPrice
}
if s.callbackRate != nil {
m["callbackRate"] = *s.callbackRate
}
if s.closePosition != nil {
m["closePosition"] = *s.closePosition
if s.priceMatch != nil {
m["priceMatch"] = *s.priceMatch
}
r.setFormParams(m)
data, header, err = s.c.callAPI(ctx, r, opts...)
Expand All @@ -427,13 +322,22 @@ func (s *ModifyOrderService) modifyOrder(ctx context.Context, endpoint string, o
return data, header, nil
}

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

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

type ModifyOrderResponse struct {
OrderID int64 `json:"orderId"`
Symbol string `json:"symbol"`
Pair string `json:"pair"`
Status OrderStatusType `json:"status"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
AveragePrice string `json:"avgPrice"`
OriginalQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CumulativeQuantity string `json:"cumQty"`
CumulativeBase string `json:"cumBase"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
ReduceOnly bool `json:"reduceOnly"`
ClosePosition bool `json:"closePosition"`
Side SideType `json:"side"`
PositionSide PositionSideType `json:"positionSide"`
StopPrice string `json:"stopPrice"`
WorkingType WorkingType `json:"workingType"`
PriceProtect bool `json:"priceProtect"` // if conditional order trigger is protected
OriginalType OrderType `json:"origType"`
PriceMatch PriceMatchType `json:"priceMatch"`
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
GoodTillDate int64 `json:"goodTillDate"` // order pre-set auto cancel time for TIF GTD order
UpdateTime int64 `json:"updateTime"`
}

// ListOpenOrdersService list opened orders
type ListOpenOrdersService struct {
c *Client
Expand Down Expand Up @@ -588,7 +520,7 @@ type Order struct {
ReduceOnly bool `json:"reduceOnly"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CumQuantity string `json:"cumQty"`
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
CumQuote string `json:"cumQuote"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Expand Down Expand Up @@ -737,7 +669,7 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
// CancelOrderResponse define response of canceling order
type CancelOrderResponse struct {
ClientOrderID string `json:"clientOrderId"`
CumQuantity string `json:"cumQty"`
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
CumQuote string `json:"cumQuote"`
ExecutedQuantity string `json:"executedQty"`
OrderID int64 `json:"orderId"`
Expand Down Expand Up @@ -874,6 +806,7 @@ func (s *ListLiquidationOrdersService) Limit(limit int) *ListLiquidationOrdersSe
}

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

0 comments on commit 8405251

Please sign in to comment.