mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
grid2: improve ExtendLowerPrice
This commit is contained in:
parent
e675a084e2
commit
d6f751c027
|
@ -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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user