From 0c6e9496b31e9a6c5b6cfd74581b7a431ddef9b7 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 25 Mar 2023 02:56:45 +0800 Subject: [PATCH] xfunding: use early return --- pkg/strategy/xfunding/strategy.go | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/strategy/xfunding/strategy.go b/pkg/strategy/xfunding/strategy.go index 01f6f4b99..df8edfb01 100644 --- a/pkg/strategy/xfunding/strategy.go +++ b/pkg/strategy/xfunding/strategy.go @@ -718,28 +718,32 @@ func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) bool { switch s.getPositionState() { case PositionClosed: - if fundingRate.Compare(s.ShortFundingRate.High) >= 0 { - log.Infof("funding rate %s is higher than the High threshold %s, start opening position...", - fundingRate.Percentage(), s.ShortFundingRate.High.Percentage()) - - s.startOpeningPosition(types.PositionShort, premiumIndex.Time) - return true + if fundingRate.Compare(s.ShortFundingRate.High) < 0 { + return false } + log.Infof("funding rate %s is higher than the High threshold %s, start opening position...", + fundingRate.Percentage(), s.ShortFundingRate.High.Percentage()) + + s.startOpeningPosition(types.PositionShort, premiumIndex.Time) + return true + case PositionReady: - if fundingRate.Compare(s.ShortFundingRate.Low) <= 0 { - log.Infof("funding rate %s is lower than the Low threshold %s, start closing position...", - fundingRate.Percentage(), s.ShortFundingRate.Low.Percentage()) - - holdingPeriod := premiumIndex.Time.Sub(s.State.PositionStartTime) - if holdingPeriod < time.Duration(s.MinHoldingPeriod) { - log.Warnf("position holding period %s is less than %s, skip closing", holdingPeriod, s.MinHoldingPeriod.Duration()) - return false - } - - s.startClosingPosition() - return true + if fundingRate.Compare(s.ShortFundingRate.Low) > 0 { + return false } + + log.Infof("funding rate %s is lower than the Low threshold %s, start closing position...", + fundingRate.Percentage(), s.ShortFundingRate.Low.Percentage()) + + holdingPeriod := premiumIndex.Time.Sub(s.State.PositionStartTime) + if holdingPeriod < time.Duration(s.MinHoldingPeriod) { + log.Warnf("position holding period %s is less than %s, skip closing", holdingPeriod, s.MinHoldingPeriod.Duration()) + return false + } + + s.startClosingPosition() + return true } return false