bbgo_origin/pkg/strategy/xdepthmaker/profitfixer.go

87 lines
2.2 KiB
Go
Raw Normal View History

2024-03-05 10:12:30 +00:00
package xdepthmaker
import (
"context"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/types"
)
type ProfitFixerConfig struct {
TradesSince types.Time `json:"tradesSince,omitempty"`
}
// ProfitFixer implements a trade history based profit fixer
type ProfitFixer struct {
market types.Market
sessions map[string]types.ExchangeTradeHistoryService
}
func NewProfitFixer(market types.Market) *ProfitFixer {
return &ProfitFixer{
market: market,
sessions: make(map[string]types.ExchangeTradeHistoryService),
}
}
func (f *ProfitFixer) AddExchange(sessionName string, service types.ExchangeTradeHistoryService) {
f.sessions[sessionName] = service
}
func (f *ProfitFixer) batchQueryTrades(
ctx context.Context,
service types.ExchangeTradeHistoryService,
symbol string,
2024-03-05 13:11:51 +00:00
since, until time.Time,
2024-03-05 10:12:30 +00:00
) ([]types.Trade, error) {
q := &batch.TradeBatchQuery{ExchangeTradeHistoryService: service}
2024-03-05 13:11:51 +00:00
return q.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
2024-03-05 10:12:30 +00:00
StartTime: &since,
2024-03-05 13:11:51 +00:00
EndTime: &until,
2024-03-05 10:12:30 +00:00
})
}
2024-03-05 13:11:51 +00:00
func (f *ProfitFixer) Fix(ctx context.Context, since, until time.Time, stats *types.ProfitStats, position *types.Position) error {
2024-03-06 09:47:18 +00:00
log.Infof("starting profitFixer with time range %s <=> %s", since, until)
2024-03-05 10:12:30 +00:00
var mu sync.Mutex
var allTrades = make([]types.Trade, 0, 1000)
g, subCtx := errgroup.WithContext(ctx)
2024-03-05 13:16:35 +00:00
for n, s := range f.sessions {
// allocate a copy of the iteration variables
2024-03-05 13:13:19 +00:00
sessionName := n
2024-03-05 13:16:35 +00:00
service := s
2024-03-05 10:12:30 +00:00
g.Go(func() error {
2024-03-05 13:13:19 +00:00
log.Infof("batch querying %s trade history from %s since %s until %s", f.market.Symbol, sessionName, since.String(), until.String())
2024-03-05 13:11:51 +00:00
trades, err := f.batchQueryTrades(subCtx, service, f.market.Symbol, since, until)
2024-03-05 10:12:30 +00:00
if err != nil {
log.WithError(err).Errorf("unable to batch query trades for fixer")
return err
}
mu.Lock()
allTrades = append(allTrades, trades...)
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return err
2024-03-05 10:12:30 +00:00
}
allTrades = types.SortTradesAscending(allTrades)
for _, trade := range allTrades {
stats.AddTrade(trade)
position.AddTrade(trade)
2024-03-05 10:12:30 +00:00
}
2024-03-06 09:47:18 +00:00
log.Infof("profitFixer done: profitStats and position are updated from %d trades", len(allTrades))
return nil
2024-03-05 10:12:30 +00:00
}