bbgo_origin/pkg/strategy/shakegrid/grid.go

91 lines
2.1 KiB
Go
Raw Normal View History

2021-10-29 05:27:12 +00:00
package shakegrid
2021-10-29 06:00:55 +00:00
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
)
2021-10-29 05:27:12 +00:00
type Grid struct {
UpperPrice fixedpoint.Value `json:"upperPrice"`
LowerPrice fixedpoint.Value `json:"lowerPrice"`
2021-10-29 06:00:55 +00:00
// Spread is the spread of each grid
Spread fixedpoint.Value `json:"spread"`
2021-10-29 05:27:12 +00:00
2021-10-29 06:00:55 +00:00
// Size is the number of total grids
Size fixedpoint.Value `json:"size"`
2021-10-29 05:27:12 +00:00
// Pins are the pinned grid prices, from low to high
Pins []fixedpoint.Value `json:"pins"`
2021-10-29 06:08:56 +00:00
pinsCache map[fixedpoint.Value]struct{} `json:"-"`
2021-10-29 05:27:12 +00:00
}
func NewGrid(lower, upper, density fixedpoint.Value) *Grid {
var height = upper - lower
var size = height.Div(density)
var pins []fixedpoint.Value
2021-10-29 06:00:55 +00:00
for p := lower; p <= upper; p += size {
2021-10-29 05:27:12 +00:00
pins = append(pins, p)
}
2021-10-29 06:08:56 +00:00
grid := &Grid{
2021-10-29 05:27:12 +00:00
UpperPrice: upper,
LowerPrice: lower,
2021-10-29 06:00:55 +00:00
Size: density,
Spread: size,
Pins: pins,
2021-10-29 06:08:56 +00:00
pinsCache: make(map[fixedpoint.Value]struct{}, len(pins)),
}
grid.updatePinsCache()
return grid
}
func (g *Grid) updatePinsCache() {
for _, pin := range g.Pins {
g.pinsCache[pin] = struct{}{}
2021-10-29 05:27:12 +00:00
}
}
2021-10-29 06:08:56 +00:00
func (g *Grid) HasPin(pin fixedpoint.Value) (ok bool) {
_, ok = g.pinsCache[pin]
return ok
}
2021-10-29 06:00:55 +00:00
func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []fixedpoint.Value) {
g.UpperPrice = upper
2021-10-29 05:27:12 +00:00
2021-10-29 06:00:55 +00:00
// since the grid is extended, the size should be updated as well
2021-10-29 06:04:56 +00:00
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor()
2021-10-29 06:00:55 +00:00
2021-10-29 06:08:56 +00:00
lastPin := g.Pins[len(g.Pins)-1]
2021-10-29 06:00:55 +00:00
for p := lastPin + g.Spread; p <= g.UpperPrice; p += g.Spread {
newPins = append(newPins, p)
}
g.Pins = append(g.Pins, newPins...)
2021-10-29 06:08:56 +00:00
g.updatePinsCache()
2021-10-29 06:00:55 +00:00
return newPins
2021-10-29 05:27:12 +00:00
}
2021-10-29 06:00:55 +00:00
func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []fixedpoint.Value) {
g.LowerPrice = lower
// since the grid is extended, the size should be updated as well
2021-10-29 06:04:56 +00:00
g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor()
2021-10-29 06:00:55 +00:00
firstPin := g.Pins[0]
2021-10-29 06:04:56 +00:00
numToAdd := (firstPin - g.LowerPrice).Div(g.Spread).Floor()
if numToAdd == 0 {
return newPins
}
2021-10-29 06:00:55 +00:00
for p := firstPin - g.Spread.Mul(numToAdd); p < firstPin; p += g.Spread {
newPins = append(newPins, p)
}
2021-10-29 05:27:12 +00:00
2021-10-29 06:00:55 +00:00
g.Pins = append(newPins, g.Pins...)
2021-10-29 06:08:56 +00:00
g.updatePinsCache()
2021-10-29 06:00:55 +00:00
return newPins
2021-10-29 05:27:12 +00:00
}