strategy/schedule: add MaxBaseBalance config

This commit is contained in:
c9s 2022-08-19 16:48:43 +08:00
parent 4622f9f34e
commit 834487d568
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -35,6 +36,8 @@ type Strategy struct {
bbgo.QuantityOrAmount
MaxBaseBalance fixedpoint.Value `json:"maxBaseBalance"`
BelowMovingAverage *bbgo.MovingAverageSettings `json:"belowMovingAverage,omitempty"`
AboveMovingAverage *bbgo.MovingAverageSettings `json:"aboveMovingAverage,omitempty"`
@ -161,6 +164,16 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// execute orders
switch side {
case types.SideTypeBuy:
if !s.MaxBaseBalance.IsZero() {
if baseBalance, ok := session.GetAccount().Balance(s.Market.BaseCurrency); ok {
total := baseBalance.Total()
if total.Add(quantity).Compare(s.MaxBaseBalance) >= 0 {
quantity = s.MaxBaseBalance.Sub(total)
quoteQuantity = quantity.Mul(closePrice)
}
}
}
quoteBalance, ok := session.GetAccount().Balance(s.Market.QuoteCurrency)
if !ok {
log.Errorf("can not place scheduled %s order, quote balance %s is empty", s.Symbol, s.Market.QuoteCurrency)
@ -180,12 +193,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return
}
if baseBalance.Available.Compare(quantity) < 0 {
bbgo.Notify("Can not place scheduled %s order: base balance %s is not enough: %v < %v", s.Symbol, s.Market.QuoteCurrency, baseBalance.Available, quantity)
log.Errorf("can not place scheduled %s order: base balance %s is not enough: %v < %v", s.Symbol, s.Market.QuoteCurrency, baseBalance.Available, quantity)
return
}
quantity = fixedpoint.Min(quantity, baseBalance.Available)
quoteQuantity = quantity.Mul(closePrice)
}
if s.Market.IsDustQuantity(quantity, closePrice) {
log.Warnf("%s: quantity %f is too small", s.Symbol, quantity.Float64())
return
}
bbgo.Notify("Submitting scheduled %s order with quantity %s at price %s", s.Symbol, quantity.String(), closePrice.String())