xgap: check balance and adjust order quantity according to the available balance

This commit is contained in:
c9s 2021-12-28 02:11:11 +08:00
parent a8546ce935
commit 8f4ae1e15b

View File

@ -286,6 +286,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
log.Infof("mid price %f", midPrice.Float64())
var balances = s.tradingSession.Account.Balances()
var quantity = s.tradingMarket.MinQuantity
if s.Quantity > 0 {
@ -299,6 +300,15 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
if volumeDiff > 0 {
quantity = volumeDiff
}
if baseBalance, ok := balances[s.tradingMarket.BaseCurrency]; ok {
quantity = math.Min(quantity, baseBalance.Available.Float64())
}
if quoteBalance, ok := balances[s.tradingMarket.QuoteCurrency]; ok {
maxQuantity := quoteBalance.Available.Float64() / price
quantity = math.Min(quantity, maxQuantity)
}
}
s.mu.Unlock()
}