mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
grid2: refactor checkRequiredInvestmentByAmount and checkRequiredInvestmentByQuantity
This commit is contained in:
parent
4eb21d5209
commit
4407aa7f97
|
@ -183,15 +183,7 @@ type InvestmentBudget struct {
|
||||||
quoteBalance fixedpoint.Value
|
quoteBalance fixedpoint.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) checkRequiredInvestmentByQuantity(baseInvestment, quoteInvestment, baseBalance, quoteBalance, quantity, lastPrice fixedpoint.Value, pins []Pin) (requiredBase, requiredQuote fixedpoint.Value, err error) {
|
func (s *Strategy) checkRequiredInvestmentByQuantity(baseBalance, quoteBalance, quantity, lastPrice fixedpoint.Value, pins []Pin) (requiredBase, requiredQuote fixedpoint.Value, err error) {
|
||||||
if baseInvestment.Compare(baseBalance) > 0 {
|
|
||||||
return fixedpoint.Zero, fixedpoint.Zero, fmt.Errorf("baseInvestment setup %f is greater than the total base balance %f", baseInvestment.Float64(), baseBalance.Float64())
|
|
||||||
}
|
|
||||||
|
|
||||||
if quoteInvestment.Compare(quoteBalance) > 0 {
|
|
||||||
return fixedpoint.Zero, fixedpoint.Zero, fmt.Errorf("quoteInvestment setup %f is greater than the total quote balance %f", quoteInvestment.Float64(), quoteBalance.Float64())
|
|
||||||
}
|
|
||||||
|
|
||||||
// check more investment budget details
|
// check more investment budget details
|
||||||
requiredBase = fixedpoint.Zero
|
requiredBase = fixedpoint.Zero
|
||||||
requiredQuote = fixedpoint.Zero
|
requiredQuote = fixedpoint.Zero
|
||||||
|
@ -249,7 +241,7 @@ func (s *Strategy) checkRequiredInvestmentByQuantity(baseInvestment, quoteInvest
|
||||||
return requiredBase, requiredQuote, nil
|
return requiredBase, requiredQuote, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) checkRequiredInvestmentByAmount(baseInvestment, quoteInvestment, baseBalance, quoteBalance, amount, lastPrice fixedpoint.Value, pins []Pin) (requiredBase, requiredQuote fixedpoint.Value, err error) {
|
func (s *Strategy) checkRequiredInvestmentByAmount(baseBalance, quoteBalance, amount, lastPrice fixedpoint.Value, pins []Pin) (requiredBase, requiredQuote fixedpoint.Value, err error) {
|
||||||
|
|
||||||
// check more investment budget details
|
// check more investment budget details
|
||||||
requiredBase = fixedpoint.Zero
|
requiredBase = fixedpoint.Zero
|
||||||
|
@ -340,15 +332,18 @@ func (s *Strategy) setupGridOrders(ctx context.Context, session *bbgo.ExchangeSe
|
||||||
|
|
||||||
// shift 1 grid because we will start from the buy order
|
// shift 1 grid because we will start from the buy order
|
||||||
// if the buy order is filled, then we will submit another sell order at the higher grid.
|
// if the buy order is filled, then we will submit another sell order at the higher grid.
|
||||||
quantityOrAmountIsSet := s.QuantityOrAmount.IsSet()
|
if s.QuantityOrAmount.IsSet() {
|
||||||
if quantityOrAmountIsSet {
|
if quantity := s.QuantityOrAmount.Quantity; !quantity.IsZero() {
|
||||||
if _, _, err2 := s.checkRequiredInvestmentByQuantity(
|
if _, _, err2 := s.checkRequiredInvestmentByQuantity(totalBase, totalQuote, lastPrice, s.QuantityOrAmount.Quantity, s.grid.Pins); err != nil {
|
||||||
s.BaseInvestment, s.QuoteInvestment,
|
|
||||||
totalBase, totalQuote,
|
|
||||||
lastPrice, s.QuantityOrAmount.Quantity, s.grid.Pins); err != nil {
|
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if amount := s.QuantityOrAmount.Amount; !amount.IsZero() {
|
||||||
|
if _, _, err2 := s.checkRequiredInvestmentByAmount(totalBase, totalQuote, lastPrice, amount, s.grid.Pins); err != nil {
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i := len(s.grid.Pins) - 2; i >= 0; i++ {
|
for i := len(s.grid.Pins) - 2; i >= 0; i++ {
|
||||||
pin := s.grid.Pins[i]
|
pin := s.grid.Pins[i]
|
||||||
|
|
|
@ -17,25 +17,19 @@ func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("basic base balance check", func(t *testing.T) {
|
t.Run("basic base balance check", func(t *testing.T) {
|
||||||
_, _, err := s.checkRequiredInvestmentByQuantity(number(2.0), number(10_000.0),
|
_, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(10_000.0), number(0.1), number(19000.0), []Pin{})
|
||||||
number(1.0), number(10_000.0),
|
|
||||||
number(0.1), number(19000.0), []Pin{})
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.EqualError(t, err, "baseInvestment setup 2.000000 is greater than the total base balance 1.000000")
|
assert.EqualError(t, err, "baseInvestment setup 2.000000 is greater than the total base balance 1.000000")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("basic quote balance check", func(t *testing.T) {
|
t.Run("basic quote balance check", func(t *testing.T) {
|
||||||
_, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(10_000.0),
|
_, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(100.0), number(0.1), number(19_000.0), []Pin{})
|
||||||
number(1.0), number(100.0),
|
|
||||||
number(0.1), number(19_000.0), []Pin{})
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.EqualError(t, err, "quoteInvestment setup 10000.000000 is greater than the total quote balance 100.000000")
|
assert.EqualError(t, err, "quoteInvestment setup 10000.000000 is greater than the total quote balance 100.000000")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("quote to base balance conversion check", func(t *testing.T) {
|
t.Run("quote to base balance conversion check", func(t *testing.T) {
|
||||||
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(10_000.0),
|
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(10_000.0), number(0.1), number(13_500.0), []Pin{
|
||||||
number(0.0), number(10_000.0),
|
|
||||||
number(0.1), number(13_500.0), []Pin{
|
|
||||||
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
||||||
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
||||||
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
||||||
|
@ -48,9 +42,7 @@ func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("quote to base balance conversion not enough", func(t *testing.T) {
|
t.Run("quote to base balance conversion not enough", func(t *testing.T) {
|
||||||
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(5_000.0),
|
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(5_000.0), number(0.1), number(13_500.0), []Pin{
|
||||||
number(0.0), number(5_000.0),
|
|
||||||
number(0.1), number(13_500.0), []Pin{
|
|
||||||
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
||||||
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
||||||
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user