From 94e2e28eddcb667c939d5e7e54a08b64bca0237b Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 17 Aug 2022 16:45:10 +0800 Subject: [PATCH 1/2] strategy/autoborrow: add debt re-balancing --- pkg/strategy/autoborrow/strategy.go | 64 ++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/pkg/strategy/autoborrow/strategy.go b/pkg/strategy/autoborrow/strategy.go index 34dfb209c..1ad7b4d59 100644 --- a/pkg/strategy/autoborrow/strategy.go +++ b/pkg/strategy/autoborrow/strategy.go @@ -47,6 +47,7 @@ type MarginAsset struct { MaxTotalBorrow fixedpoint.Value `json:"maxTotalBorrow"` MaxQuantityPerBorrow fixedpoint.Value `json:"maxQuantityPerBorrow"` MinQuantityPerBorrow fixedpoint.Value `json:"minQuantityPerBorrow"` + MinDebtRatio fixedpoint.Value `json:"debtRatio"` } type Strategy struct { @@ -109,7 +110,68 @@ func (s *Strategy) tryToRepayAnyDebt(ctx context.Context) { } } +func (s *Strategy) reBalanceDebt(ctx context.Context) { + account, err := s.ExchangeSession.UpdateAccount(ctx) + if err != nil { + log.WithError(err).Errorf("can not update account") + return + } + + minMarginLevel := s.MinMarginLevel + curMarginLevel := account.MarginLevel + + balances := account.Balances() + if len(balances) == 0 { + log.Warn("balance is empty, skip autoborrow") + return + } + + for _, marginAsset := range s.Assets { + b, ok := balances[marginAsset.Asset] + if !ok { + continue + } + + // debt / total + debtRatio := b.Debt().Div(b.Total()) + if marginAsset.MinDebtRatio.IsZero() { + marginAsset.MinDebtRatio = fixedpoint.One + } + + // if debt is greater than total, skip repay + if b.Debt().Compare(b.Total()) > 0 { + continue + } + + // the current debt ratio is less than the minimal ratio, + // we need to repay and reduce the debt + if marginAsset.MinDebtRatio.Compare(debtRatio) < 0 { + continue + } + + toRepay := fixedpoint.Min(b.Borrowed, b.Available) + if toRepay.IsZero() { + return + } + + bbgo.Notify(&MarginAction{ + Exchange: s.ExchangeSession.ExchangeName, + Action: "Repay for Debt Ratio", + Asset: b.Currency, + Amount: toRepay, + MarginLevel: curMarginLevel, + MinMarginLevel: minMarginLevel, + }) + + if err := s.marginBorrowRepay.RepayMarginAsset(context.Background(), b.Currency, toRepay); err != nil { + log.WithError(err).Errorf("margin repay error") + } + } +} + func (s *Strategy) checkAndBorrow(ctx context.Context) { + s.reBalanceDebt(ctx) + if s.MinMarginLevel.IsZero() { return } @@ -148,7 +210,7 @@ func (s *Strategy) checkAndBorrow(ctx context.Context) { log.Warn("balance is empty, skip autoborrow") return } - + for _, marginAsset := range s.Assets { changed := false From 93ab26fbf7d2ea818558fb473191ec494ec303b5 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 17 Aug 2022 17:00:00 +0800 Subject: [PATCH 2/2] doc: add instruction for creating branch --- doc/development/release-process.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/development/release-process.md b/doc/development/release-process.md index f92152dd1..89efb1b35 100644 --- a/doc/development/release-process.md +++ b/doc/development/release-process.md @@ -1,5 +1,11 @@ # Release Process +Create a new branch for the new release: + +```shell +git checkout -b release/v1.39.0 origin/main +``` + ## 1. Run the release test script ```shell