From e3181202dbaf02960db9165859080314587c2513 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 26 Dec 2021 01:44:05 +0800 Subject: [PATCH] kucoin: implement QueryTrades --- pkg/exchange/kucoin/convert.go | 20 ++++++++++++++++++++ pkg/exchange/kucoin/exchange.go | 23 +++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pkg/exchange/kucoin/convert.go b/pkg/exchange/kucoin/convert.go index 70600e70d..59690ef10 100644 --- a/pkg/exchange/kucoin/convert.go +++ b/pkg/exchange/kucoin/convert.go @@ -213,3 +213,23 @@ func toGlobalOrder(o kucoinapi.Order) types.Order { } return order } + +func toGlobalTrade(fill kucoinapi.Fill) types.Trade { + var trade = types.Trade{ + ID: hashStringID(fill.TradeId), + OrderID: hashStringID(fill.OrderId), + Exchange: types.ExchangeKucoin, + Price: fill.Price.Float64(), + Quantity: fill.Size.Float64(), + QuoteQuantity: fill.Funds.Float64(), + Symbol: toGlobalSymbol(fill.Symbol), + Side: toGlobalSide(string(fill.Side)), + IsBuyer: fill.Side == kucoinapi.SideTypeBuy, + IsMaker: fill.Liquidity == kucoinapi.LiquidityTypeMaker, + Time: types.Time{}, + Fee: fill.Fee.Float64(), + FeeCurrency: toGlobalSymbol(fill.FeeCurrency), + } + return trade +} + diff --git a/pkg/exchange/kucoin/exchange.go b/pkg/exchange/kucoin/exchange.go index 66418a52b..67be13b7e 100644 --- a/pkg/exchange/kucoin/exchange.go +++ b/pkg/exchange/kucoin/exchange.go @@ -27,7 +27,6 @@ type Exchange struct { client *kucoinapi.RestClient } - func New(key, secret, passphrase string) *Exchange { client := kucoinapi.NewClient() @@ -247,10 +246,26 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, return orders, err } -func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) { - panic("implement me") -} +func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) { + req := e.client.TradeService.NewGetFillsRequest() + req.Symbol(toLocalSymbol(symbol)) + if options.StartTime != nil { + req.StartAt(*options.StartTime) + } else if options.EndTime != nil { + req.EndAt(*options.EndTime) + } + response, err := req.Do(ctx) + if err != nil { + return trades, err + } + for _, fill := range response.Items { + trade := toGlobalTrade(fill) + trades = append(trades, trade) + } + + return trades, nil +} func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (errs error) { for _, o := range orders {