From 629cea0f44b45eb5590aa661a4e4f54c41e01ce4 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 8 Nov 2022 20:27:55 +0800 Subject: [PATCH] grid2: fix ExtendUpperPrice and its tests --- pkg/strategy/grid2/grid.go | 6 +++++- pkg/strategy/grid2/grid_test.go | 11 +++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/strategy/grid2/grid.go b/pkg/strategy/grid2/grid.go index d075ed2be..35633f633 100644 --- a/pkg/strategy/grid2/grid.go +++ b/pkg/strategy/grid2/grid.go @@ -87,7 +87,11 @@ func (g *Grid) HasPin(pin Pin) (ok bool) { } func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) { - newPins = calculateArithmeticPins(g.UpperPrice, upper, g.Spread, g.TickSize) + if upper.Compare(g.UpperPrice) <= 0 { + return nil + } + + newPins = calculateArithmeticPins(g.UpperPrice.Add(g.Spread), upper, g.Spread, g.TickSize) g.UpperPrice = upper g.addPins(newPins) return newPins diff --git a/pkg/strategy/grid2/grid_test.go b/pkg/strategy/grid2/grid_test.go index 6568bdcc7..18e5c8095 100644 --- a/pkg/strategy/grid2/grid_test.go +++ b/pkg/strategy/grid2/grid_test.go @@ -51,16 +51,19 @@ func TestGrid_HasPin(t *testing.T) { func TestGrid_ExtendUpperPrice(t *testing.T) { upper := number(500.0) lower := number(100.0) - size := number(40.0) + size := number(4.0) grid := NewGrid(lower, upper, size, number(0.01)) originalSpread := grid.Spread - assert.Equal(t, number(10.0), originalSpread) - assert.Len(t, grid.Pins, 40) // (1000-500) / 4 + t.Logf("pins: %+v", grid.Pins) + assert.Equal(t, number(100.0), originalSpread) + assert.Len(t, grid.Pins, 5) // (1000-500) / 4 newPins := grid.ExtendUpperPrice(number(1000.0)) + assert.Len(t, grid.Pins, 10) + assert.Len(t, newPins, 5) assert.Equal(t, originalSpread, grid.Spread) - assert.Len(t, newPins, 51) // (1000-500) / 4 + t.Logf("pins: %+v", grid.Pins) } func TestGrid_ExtendLowerPrice(t *testing.T) {