Skip to content

Commit

Permalink
kucoin: fix trades sync
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Apr 12, 2022
1 parent fad9449 commit a34dbf1
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 54 deletions.
10 changes: 4 additions & 6 deletions pkg/exchange/batch/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package batch
import (
"context"
"errors"
"sort"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -41,7 +40,6 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
var startTime = *options.StartTime
var endTime = *options.EndTime


go func() {
limiter := rate.NewLimiter(rate.Every(5*time.Second), 2) // from binance (original 1200, use 1000 for safety)

Expand All @@ -66,9 +64,7 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
})

// sort trades by time in ascending order
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(time.Time(trades[j].Time))
})
types.SortTradesAscending(trades)

if err != nil {
errC <- err
Expand All @@ -78,7 +74,9 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
// if all trades are duplicated or empty, we end the batch query
if len(trades) == 0 {
return
} else if len(trades) > 0 {
}

if len(trades) > 0 {
allExists := true
for _, td := range trades {
k := td.Key()
Expand Down
4 changes: 2 additions & 2 deletions pkg/exchange/kucoin/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,9 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type

if options.StartTime != nil && options.EndTime != nil {
req.StartAt(*options.StartTime)

if options.EndTime.Sub(*options.StartTime) < 7*24*time.Hour {
req.EndAt(*options.EndTime)
} else {
req.StartAt(options.StartTime.Add(7*24*time.Hour - time.Minute))
}
} else if options.StartTime != nil {
req.StartAt(*options.StartTime)
Expand All @@ -383,6 +382,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
if err != nil {
return trades, err
}

for _, fill := range response.Items {
trade := toGlobalTrade(fill)
trades = append(trades, trade)
Expand Down
1 change: 0 additions & 1 deletion pkg/exchange/kucoin/kucoinapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,3 @@ type APIResponse struct {
Message string `json:"msg"`
Data json.RawMessage `json:"data"`
}

31 changes: 17 additions & 14 deletions pkg/exchange/kucoin/kucoinapi/get_fills_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/exchange/kucoin/kucoinapi/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ func (c *TradeService) NewGetFillsRequest() *GetFillsRequest {
type GetFillsRequest struct {
client requestgen.AuthenticatedAPIClient

orderID *string `param:"orderId"`
orderID *string `param:"orderId,query"`

tradeType *string `param:"tradeType" default:"TRADE"`
tradeType *string `param:"tradeType,query" default:"TRADE"`

symbol *string `param:"symbol"`
symbol *string `param:"symbol,query"`

side *string `param:"side" validValues:"buy,sell"`
side *string `param:"side,query" validValues:"buy,sell"`

orderType *string `param:"type" validValues:"limit,market,limit_stop,market_stop"`
orderType *string `param:"type,query" validValues:"limit,market,limit_stop,market_stop"`

startAt *time.Time `param:"startAt,milliseconds"`
startAt *time.Time `param:"startAt,query,milliseconds"`

endAt *time.Time `param:"endAt,milliseconds"`
endAt *time.Time `param:"endAt,query,milliseconds"`
}

type FillListPage struct {
Expand Down
53 changes: 29 additions & 24 deletions pkg/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,37 @@ func (s *Server) Subscribe(request *pb.SubscribeRequest, server pb.MarketDataSer

streamPool := map[string]types.Stream{}
for sessionName, subs := range exchangeSubscriptions {
if session, ok := s.Environ.Session(sessionName); ok {
stream := session.Exchange.NewStream()
stream.SetPublicOnly()
for _, sub := range subs {
stream.Subscribe(sub.Channel, sub.Symbol, sub.Options)
}

stream.OnBookSnapshot(func(book types.SliceOrderBook) {
if err := server.Send(transBook(session, book, pb.Event_SNAPSHOT)); err != nil {
log.WithError(err).Error("grpc stream send error")
}
})
session, ok := s.Environ.Session(sessionName)
if !ok {
log.Errorf("session %s not found", sessionName)
continue
}

stream.OnBookUpdate(func(book types.SliceOrderBook) {
if err := server.Send(transBook(session, book, pb.Event_UPDATE)); err != nil {
log.WithError(err).Error("grpc stream send error")
}
})
stream.OnKLineClosed(func(kline types.KLine) {
err := server.Send(transKLine(session, kline))
if err != nil {
log.WithError(err).Error("grpc stream send error")
}
})
streamPool[sessionName] = stream
stream := session.Exchange.NewStream()
stream.SetPublicOnly()
for _, sub := range subs {
log.Infof("%s subscribe %s %s %+v", sessionName, sub.Channel, sub.Symbol, sub.Options)
stream.Subscribe(sub.Channel, sub.Symbol, sub.Options)
}

stream.OnBookSnapshot(func(book types.SliceOrderBook) {
if err := server.Send(transBook(session, book, pb.Event_SNAPSHOT)); err != nil {
log.WithError(err).Error("grpc stream send error")
}
})

stream.OnBookUpdate(func(book types.SliceOrderBook) {
if err := server.Send(transBook(session, book, pb.Event_UPDATE)); err != nil {
log.WithError(err).Error("grpc stream send error")
}
})
stream.OnKLineClosed(func(kline types.KLine) {
err := server.Send(transKLine(session, kline))
if err != nil {
log.WithError(err).Error("grpc stream send error")
}
})
streamPool[sessionName] = stream
}

for sessionName, stream := range streamPool {
Expand Down
13 changes: 13 additions & 0 deletions pkg/types/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

import (
"sort"
"time"
)

func SortTradesAscending(trades []Trade) []Trade {
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(time.Time(trades[j].Time))
})
return trades
}

0 comments on commit a34dbf1

Please sign in to comment.