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 <oharareid.ryan@gmail.com>

* fix: fix the linter issue

* fix(binance_test): fix gofmt lint

Co-authored-by: xw <xw@xwdeiMac.local>
Co-authored-by: Ryan O'Hara-Reid <oharareid.ryan@gmail.com>
This commit is contained in:
xiiiew
2022-09-21 10:51:17 +08:00
committed by GitHub
parent 0b6916cd45
commit 0184b8c924
4 changed files with 74 additions and 37 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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"`
}