mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-23 15:25:14 +00:00
grid: add pin cache
This commit is contained in:
parent
b2f8abd56c
commit
47fbe3a3ed
|
@ -16,6 +16,8 @@ type Grid struct {
|
|||
|
||||
// Pins are the pinned grid prices, from low to high
|
||||
Pins []fixedpoint.Value `json:"pins"`
|
||||
|
||||
pinsCache map[fixedpoint.Value]struct{} `json:"-"`
|
||||
}
|
||||
|
||||
func NewGrid(lower, upper, density fixedpoint.Value) *Grid {
|
||||
|
@ -27,13 +29,27 @@ func NewGrid(lower, upper, density fixedpoint.Value) *Grid {
|
|||
pins = append(pins, p)
|
||||
}
|
||||
|
||||
return &Grid{
|
||||
grid := &Grid{
|
||||
UpperPrice: upper,
|
||||
LowerPrice: lower,
|
||||
Size: density,
|
||||
Spread: size,
|
||||
Pins: pins,
|
||||
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{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Grid) HasPin(pin fixedpoint.Value) (ok bool) {
|
||||
_, ok = g.pinsCache[pin]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []fixedpoint.Value) {
|
||||
|
@ -48,6 +64,7 @@ func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []fixedpoint.Va
|
|||
}
|
||||
|
||||
g.Pins = append(g.Pins, newPins...)
|
||||
g.updatePinsCache()
|
||||
return newPins
|
||||
}
|
||||
|
||||
|
@ -68,5 +85,6 @@ func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []fixedpoint.Va
|
|||
}
|
||||
|
||||
g.Pins = append(newPins, g.Pins...)
|
||||
g.updatePinsCache()
|
||||
return newPins
|
||||
}
|
||||
|
|
|
@ -20,6 +20,17 @@ func TestNewGrid(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGrid_HasPin(t *testing.T) {
|
||||
upper := fixedpoint.NewFromFloat(500.0)
|
||||
lower := fixedpoint.NewFromFloat(100.0)
|
||||
size := fixedpoint.NewFromFloat(100.0)
|
||||
grid := NewGrid(lower, upper, size)
|
||||
|
||||
assert.True(t, grid.HasPin(fixedpoint.NewFromFloat(100.0)))
|
||||
assert.True(t, grid.HasPin(fixedpoint.NewFromFloat(500.0)))
|
||||
assert.False(t, grid.HasPin(fixedpoint.NewFromFloat(101.0)))
|
||||
}
|
||||
|
||||
func TestGrid_ExtendUpperPrice(t *testing.T) {
|
||||
upper := fixedpoint.NewFromFloat(500.0)
|
||||
lower := fixedpoint.NewFromFloat(100.0)
|
||||
|
|
Loading…
Reference in New Issue
Block a user