mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
Merge pull request #882 from c9s/improve/autoborrow
strategy/autoborrow: add debt re-balancing
This commit is contained in:
commit
c786d7d395
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user