From 523d9b3071d40a45e8ccdd84d5024c5838d575fd Mon Sep 17 00:00:00 2001 From: Lee Date: Wed, 12 Jan 2022 03:47:12 +0800 Subject: [PATCH] ftx: Support LIMIT_MAKER and IOC_LIMIT order type --- pkg/exchange/ftx/convert.go | 26 +++++++++++++++++++++ pkg/exchange/ftx/convert_test.go | 40 ++++++++++++++++++++++++++++++++ pkg/exchange/ftx/exchange.go | 13 +++++++---- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/pkg/exchange/ftx/convert.go b/pkg/exchange/ftx/convert.go index 039fdeaf0..b67e72c89 100644 --- a/pkg/exchange/ftx/convert.go +++ b/pkg/exchange/ftx/convert.go @@ -152,3 +152,29 @@ func toGlobalKLine(symbol string, interval types.Interval, h Candle) (types.KLin Closed: true, }, nil } + +type OrderType string + +const ( + OrderTypeLimit OrderType = "limit" + OrderTypeMarket OrderType = "market" +) + +func toLocalOrderType(orderType types.OrderType) (OrderType, bool, bool, error) { + switch orderType { + + case types.OrderTypeLimitMaker: + return OrderTypeLimit, true, false, nil + + case types.OrderTypeLimit: + return OrderTypeLimit, false, false, nil + + case types.OrderTypeMarket: + return OrderTypeMarket, false, false, nil + + case types.OrderTypeIOCLimit: + return OrderTypeLimit, false, true, nil + } + + return "", false, false, fmt.Errorf("order type %s not supported", orderType) +} diff --git a/pkg/exchange/ftx/convert_test.go b/pkg/exchange/ftx/convert_test.go index 540424259..bfe4de7e2 100644 --- a/pkg/exchange/ftx/convert_test.go +++ b/pkg/exchange/ftx/convert_test.go @@ -100,3 +100,43 @@ func TestTrimLowerString(t *testing.T) { func Test_toGlobalSymbol(t *testing.T) { assert.Equal(t, "BTCUSDT", toGlobalSymbol("BTC/USDT")) } + +func Test_toLocalOrderTypeWithLimitMaker(t *testing.T) { + + orderType, postOnly, IOC, err := toLocalOrderType(types.OrderTypeLimitMaker) + + assert.NoError(t, err) + assert.Equal(t, orderType, OrderTypeLimit) + assert.Equal(t, postOnly, true) + assert.Equal(t, IOC, false) +} + +func Test_toLocalOrderTypeWithLimit(t *testing.T) { + + orderType, postOnly, IOC, err := toLocalOrderType(types.OrderTypeLimit) + + assert.NoError(t, err) + assert.Equal(t, orderType, OrderTypeLimit) + assert.Equal(t, postOnly, false) + assert.Equal(t, IOC, false) +} + +func Test_toLocalOrderTypeWithMarket(t *testing.T) { + + orderType, postOnly, IOC, err := toLocalOrderType(types.OrderTypeMarket) + + assert.NoError(t, err) + assert.Equal(t, orderType, OrderTypeMarket) + assert.Equal(t, postOnly, false) + assert.Equal(t, IOC, false) +} + +func Test_toLocalOrderTypeWithIOCLimit(t *testing.T) { + + orderType, postOnly, IOC, err := toLocalOrderType(types.OrderTypeIOCLimit) + + assert.NoError(t, err) + assert.Equal(t, orderType, OrderTypeLimit) + assert.Equal(t, postOnly, false) + assert.Equal(t, IOC, true) +} diff --git a/pkg/exchange/ftx/exchange.go b/pkg/exchange/ftx/exchange.go index 522e859d7..c7faec912 100644 --- a/pkg/exchange/ftx/exchange.go +++ b/pkg/exchange/ftx/exchange.go @@ -3,13 +3,14 @@ package ftx import ( "context" "fmt" - "golang.org/x/time/rate" "net/http" "net/url" "sort" "strings" "time" + "golang.org/x/time/rate" + "github.com/google/uuid" "github.com/sirupsen/logrus" @@ -454,15 +455,19 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder if err := requestLimit.Wait(ctx); err != nil { logrus.WithError(err).Error("rate limit error") } + orderType, postOnly, IOC, err := toLocalOrderType(so.Type) + if err != nil { + logrus.WithError(err).Error("type error") + } or, err := e.newRest().PlaceOrder(ctx, PlaceOrderPayload{ Market: toLocalSymbol(TrimUpperString(so.Symbol)), Side: TrimLowerString(string(so.Side)), Price: so.Price, - Type: TrimLowerString(string(so.Type)), + Type: string(orderType), Size: so.Quantity, ReduceOnly: false, - IOC: false, - PostOnly: false, + IOC: IOC, + PostOnly: postOnly, ClientID: newSpotClientOrderID(so.ClientOrderID), }) if err != nil {