From 0184b8c924146ef7e04274fa830bece3d62d2bb6 Mon Sep 17 00:00:00 2001 From: xiiiew <35555830+xiiiew@users.noreply.github.com> Date: Wed, 21 Sep 2022 10:51:17 +0800 Subject: [PATCH] binance/perf: Optimize UFuturesNewOrder function parameters (#1043) * pref(Binance.UFuturesNewOrder()):Optimize function parameters * perf(Binance.UFuturesNewOrder()):Optimize function parameters * Apply suggestions from code review Co-authored-by: Ryan O'Hara-Reid * fix: fix the linter issue * fix(binance_test): fix gofmt lint Co-authored-by: xw Co-authored-by: Ryan O'Hara-Reid --- exchanges/binance/binance_test.go | 11 ++++- exchanges/binance/binance_ufutures.go | 60 +++++++++++++-------------- exchanges/binance/binance_wrapper.go | 15 +++++-- exchanges/binance/ufutures_types.go | 25 ++++++++++- 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/exchanges/binance/binance_test.go b/exchanges/binance/binance_test.go index 8f99b6f5..9f717957 100644 --- a/exchanges/binance/binance_test.go +++ b/exchanges/binance/binance_test.go @@ -425,7 +425,16 @@ func TestUFuturesNewOrder(t *testing.T) { if !areTestAPIKeysSet() || !canManipulateRealOrders { t.Skip("skipping test: api keys not set or canManipulateRealOrders set to false") } - _, err := b.UFuturesNewOrder(context.Background(), currency.NewPair(currency.BTC, currency.USDT), "BUY", "", "LIMIT", "GTC", "", "", "", "", 1, 1, 0, 0, 0, false) + _, err := b.UFuturesNewOrder(context.Background(), + &UFuturesNewOrderRequest{ + Symbol: currency.NewPair(currency.BTC, currency.USDT), + Side: "BUY", + OrderType: "LIMIT", + TimeInForce: "GTC", + Quantity: 1, + Price: 1, + }, + ) if err != nil { t.Error(err) } diff --git a/exchanges/binance/binance_ufutures.go b/exchanges/binance/binance_ufutures.go index 9a4fdf55..09a8dfca 100644 --- a/exchanges/binance/binance_ufutures.go +++ b/exchanges/binance/binance_ufutures.go @@ -627,60 +627,58 @@ func (b *Binance) UCompositeIndexInfo(ctx context.Context, symbol currency.Pair) } // UFuturesNewOrder sends a new order for USDTMarginedFutures -func (b *Binance) UFuturesNewOrder(ctx context.Context, symbol currency.Pair, side, positionSide, orderType, timeInForce, - newClientOrderID, closePosition, workingType, newOrderRespType string, - quantity, price, stopPrice, activationPrice, callbackRate float64, reduceOnly bool) (UOrderData, error) { +func (b *Binance) UFuturesNewOrder(ctx context.Context, data *UFuturesNewOrderRequest) (UOrderData, error) { var resp UOrderData params := url.Values{} - symbolValue, err := b.FormatSymbol(symbol, asset.USDTMarginedFutures) + symbolValue, err := b.FormatSymbol(data.Symbol, asset.USDTMarginedFutures) if err != nil { return resp, err } params.Set("symbol", symbolValue) - params.Set("side", side) - if positionSide != "" { - if !common.StringDataCompare(validPositionSide, positionSide) { + params.Set("side", data.Side) + if data.PositionSide != "" { + if !common.StringDataCompare(validPositionSide, data.PositionSide) { return resp, errors.New("invalid positionSide") } - params.Set("positionSide", positionSide) + params.Set("positionSide", data.PositionSide) } - params.Set("type", orderType) - params.Set("timeInForce", timeInForce) - if reduceOnly { + params.Set("type", data.OrderType) + params.Set("timeInForce", data.TimeInForce) + if data.ReduceOnly { params.Set("reduceOnly", "true") } - if newClientOrderID != "" { - params.Set("newClientOrderID", newClientOrderID) + if data.NewClientOrderID != "" { + params.Set("newClientOrderID", data.NewClientOrderID) } - if closePosition != "" { - params.Set("closePosition", closePosition) + if data.ClosePosition != "" { + params.Set("closePosition", data.ClosePosition) } - if workingType != "" { - if !common.StringDataCompare(validWorkingType, workingType) { + if data.WorkingType != "" { + if !common.StringDataCompare(validWorkingType, data.WorkingType) { return resp, errors.New("invalid workingType") } - params.Set("workingType", workingType) + params.Set("workingType", data.WorkingType) } - if newOrderRespType != "" { - if !common.StringDataCompare(validNewOrderRespType, newOrderRespType) { + if data.NewOrderRespType != "" { + if !common.StringDataCompare(validNewOrderRespType, data.NewOrderRespType) { return resp, errors.New("invalid newOrderRespType") } - params.Set("newOrderRespType", newOrderRespType) + params.Set("newOrderRespType", data.NewOrderRespType) } - if quantity != 0 { - params.Set("quantity", strconv.FormatFloat(quantity, 'f', -1, 64)) + if data.Quantity != 0 { + params.Set("quantity", strconv.FormatFloat(data.Quantity, 'f', -1, 64)) } - if price != 0 { - params.Set("price", strconv.FormatFloat(price, 'f', -1, 64)) + if data.Price != 0 { + params.Set("price", strconv.FormatFloat(data.Price, 'f', -1, 64)) } - if stopPrice != 0 { - params.Set("stopPrice", strconv.FormatFloat(stopPrice, 'f', -1, 64)) + if data.StopPrice != 0 { + params.Set("stopPrice", strconv.FormatFloat(data.StopPrice, 'f', -1, 64)) } - if activationPrice != 0 { - params.Set("activationPrice", strconv.FormatFloat(activationPrice, 'f', -1, 64)) + if data.ActivationPrice != 0 { + params.Set("activationPrice", strconv.FormatFloat(data.ActivationPrice, 'f', -1, 64)) } - if callbackRate != 0 { - params.Set("callbackRate", strconv.FormatFloat(callbackRate, 'f', -1, 64)) + if data.CallbackRate != 0 { + params.Set("callbackRate", strconv.FormatFloat(data.CallbackRate, 'f', -1, 64)) } return resp, b.SendAuthHTTPRequest(ctx, exchange.RestUSDTMargined, http.MethodPost, ufuturesOrder, params, uFuturesOrdersDefaultRate, &resp) } diff --git a/exchanges/binance/binance_wrapper.go b/exchanges/binance/binance_wrapper.go index dfedbe9d..ee7715bd 100644 --- a/exchanges/binance/binance_wrapper.go +++ b/exchanges/binance/binance_wrapper.go @@ -1053,10 +1053,17 @@ func (b *Binance) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Subm return nil, errors.New("invalid type, check api docs for updates") } order, err := b.UFuturesNewOrder(ctx, - s.Pair, reqSide, - "", oType, "GTC", "", - s.ClientOrderID, "", "", - s.Amount, s.Price, 0, 0, 0, s.ReduceOnly) + &UFuturesNewOrderRequest{ + Symbol: s.Pair, + Side: reqSide, + OrderType: oType, + TimeInForce: "GTC", + NewClientOrderID: s.ClientOrderID, + Quantity: s.Amount, + Price: s.Price, + ReduceOnly: s.ReduceOnly, + }, + ) if err != nil { return nil, err } diff --git a/exchanges/binance/ufutures_types.go b/exchanges/binance/ufutures_types.go index a2a6a953..e25f1efb 100644 --- a/exchanges/binance/ufutures_types.go +++ b/exchanges/binance/ufutures_types.go @@ -1,6 +1,10 @@ package binance -import "time" +import ( + "time" + + "github.com/thrasher-corp/gocryptotrader/currency" +) var ( validFuturesIntervals = []string{ @@ -407,3 +411,22 @@ type UForceOrdersData struct { Time int64 `json:"time"` UpdateTime int64 `json:"updateTime"` } + +// UFuturesNewOrderRequest stores order data for placing +type UFuturesNewOrderRequest struct { + Symbol currency.Pair `json:"symbol"` + Side string `json:"side"` + PositionSide string `json:"position_side"` + OrderType string `json:"order_type"` + TimeInForce string `json:"time_in_force"` + NewClientOrderID string `json:"new_client_order_id"` + ClosePosition string `json:"close_position"` + WorkingType string `json:"working_type"` + NewOrderRespType string `json:"new_order_resp_type"` + Quantity float64 `json:"quantity"` + Price float64 `json:"price"` + StopPrice float64 `json:"stop_price"` + ActivationPrice float64 `json:"activation_price"` + CallbackRate float64 `json:"callback_rate"` + ReduceOnly bool `json:"reduce_only"` +}