2020-09-16 06:05:03 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2022-09-19 01:10:59 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2020-09-16 06:05:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrQuoteBalanceLevelTooLow = errors.New("quote balance level is too low")
|
|
|
|
ErrInsufficientQuoteBalance = errors.New("insufficient quote balance")
|
|
|
|
|
|
|
|
ErrAssetBalanceLevelTooLow = errors.New("asset balance level too low")
|
|
|
|
ErrInsufficientAssetBalance = errors.New("insufficient asset balance")
|
|
|
|
ErrAssetBalanceLevelTooHigh = errors.New("asset balance level too high")
|
|
|
|
)
|
|
|
|
|
2022-09-19 01:10:59 +00:00
|
|
|
// AdjustQuantityByMaxAmount adjusts the quantity to make the amount less than the given maxAmount
|
2021-05-15 01:46:07 +00:00
|
|
|
func AdjustQuantityByMaxAmount(quantity, currentPrice, maxAmount fixedpoint.Value) fixedpoint.Value {
|
|
|
|
// modify quantity for the min amount
|
|
|
|
amount := currentPrice.Mul(quantity)
|
2022-02-03 11:19:56 +00:00
|
|
|
if amount.Compare(maxAmount) < 0 {
|
2021-07-08 02:26:20 +00:00
|
|
|
return quantity
|
2021-05-15 01:46:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 02:26:20 +00:00
|
|
|
ratio := maxAmount.Div(amount)
|
|
|
|
return quantity.Mul(ratio)
|
2021-05-15 01:46:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 12:13:23 +00:00
|
|
|
// AdjustQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
|
2021-05-14 06:52:19 +00:00
|
|
|
func AdjustQuantityByMinAmount(quantity, currentPrice, minAmount fixedpoint.Value) fixedpoint.Value {
|
|
|
|
// modify quantity for the min amount
|
|
|
|
amount := currentPrice.Mul(quantity)
|
2022-02-03 11:19:56 +00:00
|
|
|
if amount.Compare(minAmount) < 0 {
|
2021-05-14 06:52:19 +00:00
|
|
|
ratio := minAmount.Div(amount)
|
|
|
|
quantity = quantity.Mul(ratio)
|
|
|
|
}
|
|
|
|
|
|
|
|
return quantity
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdjustFloatQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
|
2022-02-03 11:19:56 +00:00
|
|
|
func AdjustFloatQuantityByMinAmount(quantity, currentPrice, minAmount fixedpoint.Value) fixedpoint.Value {
|
2020-09-16 06:05:03 +00:00
|
|
|
// modify quantity for the min amount
|
2022-02-03 11:19:56 +00:00
|
|
|
amount := currentPrice.Mul(quantity)
|
|
|
|
if amount.Compare(minAmount) < 0 {
|
|
|
|
ratio := minAmount.Div(amount)
|
|
|
|
return quantity.Mul(ratio)
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return quantity
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
func AdjustFloatQuantityByMaxAmount(quantity fixedpoint.Value, price fixedpoint.Value, maxAmount fixedpoint.Value) fixedpoint.Value {
|
|
|
|
amount := price.Mul(quantity)
|
|
|
|
if amount.Compare(maxAmount) > 0 {
|
|
|
|
ratio := maxAmount.Div(amount)
|
|
|
|
return quantity.Mul(ratio)
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return quantity
|
|
|
|
}
|