grid2: include the order dust for the quote investment calculation

This commit is contained in:
c9s 2023-02-16 21:55:53 +08:00
parent 156da92670
commit 55476e4176
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 14 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package grid2
import (
"context"
"fmt"
"math"
"sort"
"strconv"
"sync"
@ -558,6 +559,7 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
// q = quoteInvestment / (p1 + p2 + p3)
totalQuotePrice := fixedpoint.Zero
si := len(pins)
cntOrder := 0
for i := len(pins) - 1; i >= 0; i-- {
pin := pins[i]
price := fixedpoint.Value(pin)
@ -581,6 +583,8 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
nextLowerPrice := fixedpoint.Value(nextLowerPin)
totalQuotePrice = totalQuotePrice.Add(nextLowerPrice)
}
cntOrder++
} else {
// for orders that buy
if s.ProfitSpread.IsZero() && i+1 == si {
@ -593,11 +597,14 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
}
totalQuotePrice = totalQuotePrice.Add(price)
cntOrder++
}
}
q := quoteInvestment.Div(totalQuotePrice)
s.logger.Infof("calculateQuoteInvestmentQuantity: sumOfPrice=%f quantity=%f", totalQuotePrice.Float64(), q.Float64())
orderDusts := fixedpoint.NewFromFloat(math.Pow10(-s.Market.PricePrecision) * float64(cntOrder))
adjustedQuoteInvestment := quoteInvestment.Sub(orderDusts)
q := adjustedQuoteInvestment.Div(totalQuotePrice)
s.logger.Infof("calculateQuoteInvestmentQuantity: adjustedQuoteInvestment=%f sumOfPrice=%f quantity=%f", adjustedQuoteInvestment.Float64(), totalQuotePrice.Float64(), q.Float64())
return q, nil
}

View File

@ -125,7 +125,7 @@ func TestStrategy_generateGridOrders(t *testing.T) {
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, s.grid.Pins)
assert.NoError(t, err)
assert.Equal(t, number(38.75968992).String(), quantity.String())
assert.InDelta(t, 38.7364341, quantity.Float64(), 0.00001)
s.QuantityOrAmount.Quantity = quantity
@ -284,7 +284,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
Pin(number(15_000.0)),
})
assert.NoError(t, err)
assert.Equal(t, number(0.2).String(), quantity.String())
assert.InDelta(t, 0.199999916, quantity.Float64(), 0.0001)
})
t.Run("quote quantity #2", func(t *testing.T) {
@ -301,7 +301,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
Pin(number(200.00)),
})
assert.NoError(t, err)
assert.Equal(t, number(1.17647058).String(), quantity.String())
assert.InDelta(t, 1.1764, quantity.Float64(), 0.00001)
})
t.Run("quote quantity #3", func(t *testing.T) {
@ -319,7 +319,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
}
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, pins)
assert.NoError(t, err)
assert.InDelta(t, 38.75968992, quantity.Float64(), 0.0001)
assert.InDelta(t, 38.736434, quantity.Float64(), 0.0001)
var totalQuoteUsed = fixedpoint.Zero
for i, pin := range pins {
@ -351,7 +351,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
Pin(number(15_000.0)), // sell order @ 17_000
})
assert.NoError(t, err)
assert.Equal(t, number(0.1).String(), quantity.String())
assert.InDelta(t, 0.099992, quantity.Float64(), 0.0001)
})
}