grid2: improve ExtendLowerPrice

This commit is contained in:
c9s 2022-11-06 10:12:50 +08:00
parent e675a084e2
commit d6f751c027
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -2,6 +2,7 @@ package grid2
import ( import (
"math" "math"
"sort"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
@ -108,24 +109,23 @@ func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) {
func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin) { func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin) {
spread := g.Spread() spread := g.Spread()
g.LowerPrice = lower startPrice := g.LowerPrice.Sub(spread)
for p := startPrice; p.Compare(lower) >= 0; p = p.Sub(spread) {
height := g.Height()
// since the grid is extended, the size should be updated as well
g.Size = height.Div(spread).Floor()
firstPinPrice := fixedpoint.Value(g.Pins[0])
numToAdd := (firstPinPrice.Sub(g.LowerPrice)).Div(spread).Floor()
if numToAdd == 0 {
return newPins
}
for p := firstPinPrice.Sub(spread.Mul(numToAdd)); p.Compare(firstPinPrice) < 0; p = p.Add(spread) {
newPins = append(newPins, Pin(p)) newPins = append(newPins, Pin(p))
} }
g.Pins = append(newPins, g.Pins...) g.addPins(newPins)
g.updatePinsCache()
return newPins return newPins
} }
func (g *Grid) addPins(pins []Pin) {
g.Pins = append(g.Pins, pins...)
sort.Slice(g.Pins, func(i, j int) bool {
a := fixedpoint.Value(g.Pins[i])
b := fixedpoint.Value(g.Pins[j])
return a.Compare(b) < 0
})
g.updatePinsCache()
}