grid2: fix si index check

This commit is contained in:
c9s 2023-02-15 16:51:12 +08:00
parent 44210bf26a
commit 35bfdfab8d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 50 additions and 2 deletions

View File

@ -934,7 +934,7 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin
var submitOrders []types.SubmitOrder var submitOrders []types.SubmitOrder
// si is for sell order price index // si is for sell order price index
var si = len(pins) - 1 var si = len(pins)
for i := len(pins) - 1; i >= 0; i-- { for i := len(pins) - 1; i >= 0; i-- {
pin := pins[i] pin := pins[i]
price := fixedpoint.Value(pin) price := fixedpoint.Value(pin)

View File

@ -77,7 +77,9 @@ func TestStrategy_generateGridOrders(t *testing.T) {
s.QuantityOrAmount.Quantity = number(0.01) s.QuantityOrAmount.Quantity = number(0.01)
lastPrice := number(15300) lastPrice := number(15300)
orders, err := s.generateGridOrders(number(10000.0), number(0), lastPrice) quoteInvestment := number(10000.0)
baseInvestment := number(0)
orders, err := s.generateGridOrders(quoteInvestment, baseInvestment, lastPrice)
assert.NoError(t, err) assert.NoError(t, err)
if !assert.Equal(t, 10, len(orders)) { if !assert.Equal(t, 10, len(orders)) {
for _, o := range orders { for _, o := range orders {
@ -99,6 +101,52 @@ func TestStrategy_generateGridOrders(t *testing.T) {
}, orders) }, orders)
}) })
t.Run("quote only + buy only", func(t *testing.T) {
s := newTestStrategy()
s.UpperPrice = number(0.9)
s.LowerPrice = number(0.1)
s.GridNum = 7
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
s.grid.CalculateArithmeticPins()
assert.Equal(t, []Pin{
Pin(number(0.1)),
Pin(number(0.23)),
Pin(number(0.36)),
Pin(number(0.50)),
Pin(number(0.63)),
Pin(number(0.76)),
Pin(number(0.9)),
}, s.grid.Pins, "pins are correct")
lastPrice := number(22100)
quoteInvestment := number(100.0)
baseInvestment := number(0)
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, s.grid.Pins)
assert.NoError(t, err)
assert.Equal(t, number(38.75968992).String(), quantity.String())
s.QuantityOrAmount.Quantity = quantity
orders, err := s.generateGridOrders(quoteInvestment, baseInvestment, lastPrice)
assert.NoError(t, err)
if !assert.Equal(t, 6, len(orders)) {
for _, o := range orders {
t.Logf("- %s %s", o.Price.String(), o.Side)
}
}
assertPriceSide(t, []PriceSideAssert{
{number(0.76), types.SideTypeBuy},
{number(0.63), types.SideTypeBuy},
{number(0.5), types.SideTypeBuy},
{number(0.36), types.SideTypeBuy},
{number(0.23), types.SideTypeBuy},
{number(0.1), types.SideTypeBuy},
}, orders)
})
t.Run("base + quote", func(t *testing.T) { t.Run("base + quote", func(t *testing.T) {
s := newTestStrategy() s := newTestStrategy()
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize) s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)