risk: move spot condition to the top

This commit is contained in:
c9s 2022-07-22 12:04:43 +08:00
parent 36cfaa924d
commit a1387bb4dd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -115,21 +115,38 @@ func calculateAccountNetValue(session *bbgo.ExchangeSession) (fixedpoint.Value,
} }
func CalculateBaseQuantity(session *bbgo.ExchangeSession, market types.Market, price, quantity, leverage fixedpoint.Value) (fixedpoint.Value, error) { func CalculateBaseQuantity(session *bbgo.ExchangeSession, market types.Market, price, quantity, leverage fixedpoint.Value) (fixedpoint.Value, error) {
// default leverage guard
if leverage.IsZero() { if leverage.IsZero() {
leverage = fixedpoint.NewFromInt(3) leverage = fixedpoint.NewFromInt(3)
} }
baseBalance, _ := session.Account.Balance(market.BaseCurrency)
quoteBalance, _ := session.Account.Balance(market.QuoteCurrency)
usingLeverage := session.Margin || session.IsolatedMargin || session.Futures || session.IsolatedFutures usingLeverage := session.Margin || session.IsolatedMargin || session.Futures || session.IsolatedFutures
if usingLeverage { if !usingLeverage {
// For spot, we simply sell the base quoteCurrency
balance, hasBalance := session.Account.Balance(market.BaseCurrency)
if hasBalance {
if quantity.IsZero() {
logrus.Warnf("sell quantity is not set, using all available base balance: %v", balance)
if !balance.Available.IsZero() {
return balance.Available, nil
}
} else {
return fixedpoint.Min(quantity, balance.Available), nil
}
}
return quantity, fmt.Errorf("quantity is zero, can not submit sell order, please check your quantity settings")
}
// using leverage -- starts from here
if !quantity.IsZero() { if !quantity.IsZero() {
return quantity, nil return quantity, nil
} }
// quantity is zero, we need to calculate the quantity logrus.Infof("calculating available leveraged base quantity: base balance = %+v, quote balance = %+v", baseBalance, quoteBalance)
baseBalance, _ := session.Account.Balance(market.BaseCurrency)
quoteBalance, _ := session.Account.Balance(market.QuoteCurrency)
logrus.Infof("calculating leveraged quantity: base balance = %+v, quote balance = %+v", baseBalance, quoteBalance)
// calculate the quantity automatically // calculate the quantity automatically
if session.Margin || session.IsolatedMargin { if session.Margin || session.IsolatedMargin {
@ -174,20 +191,6 @@ func CalculateBaseQuantity(session *bbgo.ExchangeSession, market types.Market, p
return maxPositionQuantity, nil return maxPositionQuantity, nil
} }
}
// For spot, we simply sell the base quoteCurrency
balance, hasBalance := session.Account.Balance(market.BaseCurrency)
if hasBalance {
if quantity.IsZero() {
logrus.Warnf("sell quantity is not set, submitting sell with all base balance: %s", balance.Available.String())
if !balance.Available.IsZero() {
return balance.Available, nil
}
} else {
return fixedpoint.Min(quantity, balance.Available), nil
}
}
return quantity, fmt.Errorf("quantity is zero, can not submit sell order, please check your settings") return quantity, fmt.Errorf("quantity is zero, can not submit sell order, please check your settings")
} }