Skip to content

Commit

Permalink
autoborrow: fix shouldAlert condition
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 27, 2024
1 parent 0055712 commit 345dca5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/strategy/autoborrow/alert_interest_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ func newMarginHighInterestRateWorker(strategy *Strategy, config *MarginHighInter
}

func (w *MarginHighInterestRateWorker) findMarginHighInterestRateAssets(
debts types.BalanceMap,
rateMap types.MarginNextHourlyInterestRateMap,
minAnnualRate float64,
) (highRates types.MarginNextHourlyInterestRateMap, err error) {
highRates = make(types.MarginNextHourlyInterestRateMap)
for asset, rate := range rateMap {
_, ok := debts[asset]
if !ok {
continue
}

if rate.AnnualizedRate.IsZero() {
log.Warnf("annualized rate is zero for %s", asset)
}
Expand Down Expand Up @@ -176,17 +182,18 @@ func (w *MarginHighInterestRateWorker) Run(ctx context.Context) {

log.Infof("rates: %+v", rateMap)

highRateAssets, err := w.findMarginHighInterestRateAssets(rateMap, w.config.MinAnnualInterestRate.Float64())
debts := w.session.Account.Balances().Debts()

highRateAssets, err := w.findMarginHighInterestRateAssets(debts, rateMap, w.config.MinAnnualInterestRate.Float64())
if err != nil {
log.WithError(err).Errorf("unable to query the next future hourly interest rate")
continue
}

log.Infof("found high interest rate assets: %+v", highRateAssets)

totalDebtValue := fixedpoint.Zero
nextTotalInterestValue := fixedpoint.Zero
debts := w.session.Account.Balances().Debts()
exceededDebtAmount := false
for cur, bal := range debts {
price, ok := w.session.GetPriceSolver().ResolvePrice(cur, currency.USDT)
if !ok {
Expand All @@ -198,13 +205,15 @@ func (w *MarginHighInterestRateWorker) Run(ctx context.Context) {
nextTotalInterestValue = nextTotalInterestValue.Add(
bal.Debt().Mul(rate.HourlyRate).Mul(price))

totalDebtValue = totalDebtValue.Add(bal.Debt().Mul(price))
debtValue := bal.Debt().Mul(price)
if w.config.MinDebtAmount.Sign() > 0 &&
debtValue.Compare(w.config.MinDebtAmount) > 0 {
exceededDebtAmount = true
}
}

shouldAlert := func() bool {
return len(highRateAssets) > 0 &&
w.config.MinDebtAmount.Sign() > 0 &&
totalDebtValue.Compare(w.config.MinDebtAmount) > 0
return len(highRateAssets) > 0 && exceededDebtAmount
}

// either danger or margin level is less than the minimal margin level
Expand Down

0 comments on commit 345dca5

Please sign in to comment.