Skip to content

Commit

Permalink
autoborrow: calculate debt value in usdt
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 4, 2024
1 parent 817d356 commit 36dc7c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
20 changes: 14 additions & 6 deletions pkg/strategy/autoborrow/alert_margin_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
27 changes: 21 additions & 6 deletions pkg/strategy/autoborrow/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 36dc7c0

Please sign in to comment.