add more grid extend tests

This commit is contained in:
c9s 2021-10-29 14:04:56 +08:00 committed by c9s
parent 56e40eff67
commit b2f8abd56c
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 11 additions and 4 deletions

View File

@ -40,7 +40,7 @@ func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []fixedpoint.Va
g.UpperPrice = upper
// since the grid is extended, the size should be updated as well
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Ceil()
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor()
lastPin := g.Pins[ len(g.Pins) - 1 ]
for p := lastPin + g.Spread; p <= g.UpperPrice; p += g.Spread {
@ -55,10 +55,13 @@ func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []fixedpoint.Va
g.LowerPrice = lower
// since the grid is extended, the size should be updated as well
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Ceil()
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor()
firstPin := g.Pins[0]
numToAdd := (firstPin - g.LowerPrice).Div(g.Spread).Ceil()
numToAdd := (firstPin - g.LowerPrice).Div(g.Spread).Floor()
if numToAdd == 0 {
return newPins
}
for p := firstPin - g.Spread.Mul(numToAdd); p < firstPin; p += g.Spread {
newPins = append(newPins, p)

View File

@ -61,4 +61,8 @@ func TestGrid_ExtendLowerPrice(t *testing.T) {
assert.Equal(t, fixedpoint.NewFromFloat(1000.0), grid.Pins[0])
assert.Equal(t, fixedpoint.NewFromFloat(3000.0), grid.Pins[200])
}
newPins2 := grid.ExtendLowerPrice(
fixedpoint.NewFromFloat(1000.0 - 1.0))
assert.Len(t, newPins2, 0) // should have no new pin generated
}