bbgo_origin/pkg/strategy/grid2/profit_fixer.go

106 lines
2.6 KiB
Go
Raw Normal View History

2023-04-26 08:14:53 +00:00
package grid2
import (
"context"
"time"
"github.com/pkg/errors"
2023-04-26 15:48:02 +00:00
"github.com/sirupsen/logrus"
2023-04-26 08:14:53 +00:00
"github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type ProfitFixer struct {
symbol string
grid *Grid
historyService types.ExchangeTradeHistoryService
2023-04-26 15:48:02 +00:00
logger logrus.FieldLogger
2023-04-26 08:14:53 +00:00
}
func newProfitFixer(grid *Grid, symbol string, historyService types.ExchangeTradeHistoryService) *ProfitFixer {
return &ProfitFixer{
symbol: symbol,
grid: grid,
historyService: historyService,
2023-04-26 15:48:02 +00:00
logger: logrus.StandardLogger(),
2023-04-26 08:14:53 +00:00
}
}
2023-04-26 15:48:02 +00:00
func (f *ProfitFixer) SetLogger(logger logrus.FieldLogger) {
f.logger = logger
}
2023-04-26 08:14:53 +00:00
// 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 {
2023-04-26 15:07:01 +00:00
// reset profit
2023-04-26 08:14:53 +00:00
profitStats.TotalQuoteProfit = fixedpoint.Zero
profitStats.ArbitrageCount = 0
2023-04-26 15:48:02 +00:00
defer f.logger.Infof("profitFixer: done")
2023-04-26 15:07:01 +00:00
if profitStats.Since != nil && !profitStats.Since.IsZero() && profitStats.Since.Before(since) {
2023-04-26 15:48:02 +00:00
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
}
2023-04-26 15:07:01 +00:00
ctx, cancel := context.WithTimeout(parent, 15*time.Minute)
defer cancel()
2023-04-26 08:14:53 +00:00
q := &batch.ClosedOrderBatchQuery{ExchangeTradeHistoryService: f.historyService}
orderC, errC := q.Query(ctx, f.symbol, since, until, initialOrderID)
defer func() {
2023-04-26 15:48:02 +00:00
f.logger.Infof("profitFixer: fixed profitStats=%#v", profitStats)
}()
2023-04-26 08:14:53 +00:00
for {
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.Canceled) {
return nil
}
return ctx.Err()
case order, ok := <-orderC:
if !ok {
return <-errC
}
if !f.grid.HasPrice(order.Price) {
continue
}
if profitStats.InitialOrderID == 0 || order.OrderID < profitStats.InitialOrderID {
2023-04-26 08:14:53 +00:00
profitStats.InitialOrderID = order.OrderID
}
if profitStats.Since == nil || profitStats.Since.IsZero() || order.CreationTime.Time().Before(*profitStats.Since) {
ct := order.CreationTime.Time()
profitStats.Since = &ct
}
2023-04-26 08:14:53 +00:00
if order.Status != types.OrderStatusFilled {
continue
}
if order.Type != types.OrderTypeLimit {
continue
}
if order.Side != types.SideTypeSell {
continue
}
quoteProfit := order.Quantity.Mul(f.grid.Spread)
profitStats.TotalQuoteProfit = profitStats.TotalQuoteProfit.Add(quoteProfit)
profitStats.ArbitrageCount++
2023-04-26 15:48:02 +00:00
f.logger.Debugf("profitFixer: filledSellOrder=%#v", order)
2023-04-26 08:14:53 +00:00
}
}
}