grid2: fix ExtendUpperPrice and its tests

This commit is contained in:
c9s 2022-11-08 20:27:55 +08:00
parent 4fb2230e5d
commit 629cea0f44
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 12 additions and 5 deletions

View File

@ -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

View File

@ -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) {