From 4407aa7f9788875950d0ed0178ddac2d2ea6102d Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 17 Nov 2022 17:40:59 +0800 Subject: [PATCH] grid2: refactor checkRequiredInvestmentByAmount and checkRequiredInvestmentByQuantity --- pkg/strategy/grid2/strategy.go | 29 ++++++++----------- pkg/strategy/grid2/strategy_test.go | 44 ++++++++++++----------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index d4b981f79..4b2d69765 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -183,15 +183,7 @@ type InvestmentBudget struct { quoteBalance fixedpoint.Value } -func (s *Strategy) checkRequiredInvestmentByQuantity(baseInvestment, quoteInvestment, 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()) - } - +func (s *Strategy) checkRequiredInvestmentByQuantity(baseBalance, quoteBalance, quantity, lastPrice fixedpoint.Value, pins []Pin) (requiredBase, requiredQuote fixedpoint.Value, err error) { // check more investment budget details requiredBase = fixedpoint.Zero requiredQuote = fixedpoint.Zero @@ -249,7 +241,7 @@ func (s *Strategy) checkRequiredInvestmentByQuantity(baseInvestment, quoteInvest 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 requiredBase = fixedpoint.Zero @@ -340,13 +332,16 @@ func (s *Strategy) setupGridOrders(ctx context.Context, session *bbgo.ExchangeSe // 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. - quantityOrAmountIsSet := s.QuantityOrAmount.IsSet() - if quantityOrAmountIsSet { - if _, _, err2 := s.checkRequiredInvestmentByQuantity( - s.BaseInvestment, s.QuoteInvestment, - totalBase, totalQuote, - lastPrice, s.QuantityOrAmount.Quantity, s.grid.Pins); err != nil { - return err2 + if s.QuantityOrAmount.IsSet() { + if quantity := s.QuantityOrAmount.Quantity; !quantity.IsZero() { + if _, _, err2 := s.checkRequiredInvestmentByQuantity(totalBase, totalQuote, lastPrice, s.QuantityOrAmount.Quantity, s.grid.Pins); err != nil { + return err2 + } + } + if amount := s.QuantityOrAmount.Amount; !amount.IsZero() { + if _, _, err2 := s.checkRequiredInvestmentByAmount(totalBase, totalQuote, lastPrice, amount, s.grid.Pins); err != nil { + return err2 + } } } diff --git a/pkg/strategy/grid2/strategy_test.go b/pkg/strategy/grid2/strategy_test.go index ff3163497..41217c4bb 100644 --- a/pkg/strategy/grid2/strategy_test.go +++ b/pkg/strategy/grid2/strategy_test.go @@ -17,47 +17,39 @@ func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) { } t.Run("basic base balance check", func(t *testing.T) { - _, _, err := s.checkRequiredInvestmentByQuantity(number(2.0), number(10_000.0), - number(1.0), number(10_000.0), - number(0.1), number(19000.0), []Pin{}) + _, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(10_000.0), number(0.1), number(19000.0), []Pin{}) assert.Error(t, err) 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) { - _, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(10_000.0), - number(1.0), number(100.0), - number(0.1), number(19_000.0), []Pin{}) + _, _, err := s.checkRequiredInvestmentByQuantity(number(1.0), number(100.0), number(0.1), number(19_000.0), []Pin{}) assert.Error(t, err) 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) { - _, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(10_000.0), - 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(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy) - Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy) - Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy) - Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy) - Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD - }) + _, requiredQuote, err := s.checkRequiredInvestmentByQuantity(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(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy) + Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy) + Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy) + Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy) + Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD + }) assert.NoError(t, err) assert.Equal(t, number(6000.0), requiredQuote) }) t.Run("quote to base balance conversion not enough", func(t *testing.T) { - _, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(5_000.0), - 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(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy) - Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy) - Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy) - Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy) - Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD - }) + _, requiredQuote, err := s.checkRequiredInvestmentByQuantity(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(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy) + Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy) + Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy) + Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy) + Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD + }) assert.EqualError(t, err, "quote balance (5000.000000 USDT) is not enough, required = quote 6000.000000") assert.Equal(t, number(6000.0), requiredQuote) })