2022-01-30 16:44:04 +00:00
|
|
|
package bbgo
|
|
|
|
|
2022-01-30 17:42:21 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
)
|
2022-01-30 16:44:04 +00:00
|
|
|
|
|
|
|
// QuantityOrAmount is a setting structure used for quantity/amount settings
|
|
|
|
// You can embed this struct into your strategy to share the setting methods
|
|
|
|
type QuantityOrAmount struct {
|
|
|
|
// Quantity is the base order quantity for your buy/sell order.
|
|
|
|
// when quantity is set, the amount option will be not used.
|
|
|
|
Quantity fixedpoint.Value `json:"quantity"`
|
|
|
|
|
|
|
|
// Amount is the order quote amount for your buy/sell order.
|
2022-01-30 17:42:21 +00:00
|
|
|
Amount fixedpoint.Value `json:"amount,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qa *QuantityOrAmount) IsSet() bool {
|
2022-02-03 11:19:56 +00:00
|
|
|
return qa.Quantity.Sign() > 0 || qa.Amount.Sign() > 0
|
2022-01-30 17:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qa *QuantityOrAmount) Validate() error {
|
2022-02-03 11:19:56 +00:00
|
|
|
if qa.Quantity.IsZero() && qa.Amount.IsZero() {
|
2022-01-30 17:42:21 +00:00
|
|
|
return errors.New("either quantity or amount can not be empty")
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-30 16:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateQuantity calculates the equivalent quantity of the given price when amount is set
|
|
|
|
// it returns the quantity if the quantity is set
|
|
|
|
func (qa *QuantityOrAmount) CalculateQuantity(currentPrice fixedpoint.Value) fixedpoint.Value {
|
2022-02-03 11:19:56 +00:00
|
|
|
if qa.Amount.Sign() > 0 {
|
2022-01-30 16:44:04 +00:00
|
|
|
quantity := qa.Amount.Div(currentPrice)
|
|
|
|
return quantity
|
|
|
|
}
|
|
|
|
|
|
|
|
return qa.Quantity
|
|
|
|
}
|