Skip to content

Commit

Permalink
xfunding: pull out startClosingPosition, startOpeningPosition method
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 23, 2023
1 parent 3624dd0 commit e016892
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions pkg/strategy/xfunding/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,7 @@ func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed
log.Infof("funding rate %s is higher than the High threshold %s, start opening position...",
fundingRate.Percentage(), s.ShortFundingRate.High.Percentage())

s.positionAction = PositionOpening
s.positionType = types.PositionShort

// reset the transfer stats
s.State.PositionStartTime = premiumIndex.Time
s.State.PendingBaseTransfer = fixedpoint.Zero
s.State.TotalBaseTransfer = fixedpoint.Zero
s.startOpeningPosition(types.PositionShort, premiumIndex.Time)
changed = true
} else if fundingRate.Compare(s.ShortFundingRate.Low) <= 0 {

Expand All @@ -618,17 +612,31 @@ func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed
return
}

s.positionAction = PositionClosing

// reset the transfer stats
s.State.PendingBaseTransfer = fixedpoint.Zero
s.State.TotalBaseTransfer = fixedpoint.Zero
s.startClosingPosition()
changed = true
}

return changed
}

func (s *Strategy) startOpeningPosition(pt types.PositionType, t time.Time) {
s.positionAction = PositionOpening
s.positionType = pt

// reset the transfer stats
s.State.PositionStartTime = t
s.State.PendingBaseTransfer = fixedpoint.Zero
s.State.TotalBaseTransfer = fixedpoint.Zero
}

func (s *Strategy) startClosingPosition() {
s.positionAction = PositionClosing

// reset the transfer stats
s.State.PendingBaseTransfer = fixedpoint.Zero
s.State.TotalBaseTransfer = fixedpoint.Zero
}

func (s *Strategy) allocateOrderExecutor(ctx context.Context, session *bbgo.ExchangeSession, instanceID string, position *types.Position) *bbgo.GeneralOrderExecutor {
orderExecutor := bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, position)
orderExecutor.SetMaxRetries(0)
Expand Down

0 comments on commit e016892

Please sign in to comment.