grid2: rewrite ExtendUpperPrice

This commit is contained in:
c9s 2022-11-06 10:14:07 +08:00
parent 533587ffd2
commit 725c624281
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -90,19 +90,15 @@ func (g *Grid) HasPin(pin Pin) (ok bool) {
}
func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) {
g.UpperPrice = upper
// since the grid is extended, the size should be updated as well
spread := g.Spread()
g.Size = (g.UpperPrice - g.LowerPrice).Div(spread).Floor()
lastPinPrice := fixedpoint.Value(g.Pins[len(g.Pins)-1])
for p := lastPinPrice.Add(spread); p <= g.UpperPrice; p = p.Add(spread) {
startPrice := g.UpperPrice.Add(spread)
for p := startPrice; p.Compare(upper) <= 0; p = p.Add(spread) {
newPins = append(newPins, Pin(p))
}
g.Pins = append(g.Pins, newPins...)
g.updatePinsCache()
g.UpperPrice = upper
g.addPins(newPins)
return newPins
}