always sort orders and trades in the batch query

This commit is contained in:
c9s 2021-12-31 14:12:41 +08:00
parent 2a8caa3780
commit eba33329d1
3 changed files with 14 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package batch
import (
"context"
"sort"
"time"
"github.com/sirupsen/logrus"
@ -69,6 +70,11 @@ func (e ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startTi
}
}
// sort orders by time in ascending order
sort.Slice(orders, func(i, j int) bool {
return orders[i].CreationTime.Before(time.Time(orders[j].CreationTime))
})
for _, o := range orders {
if _, ok := orderIDs[o.OrderID]; ok {
continue

View File

@ -5,6 +5,8 @@ import (
"errors"
"time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types"
)
@ -22,27 +24,32 @@ type SyncService struct {
// SyncSessionSymbols syncs the trades from the given exchange session
func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exchange, startTime time.Time, symbols ...string) error {
for _, symbol := range symbols {
log.Infof("syncing %s %s trades...", exchange.Name(), symbol)
if err := s.TradeService.Sync(ctx, exchange, symbol); err != nil {
return err
}
log.Infof("syncing %s %s orders...", exchange.Name(), symbol)
if err := s.OrderService.Sync(ctx, exchange, symbol, startTime); err != nil {
return err
}
}
log.Infof("syncing %s deposit records...", exchange.Name())
if err := s.DepositService.Sync(ctx, exchange); err != nil {
if err != ErrNotImplemented {
return err
}
}
log.Infof("syncing %s withdraw records...", exchange.Name())
if err := s.WithdrawService.Sync(ctx, exchange); err != nil {
if err != ErrNotImplemented {
return err
}
}
log.Infof("syncing %s reward records...", exchange.Name())
if err := s.RewardService.Sync(ctx, exchange); err != nil {
if err != ErrExchangeRewardServiceNotImplemented {
return err

View File

@ -74,7 +74,7 @@ func (s *TradeService) Sync(ctx context.Context, exchange types.Exchange, symbol
}
// records descending ordered
// records descending ordered, buffer 50 trades and use the trades ID to scan if the new trades are duplicated
records, err := s.QueryLast(exchange.Name(), symbol, isMargin, isFutures, isIsolated, 50)
if err != nil {
return err