grid2: improve the if err syntax

This commit is contained in:
c9s 2023-04-26 23:48:02 +08:00
parent 0c72ac2386
commit 46a6d896a2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 15 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -15,6 +16,8 @@ type ProfitFixer struct {
symbol string
grid *Grid
historyService types.ExchangeTradeHistoryService
logger logrus.FieldLogger
}
func newProfitFixer(grid *Grid, symbol string, historyService types.ExchangeTradeHistoryService) *ProfitFixer {
@ -22,19 +25,24 @@ func newProfitFixer(grid *Grid, symbol string, historyService types.ExchangeTrad
symbol: symbol,
grid: grid,
historyService: historyService,
logger: logrus.StandardLogger(),
}
}
func (f *ProfitFixer) SetLogger(logger logrus.FieldLogger) {
f.logger = logger
}
// Fix fixes the total quote profit of the given grid
func (f *ProfitFixer) Fix(parent context.Context, since, until time.Time, initialOrderID uint64, profitStats *GridProfitStats) error {
// reset profit
profitStats.TotalQuoteProfit = fixedpoint.Zero
profitStats.ArbitrageCount = 0
defer log.Infof("profitFixer: done")
defer f.logger.Infof("profitFixer: done")
if profitStats.Since != nil && !profitStats.Since.IsZero() && profitStats.Since.Before(since) {
log.Infof("profitFixer: profitStats.since %s is earlier than the given since %s, setting since to %s", profitStats.Since, since, profitStats.Since)
f.logger.Infof("profitFixer: profitStats.since %s is earlier than the given since %s, setting since to %s", profitStats.Since, since, profitStats.Since)
since = *profitStats.Since
}
@ -45,7 +53,7 @@ func (f *ProfitFixer) Fix(parent context.Context, since, until time.Time, initia
orderC, errC := q.Query(ctx, f.symbol, since, until, initialOrderID)
defer func() {
log.Infof("profitFixer: fixed profitStats=%#v", profitStats)
f.logger.Infof("profitFixer: fixed profitStats=%#v", profitStats)
}()
for {
@ -91,7 +99,7 @@ func (f *ProfitFixer) Fix(parent context.Context, since, until time.Time, initia
profitStats.TotalQuoteProfit = profitStats.TotalQuoteProfit.Add(quoteProfit)
profitStats.ArbitrageCount++
log.Debugf("profitFixer: filledSellOrder=%#v", order)
f.logger.Debugf("profitFixer: filledSellOrder=%#v", order)
}
}
}

View File

@ -81,9 +81,10 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex
}
fixer := newProfitFixer(s.grid, s.Symbol, historyService)
fixer.SetLogger(s.logger)
// set initial order ID = 0 instead of s.GridProfitStats.InitialOrderID because the order ID could be incorrect
err := fixer.Fix(ctx, since, until, 0, s.GridProfitStats)
if err != nil {
if err := fixer.Fix(ctx, since, until, 0, s.GridProfitStats); err != nil {
return err
}