mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
grid2: improve the if err syntax
This commit is contained in:
parent
0c72ac2386
commit
46a6d896a2
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user