From 256e09a86331296e270787fc0d59c4817cf71366 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 6 Mar 2024 22:04:22 +0800 Subject: [PATCH] xfunding: adjust quote investment variable only when position is not opening --- pkg/strategy/xfunding/strategy.go | 41 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/pkg/strategy/xfunding/strategy.go b/pkg/strategy/xfunding/strategy.go index 48c5ff96c..aac2908c9 100644 --- a/pkg/strategy/xfunding/strategy.go +++ b/pkg/strategy/xfunding/strategy.go @@ -360,24 +360,6 @@ func (s *Strategy) CrossRun( bbgo.Notify("Fixed profit stats", s.ProfitStats.ProfitStats) } - // adjust QuoteInvestment according to the available quote balance - if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok { - originalQuoteInvestment := s.QuoteInvestment - - // adjust available quote with the fee rate - spotFeeRate := 0.075 - availableQuoteWithoutFee := b.Available.Mul(fixedpoint.NewFromFloat(1.0 - (spotFeeRate * 0.01))) - - s.QuoteInvestment = fixedpoint.Min(availableQuoteWithoutFee, s.QuoteInvestment) - - if originalQuoteInvestment.Compare(s.QuoteInvestment) != 0 { - log.Infof("adjusted quoteInvestment from %f to %f according to the balance", - originalQuoteInvestment.Float64(), - s.QuoteInvestment.Float64(), - ) - } - } - if err := s.syncPositionRisks(ctx); err != nil { return err } @@ -402,6 +384,29 @@ func (s *Strategy) CrossRun( // TEST CODE: // s.syncFundingFeeRecords(ctx, time.Now().Add(-3*24*time.Hour)) + switch s.State.PositionState { + case PositionClosed: + // adjust QuoteInvestment according to the available quote balance + // ONLY when the position is not opening + if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok { + originalQuoteInvestment := s.QuoteInvestment + + // adjust available quote with the fee rate + spotFeeRate := 0.075 + availableQuoteWithoutFee := b.Available.Mul(fixedpoint.NewFromFloat(1.0 - (spotFeeRate * 0.01))) + + s.QuoteInvestment = fixedpoint.Min(availableQuoteWithoutFee, s.QuoteInvestment) + + if originalQuoteInvestment.Compare(s.QuoteInvestment) != 0 { + log.Infof("adjusted quoteInvestment from %f to %f according to the balance", + originalQuoteInvestment.Float64(), + s.QuoteInvestment.Float64(), + ) + } + } + + } + switch s.State.PositionState { case PositionOpening: // transfer all base assets from the spot account into the spot account