bbgo_origin/pkg/strategy/autoborrow/strategy.go

351 lines
8.7 KiB
Go
Raw Normal View History

2022-04-23 07:00:04 +00:00
package autoborrow
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
2022-04-25 10:12:08 +00:00
"github.com/slack-go/slack"
2022-04-23 07:00:04 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "autoborrow"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
/**
- on: binance
autoborrow:
interval: 30m
repayWhenDeposit: true
# minMarginLevel for triggering auto borrow
minMarginLevel: 1.5
2022-04-23 07:00:04 +00:00
assets:
- asset: ETH
low: 3.0
maxQuantityPerBorrow: 1.0
maxTotalBorrow: 10.0
- asset: USDT
low: 1000.0
maxQuantityPerBorrow: 100.0
maxTotalBorrow: 10.0
*/
type MarginAsset struct {
Asset string `json:"asset"`
Low fixedpoint.Value `json:"low"`
MaxTotalBorrow fixedpoint.Value `json:"maxTotalBorrow"`
MaxQuantityPerBorrow fixedpoint.Value `json:"maxQuantityPerBorrow"`
MinQuantityPerBorrow fixedpoint.Value `json:"minQuantityPerBorrow"`
}
type Strategy struct {
2022-04-25 10:12:08 +00:00
*bbgo.Notifiability
2022-04-23 07:00:04 +00:00
Interval types.Interval `json:"interval"`
MinMarginLevel fixedpoint.Value `json:"minMarginLevel"`
MaxMarginLevel fixedpoint.Value `json:"maxMarginLevel"`
2022-04-23 07:00:04 +00:00
AutoRepayWhenDeposit bool `json:"autoRepayWhenDeposit"`
Assets []MarginAsset `json:"assets"`
ExchangeSession *bbgo.ExchangeSession
marginBorrowRepay types.MarginBorrowRepay
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}
func (s *Strategy) tryToRepayAnyDebt(ctx context.Context) {
log.Infof("trying to repay any debt...")
if err := s.ExchangeSession.UpdateAccount(ctx); err != nil {
log.WithError(err).Errorf("can not update account")
return
}
account := s.ExchangeSession.GetAccount()
minMarginLevel := s.MinMarginLevel
curMarginLevel := account.MarginLevel
balances := account.Balances()
for _, b := range balances {
if b.Borrowed.Sign() <= 0 {
continue
}
if b.Available.IsZero() {
continue
}
toRepay := b.Available
s.Notifiability.Notify(&MarginAction{
Action: "Repay",
Asset: b.Currency,
Amount: toRepay,
MarginLevel: curMarginLevel,
MinMarginLevel: minMarginLevel,
})
log.Infof("repaying %f %s", toRepay.Float64(), b.Currency)
if err := s.marginBorrowRepay.RepayMarginAsset(context.Background(), b.Currency, toRepay); err != nil {
log.WithError(err).Errorf("margin repay error")
}
}
}
2022-04-23 07:00:04 +00:00
func (s *Strategy) checkAndBorrow(ctx context.Context) {
if s.MinMarginLevel.IsZero() {
2022-04-23 07:00:04 +00:00
return
}
2022-04-25 10:12:08 +00:00
if err := s.ExchangeSession.UpdateAccount(ctx); err != nil {
log.WithError(err).Errorf("can not update account")
return
}
minMarginLevel := s.MinMarginLevel
account := s.ExchangeSession.GetAccount()
curMarginLevel := account.MarginLevel
log.Infof("current account margin level: %s margin ratio: %s, margin tolerance: %s",
account.MarginLevel.String(),
account.MarginRatio.String(),
account.MarginTolerance.String(),
)
2022-04-23 07:00:04 +00:00
// if margin ratio is too low, do not borrow
if curMarginLevel.Compare(minMarginLevel) < 0 {
log.Infof("current margin level %f < min margin level %f, skip autoborrow", curMarginLevel.Float64(), minMarginLevel.Float64())
s.tryToRepayAnyDebt(ctx)
2022-04-23 07:00:04 +00:00
return
}
balances := s.ExchangeSession.GetAccount().Balances()
if len(balances) == 0 {
log.Warn("balance is empty, skip autoborrow")
return
}
2022-04-23 07:00:04 +00:00
for _, marginAsset := range s.Assets {
if marginAsset.Low.IsZero() {
log.Warnf("margin asset low balance is not set: %+v", marginAsset)
continue
}
b, ok := balances[marginAsset.Asset]
if ok {
2022-04-23 07:18:25 +00:00
toBorrow := marginAsset.Low.Sub(b.Total())
2022-04-23 07:00:04 +00:00
if toBorrow.Sign() < 0 {
2022-04-25 11:10:22 +00:00
log.Infof("balance %f > low %f. no need to borrow asset %+v",
b.Total().Float64(),
marginAsset.Low.Float64(),
marginAsset)
2022-04-23 07:00:04 +00:00
continue
}
if !marginAsset.MaxQuantityPerBorrow.IsZero() {
toBorrow = fixedpoint.Min(toBorrow, marginAsset.MaxQuantityPerBorrow)
}
if !marginAsset.MaxTotalBorrow.IsZero() {
2022-04-23 07:18:25 +00:00
// check if we over borrow
newBorrow := toBorrow.Add(b.Borrowed)
if newBorrow.Compare(marginAsset.MaxTotalBorrow) > 0 {
toBorrow = toBorrow.Sub(newBorrow.Sub(marginAsset.MaxTotalBorrow))
2022-04-23 07:23:43 +00:00
if toBorrow.Sign() < 0 {
log.Warnf("margin asset %s is over borrowed, skip", marginAsset.Asset)
continue
}
2022-04-23 07:18:25 +00:00
}
2022-04-23 07:00:04 +00:00
}
2022-04-25 10:12:08 +00:00
s.Notifiability.Notify(&MarginAction{
Action: "Borrow",
Asset: marginAsset.Asset,
Amount: toBorrow,
MarginLevel: curMarginLevel,
MinMarginLevel: minMarginLevel,
2022-04-25 10:12:08 +00:00
})
log.Infof("sending borrow request %f %s", toBorrow.Float64(), marginAsset.Asset)
2022-04-23 07:00:04 +00:00
s.marginBorrowRepay.BorrowMarginAsset(ctx, marginAsset.Asset, toBorrow)
} else {
// available balance is less than marginAsset.Low, we should trigger borrow
toBorrow := marginAsset.Low
if !marginAsset.MaxQuantityPerBorrow.IsZero() {
toBorrow = fixedpoint.Min(toBorrow, marginAsset.MaxQuantityPerBorrow)
}
2022-04-25 10:12:08 +00:00
s.Notifiability.Notify(&MarginAction{
Action: "Borrow",
Asset: marginAsset.Asset,
Amount: toBorrow,
MarginLevel: curMarginLevel,
MinMarginLevel: minMarginLevel,
2022-04-25 10:12:08 +00:00
})
log.Infof("sending borrow request %f %s", toBorrow.Float64(), marginAsset.Asset)
2022-04-23 07:00:04 +00:00
s.marginBorrowRepay.BorrowMarginAsset(ctx, marginAsset.Asset, toBorrow)
}
}
}
func (s *Strategy) run(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
2022-04-25 09:45:16 +00:00
s.checkAndBorrow(ctx)
2022-04-23 07:00:04 +00:00
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
2022-04-25 09:45:16 +00:00
s.checkAndBorrow(ctx)
2022-04-23 07:00:04 +00:00
}
}
}
func (s *Strategy) handleBalanceUpdate(balances types.BalanceMap) {
if s.MinMarginLevel.IsZero() {
2022-04-23 07:00:04 +00:00
return
}
if s.ExchangeSession.GetAccount().MarginLevel.Compare(s.MinMarginLevel) > 0 {
2022-04-23 07:00:04 +00:00
return
}
for _, b := range balances {
if b.Available.IsZero() && b.Borrowed.IsZero() {
continue
}
}
}
func (s *Strategy) handleBinanceBalanceUpdateEvent(event *binance.BalanceUpdateEvent) {
if s.MinMarginLevel.IsZero() {
2022-04-23 07:00:04 +00:00
return
}
if s.ExchangeSession.GetAccount().MarginLevel.Compare(s.MinMarginLevel) > 0 {
2022-04-23 07:00:04 +00:00
return
}
delta := fixedpoint.MustNewFromString(event.Delta)
// ignore outflow
if delta.Sign() < 0 {
return
}
account := s.ExchangeSession.GetAccount()
minMarginLevel := s.MinMarginLevel
curMarginLevel := account.MarginLevel
2022-04-25 10:12:08 +00:00
if b, ok := account.Balance(event.Asset); ok {
2022-04-23 07:00:04 +00:00
if b.Available.IsZero() || b.Borrowed.IsZero() {
return
}
2022-04-25 10:12:08 +00:00
toRepay := b.Available
s.Notifiability.Notify(&MarginAction{
Action: "Repay",
2022-04-25 10:12:08 +00:00
Asset: b.Currency,
Amount: toRepay,
MarginLevel: curMarginLevel,
MinMarginLevel: minMarginLevel,
2022-04-25 10:12:08 +00:00
})
if err := s.marginBorrowRepay.RepayMarginAsset(context.Background(), event.Asset, toRepay); err != nil {
2022-04-23 07:00:04 +00:00
log.WithError(err).Errorf("margin repay error")
}
}
}
2022-04-25 10:12:08 +00:00
type MarginAction struct {
Action string
Asset string
Amount fixedpoint.Value
MarginLevel fixedpoint.Value
MinMarginLevel fixedpoint.Value
2022-04-25 10:12:08 +00:00
}
func (a *MarginAction) SlackAttachment() slack.Attachment {
return slack.Attachment{
Title: fmt.Sprintf("%s %s %s", a.Action, a.Amount, a.Asset),
Color: "warning",
2022-04-25 10:12:08 +00:00
Fields: []slack.AttachmentField{
{
Title: "Action",
Value: a.Action,
Short: true,
},
{
Title: "Asset",
Value: a.Asset,
Short: true,
},
{
Title: "Amount",
Value: a.Amount.String(),
Short: true,
},
{
2022-04-25 11:15:47 +00:00
Title: "Current Margin Level",
Value: a.MarginLevel.String(),
2022-04-25 10:12:08 +00:00
Short: true,
},
{
2022-04-25 11:15:47 +00:00
Title: "Min Margin Level",
Value: a.MinMarginLevel.String(),
2022-04-25 10:12:08 +00:00
Short: true,
},
},
}
}
2022-04-23 07:00:04 +00:00
// This strategy simply spent all available quote currency to buy the symbol whenever kline gets closed
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
if s.MinMarginLevel.IsZero() {
log.Warnf("minMarginLevel is 0, you should configure this minimal margin ratio for controlling the liquidation risk")
2022-04-23 07:00:04 +00:00
}
2022-04-25 09:54:16 +00:00
s.ExchangeSession = session
2022-04-23 07:00:04 +00:00
marginBorrowRepay, ok := session.Exchange.(types.MarginBorrowRepay)
if !ok {
return fmt.Errorf("exchange %s does not implement types.MarginBorrowRepay", session.ExchangeName)
}
s.marginBorrowRepay = marginBorrowRepay
if s.AutoRepayWhenDeposit {
binanceStream, ok := session.UserDataStream.(*binance.Stream)
if ok {
binanceStream.OnBalanceUpdateEvent(s.handleBinanceBalanceUpdateEvent)
} else {
session.UserDataStream.OnBalanceUpdate(s.handleBalanceUpdate)
}
}
go s.run(ctx, s.Interval.Duration())
return nil
}