From 36dc7c0c0b45bffda3c8922cde96ee65d03af2f1 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 Dec 2024 15:00:14 +0800 Subject: [PATCH] autoborrow: calculate debt value in usdt --- pkg/strategy/autoborrow/alert_margin_level.go | 20 +++++++++----- pkg/strategy/autoborrow/strategy.go | 27 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pkg/strategy/autoborrow/alert_margin_level.go b/pkg/strategy/autoborrow/alert_margin_level.go index 44256c1ceb..380463ec07 100644 --- a/pkg/strategy/autoborrow/alert_margin_level.go +++ b/pkg/strategy/autoborrow/alert_margin_level.go @@ -19,12 +19,13 @@ type MarginLevelAlertConfig struct { // MarginLevelAlert is used to send the slack mention alerts when the current margin is less than the required margin level type MarginLevelAlert struct { - AccountLabel string - CurrentMarginLevel fixedpoint.Value - MinimalMarginLevel fixedpoint.Value - SessionName string - Exchange types.ExchangeName - Debts types.BalanceMap + AccountLabel string + CurrentMarginLevel fixedpoint.Value + MinimalMarginLevel fixedpoint.Value + SessionName string + Exchange types.ExchangeName + TotalDebtValueInUSD fixedpoint.Value + Debts types.BalanceMap } func (m *MarginLevelAlert) ObjectID() string { @@ -66,6 +67,13 @@ func (m *MarginLevelAlert) SlackAttachment() slack.Attachment { }, }...) + if !m.TotalDebtValueInUSD.IsZero() { + fields = append(fields, slack.AttachmentField{ + Title: "Total Debt Value In USD", + Value: m.TotalDebtValueInUSD.String(), + }) + } + // collect the current debts into the alert fields if m.Debts != nil && len(m.Debts) > 0 { fields = append(fields, m.Debts.SlackAttachment().Fields...) diff --git a/pkg/strategy/autoborrow/strategy.go b/pkg/strategy/autoborrow/strategy.go index 2eb4490a16..c890907aa8 100644 --- a/pkg/strategy/autoborrow/strategy.go +++ b/pkg/strategy/autoborrow/strategy.go @@ -618,14 +618,28 @@ func (s *Strategy) marginAlertWorker(ctx context.Context, alertInterval time.Dur // if the previous danger is set to true, we should send the alert again to // update the previous danger margin alert message if danger || account.MarginLevel.Compare(s.MarginLevelAlert.MinMargin) <= 0 { + // calculate the debt value by the price solver + totalDebtValueInUSDT := fixedpoint.Zero + debts := account.Balances().Debts() + for currency, bal := range debts { + price, ok := s.priceSolver.ResolvePrice(currency, types.USDT) + if !ok { + log.Warnf("unable to resolve price for %s", currency) + continue + } + + debtValue := bal.Debt().Mul(price) + totalDebtValueInUSDT = totalDebtValueInUSDT.Add(debtValue) + } alert := &MarginLevelAlert{ - AccountLabel: s.ExchangeSession.GetAccountLabel(), - Exchange: s.ExchangeSession.ExchangeName, - CurrentMarginLevel: account.MarginLevel, - MinimalMarginLevel: s.MarginLevelAlert.MinMargin, - SessionName: s.ExchangeSession.Name, - Debts: account.Balances().Debts(), + AccountLabel: s.ExchangeSession.GetAccountLabel(), + Exchange: s.ExchangeSession.ExchangeName, + CurrentMarginLevel: account.MarginLevel, + MinimalMarginLevel: s.MarginLevelAlert.MinMargin, + SessionName: s.ExchangeSession.Name, + TotalDebtValueInUSD: totalDebtValueInUSDT, + Debts: account.Balances().Debts(), } bbgo.PostLiveNote(alert, @@ -638,6 +652,7 @@ func (s *Strategy) marginAlertWorker(ctx context.Context, alertInterval time.Dur s.postLiveNoteMessage(alert, s.MarginLevelAlert.Slack, "the current margin level %f is less than the minimal margin level %f, please repay the debt", account.MarginLevel.Float64(), s.MarginLevelAlert.MinMargin.Float64()) + } // update danger flag