From e675a084e2c1ddc61ee55e903a64977b2429e486 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 6 Nov 2022 10:06:57 +0800 Subject: [PATCH] grid2: refactor spread, height methods --- pkg/strategy/grid2/grid.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/strategy/grid2/grid.go b/pkg/strategy/grid2/grid.go index 054e8f17c..555223fab 100644 --- a/pkg/strategy/grid2/grid.go +++ b/pkg/strategy/grid2/grid.go @@ -10,9 +10,6 @@ type Grid struct { UpperPrice fixedpoint.Value `json:"upperPrice"` LowerPrice fixedpoint.Value `json:"lowerPrice"` - // Spread is the spread of each grid - Spread fixedpoint.Value `json:"spread"` - // Size is the number of total grids Size fixedpoint.Value `json:"size"` @@ -48,15 +45,12 @@ func buildPinCache(pins []Pin) map[Pin]struct{} { } func NewGrid(lower, upper, size, tickSize fixedpoint.Value) *Grid { - var height = upper.Sub(lower) - var spread = height.Div(size) var pins = calculateArithmeticPins(lower, upper, size, tickSize) grid := &Grid{ UpperPrice: upper, LowerPrice: lower, Size: size, - Spread: spread, Pins: pins, pinsCache: buildPinCache(pins), } @@ -64,6 +58,15 @@ func NewGrid(lower, upper, size, tickSize fixedpoint.Value) *Grid { return grid } +func (g *Grid) Height() fixedpoint.Value { + return g.UpperPrice.Sub(g.LowerPrice) +} + +// Spread returns the spread of each grid +func (g *Grid) Spread() fixedpoint.Value { + return g.Height().Div(g.Size) +} + func (g *Grid) Above(price fixedpoint.Value) bool { return price.Compare(g.UpperPrice) > 0 } @@ -89,10 +92,11 @@ func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) { g.UpperPrice = upper // since the grid is extended, the size should be updated as well - g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor() + 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(g.Spread); p <= g.UpperPrice; p += g.Spread { + for p := lastPinPrice.Add(spread); p <= g.UpperPrice; p = p.Add(spread) { newPins = append(newPins, Pin(p)) } @@ -102,18 +106,22 @@ func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) { } func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin) { + spread := g.Spread() + g.LowerPrice = lower + height := g.Height() + // since the grid is extended, the size should be updated as well - g.Size = (g.UpperPrice - g.LowerPrice).Div(g.Spread).Floor() + g.Size = height.Div(spread).Floor() firstPinPrice := fixedpoint.Value(g.Pins[0]) - numToAdd := (firstPinPrice.Sub(g.LowerPrice)).Div(g.Spread).Floor() + numToAdd := (firstPinPrice.Sub(g.LowerPrice)).Div(spread).Floor() if numToAdd == 0 { return newPins } - for p := firstPinPrice.Sub(g.Spread.Mul(numToAdd)); p.Compare(firstPinPrice) < 0; p = p.Add(g.Spread) { + for p := firstPinPrice.Sub(spread.Mul(numToAdd)); p.Compare(firstPinPrice) < 0; p = p.Add(spread) { newPins = append(newPins, Pin(p)) }