Merge pull request #1666 from c9s/c9s/liqmaker-profit-fixer

FEATURE: [liqmaker] add profit fixer support
This commit is contained in:
c9s 2024-07-08 16:41:17 +08:00 committed by GitHub
commit d41de325f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 1 deletions

View File

@ -2,12 +2,14 @@ package common
import (
"context"
"fmt"
"sync"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/types"
)
@ -101,3 +103,38 @@ func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.Profit
log.Infof("profitFixer fix finished: profitStats and position are updated from %d trades", len(allTrades))
return nil
}
type ProfitFixerBundle struct {
ProfitFixerConfig *ProfitFixerConfig `json:"profitFixer,omitempty"`
}
func (f *ProfitFixerBundle) Fix(
ctx context.Context,
symbol string,
position *types.Position,
profitStats *types.ProfitStats,
sessions ...*bbgo.ExchangeSession,
) error {
bbgo.Notify("Fixing %s profitStats and position...", symbol)
log.Infof("profitFixer is enabled, checking checkpoint: %+v", f.ProfitFixerConfig.TradesSince)
if f.ProfitFixerConfig.TradesSince.Time().IsZero() {
return fmt.Errorf("tradesSince time can not be zero")
}
fixer := NewProfitFixer()
for _, session := range sessions {
if ss, ok := session.Exchange.(types.ExchangeTradeHistoryService); ok {
log.Infof("adding makerSession %s to profitFixer", session.Name)
fixer.AddExchange(session.Name, ss)
}
}
return fixer.Fix(ctx,
symbol,
f.ProfitFixerConfig.TradesSince.Time(),
time.Now(),
profitStats,
position)
}

View File

@ -63,6 +63,8 @@ type Strategy struct {
MinProfit fixedpoint.Value `json:"minProfit"`
common.ProfitFixerBundle
liquidityOrderBook, adjustmentOrderBook *bbgo.ActiveOrderBook
liquidityScale bbgo.Scale
@ -91,6 +93,19 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
}
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
if s.ProfitFixerBundle.ProfitFixerConfig != nil {
market, _ := session.Market(s.Symbol)
s.Position = types.NewPositionFromMarket(market)
s.ProfitStats = types.NewProfitStats(market)
if err := s.ProfitFixerBundle.Fix(ctx, s.Symbol, s.Position, s.ProfitStats, session); err != nil {
return err
}
bbgo.Notify("Fixed %s position", s.Symbol, s.Position)
bbgo.Notify("Fixed %s profitStats", s.Symbol, s.ProfitStats)
}
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
s.orderGenerator = &LiquidityOrderGenerator{

View File

@ -196,7 +196,7 @@ type Strategy struct {
// Pips is the pips of the layer prices
Pips fixedpoint.Value `json:"pips"`
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer"`
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer,omitempty"`
// --------------------------------
// private fields