simplify profitFixer and apply it to xfunding

This commit is contained in:
c9s 2024-03-06 21:56:32 +08:00
parent 645d1792d2
commit f609b1cdc4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 52 additions and 11 deletions

View File

@ -19,14 +19,11 @@ type ProfitFixerConfig struct {
// 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 {
func NewProfitFixer() *ProfitFixer {
return &ProfitFixer{
market: market,
sessions: make(map[string]types.ExchangeTradeHistoryService),
}
}
@ -48,7 +45,7 @@ func (f *ProfitFixer) batchQueryTrades(
})
}
func (f *ProfitFixer) aggregateAllTrades(ctx context.Context, market types.Market, since, until time.Time) ([]types.Trade, error) {
func (f *ProfitFixer) aggregateAllTrades(ctx context.Context, symbol string, since, until time.Time) ([]types.Trade, error) {
var mu sync.Mutex
var allTrades = make([]types.Trade, 0, 1000)
@ -58,8 +55,8 @@ func (f *ProfitFixer) aggregateAllTrades(ctx context.Context, market types.Marke
sessionName := n
service := s
g.Go(func() error {
log.Infof("batch querying %s trade history from %s since %s until %s", market.Symbol, sessionName, since.String(), until.String())
trades, err := f.batchQueryTrades(subCtx, service, f.market.Symbol, since, until)
log.Infof("batch querying %s trade history from %s since %s until %s", symbol, sessionName, since.String(), until.String())
trades, err := f.batchQueryTrades(subCtx, service, symbol, since, until)
if err != nil {
log.WithError(err).Errorf("unable to batch query trades for fixer")
return err
@ -80,9 +77,11 @@ func (f *ProfitFixer) aggregateAllTrades(ctx context.Context, market types.Marke
return allTrades, nil
}
func (f *ProfitFixer) Fix(ctx context.Context, since, until time.Time, stats *types.ProfitStats, position *types.Position) error {
func (f *ProfitFixer) Fix(
ctx context.Context, symbol string, since, until time.Time, stats *types.ProfitStats, position *types.Position,
) error {
log.Infof("starting profitFixer with time range %s <=> %s", since, until)
allTrades, err := f.aggregateAllTrades(ctx, f.market, since, until)
allTrades, err := f.aggregateAllTrades(ctx, symbol, since, until)
if err != nil {
return err
}

View File

@ -333,7 +333,7 @@ func (s *Strategy) CrossRun(
s.CrossExchangeMarketMakingStrategy.Position = types.NewPositionFromMarket(makerMarket)
s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket)
fixer := common.NewProfitFixer(makerMarket)
fixer := common.NewProfitFixer()
if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok {
log.Infof("adding makerSession %s to profitFixer", makerSession.Name)
fixer.AddExchange(makerSession.Name, ss)
@ -344,7 +344,11 @@ func (s *Strategy) CrossRun(
fixer.AddExchange(hedgeSession.Name, ss)
}
if err2 := fixer.Fix(ctx, s.ProfitFixerConfig.TradesSince.Time(), time.Now(), s.CrossExchangeMarketMakingStrategy.ProfitStats, s.CrossExchangeMarketMakingStrategy.Position); err2 != nil {
if err2 := fixer.Fix(ctx, makerMarket.Symbol,
s.ProfitFixerConfig.TradesSince.Time(),
time.Now(),
s.CrossExchangeMarketMakingStrategy.ProfitStats,
s.CrossExchangeMarketMakingStrategy.Position); err2 != nil {
return err2
}

View File

@ -13,6 +13,7 @@ import (
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/util/backoff"
"github.com/c9s/bbgo/pkg/bbgo"
@ -137,6 +138,8 @@ type Strategy struct {
// Reset your position info
Reset bool `json:"reset"`
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer"`
// CloseFuturesPosition can be enabled to close the futures position and then transfer the collateral asset back to the spot account.
CloseFuturesPosition bool `json:"closeFuturesPosition"`
@ -318,6 +321,41 @@ func (s *Strategy) CrossRun(
s.State = newState()
}
if s.ProfitFixerConfig != nil {
log.Infof("profitFixer is enabled, start fixing with config: %+v", s.ProfitFixerConfig)
s.SpotPosition = types.NewPositionFromMarket(s.spotMarket)
s.FuturesPosition = types.NewPositionFromMarket(s.futuresMarket)
s.ProfitStats.ProfitStats = types.NewProfitStats(s.Market)
since := s.ProfitFixerConfig.TradesSince.Time()
now := time.Now()
spotFixer := common.NewProfitFixer()
if ss, ok := s.spotSession.Exchange.(types.ExchangeTradeHistoryService); ok {
spotFixer.AddExchange(s.spotSession.Name, ss)
}
if err2 := spotFixer.Fix(ctx, s.Symbol,
since, now,
s.ProfitStats.ProfitStats,
s.SpotPosition); err2 != nil {
return err2
}
futuresFixer := common.NewProfitFixer()
if ss, ok := s.futuresSession.Exchange.(types.ExchangeTradeHistoryService); ok {
futuresFixer.AddExchange(s.futuresSession.Name, ss)
}
if err2 := futuresFixer.Fix(ctx, s.Symbol,
since, now,
s.ProfitStats.ProfitStats,
s.FuturesPosition); err2 != nil {
return err2
}
}
// adjust QuoteInvestment according to the available quote balance
if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok {
originalQuoteInvestment := s.QuoteInvestment