From 5fd0ab4cd3472d4862e7cd52ae83323f56f3296a Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 7 Jun 2021 01:00:01 +0800 Subject: [PATCH] skip client order id when no client order is given --- pkg/exchange/binance/exchange.go | 16 +++++++--------- pkg/exchange/max/client_order_id.go | 10 +++++++++- pkg/exchange/max/exchange.go | 29 ++++++++++++++++++----------- pkg/types/order.go | 2 ++ 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index 98f31e0cc..ccbf2283a 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -522,19 +522,13 @@ func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrde return nil, err } - clientOrderID := uuid.New().String() - if len(order.ClientOrderID) > 0 { - clientOrderID = order.ClientOrderID - } + clientOrderID := newSpotClientOrderID(order.ClientOrderID) req := e.Client.NewCreateMarginOrderService(). Symbol(order.Symbol). Type(orderType). - Side(binance.SideType(order.Side)) - - if len(clientOrderID) > 0 { - req.NewClientOrderID(clientOrderID) - } + Side(binance.SideType(order.Side)). + NewClientOrderID(clientOrderID) // use response result format req.NewOrderRespType(binance.NewOrderRespTypeRESULT) @@ -618,6 +612,10 @@ func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrde const spotBrokerID = "NSUYEBKM" func newSpotClientOrderID(originalID string) (clientOrderID string) { + if originalID == types.NoClientOrderID { + return "" + } + prefix := "x-" + spotBrokerID prefixLen := len(prefix) diff --git a/pkg/exchange/max/client_order_id.go b/pkg/exchange/max/client_order_id.go index a24aba213..89836c6a2 100644 --- a/pkg/exchange/max/client_order_id.go +++ b/pkg/exchange/max/client_order_id.go @@ -1,11 +1,19 @@ package max -import "github.com/google/uuid" +import ( + "github.com/c9s/bbgo/pkg/types" + "github.com/google/uuid" +) // BBGO is a broker on MAX const spotBrokerID = "bbgo" func NewClientOrderID(originalID string, tags ...string) (clientOrderID string) { + // skip blank client order ID + if originalID == types.NoClientOrderID { + return "" + } + prefix := "x-" + spotBrokerID + "-" for _, tag := range tags { diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index e9829dc63..7243fee2f 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -286,7 +286,7 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err var req = e.client.OrderService.NewOrderCancelRequest() if o.OrderID > 0 { req.ID(o.OrderID) - } else if len(o.ClientOrderID) > 0 { + } else if len(o.ClientOrderID) > 0 && o.ClientOrderID != types.NoClientOrderID { req.ClientOrderID(o.ClientOrderID) } else { return fmt.Errorf("order id or client order id is not defined, order=%+v", o) @@ -308,14 +308,12 @@ func toMaxSubmitOrder(o types.SubmitOrder) (*maxapi.Order, error) { return nil, err } - clientOrderID := NewClientOrderID(o.ClientOrderID) - volumeInString := o.QuantityString if len(volumeInString) == 0 { if o.Market.Symbol != "" { volumeInString = o.Market.FormatQuantity(o.Quantity) } else { - volumeInString = strconv.FormatFloat(o.Quantity, 'f', 8, 64) + volumeInString = strconv.FormatFloat(o.Quantity, 'f', -1, 64) } } @@ -324,9 +322,16 @@ func toMaxSubmitOrder(o types.SubmitOrder) (*maxapi.Order, error) { Side: toLocalSideType(o.Side), OrderType: orderType, // Price: priceInString, - Volume: volumeInString, - GroupID: o.GroupID, - ClientOID: clientOrderID, + Volume: volumeInString, + } + + if o.GroupID > 0 { + maxOrder.GroupID = o.GroupID + } + + clientOrderID := NewClientOrderID(o.ClientOrderID) + if len(clientOrderID) > 0 { + maxOrder.ClientOID = clientOrderID } switch o.Type { @@ -336,7 +341,7 @@ func toMaxSubmitOrder(o types.SubmitOrder) (*maxapi.Order, error) { if o.Market.Symbol != "" { priceInString = o.Market.FormatPrice(o.Price) } else { - priceInString = strconv.FormatFloat(o.Price, 'f', 8, 64) + priceInString = strconv.FormatFloat(o.Price, 'f', -1, 64) } } maxOrder.Price = priceInString @@ -451,12 +456,14 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder return createdOrders, err } - // TODO: replace OrderType string type req := e.client.OrderService.NewCreateOrderRequest(). Market(maxOrder.Market). Side(maxOrder.Side). - OrderType(string(maxOrder.OrderType)). - ClientOrderID(maxOrder.ClientOID) + OrderType(string(maxOrder.OrderType)) + + if len(maxOrder.ClientOID) > 0 { + req.ClientOrderID(maxOrder.ClientOID) + } if len(maxOrder.Volume) > 0 { req.Volume(maxOrder.Volume) diff --git a/pkg/types/order.go b/pkg/types/order.go index fc5439cde..cf1bb2943 100644 --- a/pkg/types/order.go +++ b/pkg/types/order.go @@ -79,6 +79,8 @@ func (t *OrderType) Scan(v interface{}) error { } */ +const NoClientOrderID = "0" + type OrderStatus string const (