mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
grid2: implement find next higher/lower pin
This commit is contained in:
parent
629cea0f44
commit
84c3d386ca
|
@ -86,6 +86,34 @@ func (g *Grid) HasPin(pin Pin) (ok bool) {
|
|||
return ok
|
||||
}
|
||||
|
||||
// NextHigherPin finds the next higher pin
|
||||
func (g *Grid) NextHigherPin(price fixedpoint.Value) (Pin, bool) {
|
||||
i := g.SearchPin(price)
|
||||
if i < len(g.Pins) && fixedpoint.Value(g.Pins[i]).Compare(price) == 0 && i+1 < len(g.Pins) {
|
||||
return g.Pins[i+1], true
|
||||
}
|
||||
|
||||
return Pin(fixedpoint.Zero), false
|
||||
}
|
||||
|
||||
// NextLowerPin finds the next lower pin
|
||||
func (g *Grid) NextLowerPin(price fixedpoint.Value) (Pin, bool) {
|
||||
i := g.SearchPin(price)
|
||||
if i < len(g.Pins) && fixedpoint.Value(g.Pins[i]).Compare(price) == 0 && i-1 >= 0 {
|
||||
return g.Pins[i-1], true
|
||||
}
|
||||
|
||||
return Pin(fixedpoint.Zero), false
|
||||
}
|
||||
|
||||
func (g *Grid) SearchPin(price fixedpoint.Value) int {
|
||||
i := sort.Search(len(g.Pins), func(i int) bool {
|
||||
a := fixedpoint.Value(g.Pins[i])
|
||||
return a.Compare(price) >= 0
|
||||
})
|
||||
return i
|
||||
}
|
||||
|
||||
func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) {
|
||||
if upper.Compare(g.UpperPrice) <= 0 {
|
||||
return nil
|
||||
|
|
|
@ -100,6 +100,42 @@ func TestGrid_ExtendLowerPrice(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGrid_NextLowerPin(t *testing.T) {
|
||||
upper := number(500.0)
|
||||
lower := number(100.0)
|
||||
size := number(4.0)
|
||||
grid := NewGrid(lower, upper, size, number(0.01))
|
||||
t.Logf("pins: %+v", grid.Pins)
|
||||
|
||||
next, ok := grid.NextLowerPin(number(200.0))
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, Pin(number(100.0)), next)
|
||||
|
||||
next, ok = grid.NextLowerPin(number(150.0))
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Pin(fixedpoint.Zero), next)
|
||||
}
|
||||
|
||||
func TestGrid_NextHigherPin(t *testing.T) {
|
||||
upper := number(500.0)
|
||||
lower := number(100.0)
|
||||
size := number(4.0)
|
||||
grid := NewGrid(lower, upper, size, number(0.01))
|
||||
t.Logf("pins: %+v", grid.Pins)
|
||||
|
||||
next, ok := grid.NextHigherPin(number(100.0))
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, Pin(number(200.0)), next)
|
||||
|
||||
next, ok = grid.NextHigherPin(number(400.0))
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, Pin(number(500.0)), next)
|
||||
|
||||
next, ok = grid.NextHigherPin(number(500.0))
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Pin(fixedpoint.Zero), next)
|
||||
}
|
||||
|
||||
func Test_calculateArithmeticPins(t *testing.T) {
|
||||
type args struct {
|
||||
lower fixedpoint.Value
|
||||
|
|
Loading…
Reference in New Issue
Block a user