Skip to content

Commit

Permalink
xfunding: adjust quote investment variable only when position is not …
Browse files Browse the repository at this point in the history
…opening
  • Loading branch information
c9s committed Mar 6, 2024
1 parent dc0f07d commit 256e09a
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions pkg/strategy/xfunding/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 256e09a

Please sign in to comment.