Skip to content
forked from c9s/bbgo

Commit

Permalink
common: fix profit fixer batch query
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 8, 2024
1 parent d41de32 commit 22c154f
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions pkg/strategy/common/profit_fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (f *ProfitFixer) batchQueryTrades(
service types.ExchangeTradeHistoryService,
symbol string,
since, until time.Time,
) ([]types.Trade, error) {
) (chan types.Trade, chan error) {
q := &batch.TradeBatchQuery{ExchangeTradeHistoryService: service}
return q.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
return q.Query(ctx, symbol, &types.TradeQueryOptions{
StartTime: &since,
EndTime: &until,
})
Expand All @@ -58,24 +58,37 @@ func (f *ProfitFixer) aggregateAllTrades(ctx context.Context, symbol string, sin
service := s
g.Go(func() error {
log.Infof("batch querying %s trade history from %s since %s until %s", symbol, sessionName, since.String(), until.String())
trades, err := f.batchQueryTrades(subCtx, service, symbol, since, until)
if err != nil {
log.WithError(err).Errorf("unable to batch query trades for fixer")
return err
}
tradeC, errC := f.batchQueryTrades(subCtx, service, symbol, since, until)

for {
select {
case <-ctx.Done():
return ctx.Err()

case err := <-errC:
return err

mu.Lock()
allTrades = append(allTrades, trades...)
mu.Unlock()
return nil
case trade, ok := <-tradeC:
if !ok {
return nil
}

mu.Lock()
allTrades = append(allTrades, trade)
mu.Unlock()
}
}
})
}

if err := g.Wait(); err != nil {
return nil, err
}

mu.Lock()
allTrades = types.SortTradesAscending(allTrades)
mu.Unlock()

return allTrades, nil
}

Expand Down

0 comments on commit 22c154f

Please sign in to comment.