Skip to content

Commit

Permalink
xfunding: add MinHoldingPeriod support
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 23, 2023
1 parent a933f90 commit aba8039
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/strategy/xfunding/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Strategy struct {

QuoteInvestment fixedpoint.Value `json:"quoteInvestment"`

MinHoldingPeriod types.Duration `json:"minHoldingPeriod"`

// ShortFundingRate is the funding rate range for short positions
// TODO: right now we don't support negative funding rate (long position) since it's rarer
ShortFundingRate *struct {
Expand Down Expand Up @@ -111,6 +113,7 @@ type Strategy struct {
}

type State struct {
PositionStartTime time.Time `json:"positionStartTime"`
PendingBaseTransfer fixedpoint.Value `json:"pendingBaseTransfer"`
TotalBaseTransfer fixedpoint.Value `json:"totalBaseTransfer"`
UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"`
Expand Down Expand Up @@ -146,7 +149,14 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
}

func (s *Strategy) Defaults() error {
s.Leverage = fixedpoint.One
if s.Leverage.IsZero() {
s.Leverage = fixedpoint.One
}

if s.MinHoldingPeriod == 0 {
s.MinHoldingPeriod = types.Duration(3 * 24 * time.Hour)
}

return nil
}

Expand Down Expand Up @@ -574,6 +584,7 @@ func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed
s.positionType = types.PositionShort

// reset the transfer stats
s.State.PositionStartTime = premiumIndex.Time
s.State.PendingBaseTransfer = fixedpoint.Zero
s.State.TotalBaseTransfer = fixedpoint.Zero
changed = true
Expand All @@ -582,6 +593,12 @@ func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed
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)
return
}

s.positionAction = PositionClosing

// reset the transfer stats
Expand Down

0 comments on commit aba8039

Please sign in to comment.