grid2: fix calculateMinimalQuoteInvestment

This commit is contained in:
c9s 2023-02-21 17:48:40 +08:00
parent 0402fddea3
commit 9c1110fb44
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 31 additions and 15 deletions

View File

@ -1120,15 +1120,27 @@ func (s *Strategy) getLastTradePrice(ctx context.Context, session *bbgo.Exchange
return fixedpoint.Zero, fmt.Errorf("%s ticker price not found", s.Symbol)
}
func calculateMinimalQuoteInvestment(market types.Market, lowerPrice, upperPrice fixedpoint.Value, gridNum int64) fixedpoint.Value {
num := fixedpoint.NewFromInt(gridNum - 1)
minimalAmountLowerPrice := fixedpoint.Max(lowerPrice.Mul(market.MinQuantity), market.MinNotional)
minimalAmountUpperPrice := fixedpoint.Max(upperPrice.Mul(market.MinQuantity), market.MinNotional)
return fixedpoint.Max(minimalAmountLowerPrice, minimalAmountUpperPrice).Mul(num)
func calculateMinimalQuoteInvestment(market types.Market, grid *Grid) fixedpoint.Value {
// upperPrice for buy order
upperPrice := grid.UpperPrice.Sub(grid.Spread)
minQuantity := fixedpoint.Max(
fixedpoint.Max(upperPrice.Mul(market.MinQuantity), market.MinNotional).Div(upperPrice),
market.MinQuantity,
)
var pins = grid.Pins
var totalQuote = fixedpoint.Zero
for i := len(pins) - 2; i >= 0; i-- {
pin := pins[i]
price := fixedpoint.Value(pin)
totalQuote = totalQuote.Add(price.Mul(minQuantity))
}
return totalQuote
}
func (s *Strategy) checkMinimalQuoteInvestment() error {
minimalQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
func (s *Strategy) checkMinimalQuoteInvestment(grid *Grid) error {
minimalQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, grid)
if s.QuoteInvestment.Compare(minimalQuoteInvestment) <= 0 {
return fmt.Errorf("need at least %f %s for quote investment, %f %s given",
minimalQuoteInvestment.Float64(),
@ -1475,7 +1487,8 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
// we need to check the minimal quote investment here, because we need the market info
if s.QuoteInvestment.Sign() > 0 {
if err := s.checkMinimalQuoteInvestment(); err != nil {
grid := s.newGrid()
if err := s.checkMinimalQuoteInvestment(grid); err != nil {
return err
}
}

View File

@ -894,22 +894,25 @@ func TestStrategy_checkMinimalQuoteInvestment(t *testing.T) {
// hence we should have at least: 20USDT * 10 grids
s.QuoteInvestment = number(10_000)
s.GridNum = 10
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
assert.Equal(t, "180", minQuoteInvestment.String())
grid := s.newGrid()
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, grid)
assert.InDelta(t, 129.9999, minQuoteInvestment.Float64(), 0.01)
err := s.checkMinimalQuoteInvestment()
err := s.checkMinimalQuoteInvestment(grid)
assert.NoError(t, err)
})
t.Run("1000 grids", func(t *testing.T) {
s.QuoteInvestment = number(10_000)
s.GridNum = 1000
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
assert.Equal(t, "19980", minQuoteInvestment.String())
err := s.checkMinimalQuoteInvestment()
grid := s.newGrid()
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, grid)
assert.InDelta(t, 14979.995499, minQuoteInvestment.Float64(), 0.001)
err := s.checkMinimalQuoteInvestment(grid)
assert.Error(t, err)
assert.EqualError(t, err, "need at least 19980.000000 USDT for quote investment, 10000.000000 USDT given")
assert.EqualError(t, err, "need at least 14979.995500 USDT for quote investment, 10000.000000 USDT given")
})
}