make rightWindow possible to be set as zero

This commit is contained in:
c9s 2023-10-16 12:36:52 +08:00
parent d617bf8ae5
commit 4c69dccf09
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
7 changed files with 56 additions and 15 deletions

View File

@ -7,10 +7,6 @@ func (s Slice) Pivot(left, right int, f func(a, pivot float64) bool) (float64, b
func FindPivot(values Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) { func FindPivot(values Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) {
length := len(values) length := len(values)
if right == 0 {
right = left
}
if length == 0 || length < left+right+1 { if length == 0 || length < left+right+1 {
return 0.0, false return 0.0, false
} }

View File

@ -0,0 +1,29 @@
package floats
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFindPivot(t *testing.T) {
t.Run("middle", func(t *testing.T) {
pv, ok := FindPivot(Slice{10, 20, 30, 40, 30, 20}, 2, 2, func(a, pivot float64) bool {
return a < pivot
})
if assert.True(t, ok) {
assert.Equal(t, 40., pv)
}
})
t.Run("last", func(t *testing.T) {
pv, ok := FindPivot(Slice{10, 20, 30, 40, 30, 45}, 2, 0, func(a, pivot float64) bool {
return a < pivot
})
if assert.True(t, ok) {
assert.Equal(t, 45., pv)
}
})
}

View File

@ -39,7 +39,11 @@ func (inc *PivotHigh) Update(value float64) {
return return
} }
high, ok := calculatePivotHigh(inc.Highs, inc.Window, inc.RightWindow) if inc.RightWindow == nil {
inc.RightWindow = &inc.Window
}
high, ok := calculatePivotHigh(inc.Highs, inc.Window, *inc.RightWindow)
if !ok { if !ok {
return return
} }

View File

@ -39,7 +39,11 @@ func (inc *PivotLow) Update(value float64) {
return return
} }
low, ok := calculatePivotLow(inc.Lows, inc.Window, inc.RightWindow) if inc.RightWindow == nil {
inc.RightWindow = &inc.Window
}
low, ok := calculatePivotLow(inc.Lows, inc.Window, *inc.RightWindow)
if !ok { if !ok {
return return
} }

View File

@ -36,8 +36,8 @@ func Test_calculatePivotLow(t *testing.T) {
assert.Equal(t, 0.0, low) assert.Equal(t, 0.0, low)
}) })
t.Run("right window 0", func(t *testing.T) { t.Run("right window same", func(t *testing.T) {
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 0) low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 2)
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, 10.0, low) assert.Equal(t, 10.0, low)
}) })

View File

@ -14,7 +14,7 @@ type TrendLine struct {
Market types.Market `json:"-"` Market types.Market `json:"-"`
types.IntervalWindow types.IntervalWindow
PivotRightWindow fixedpoint.Value `json:"pivotRightWindow"` PivotRightWindow int `json:"pivotRightWindow"`
// MarketOrder is the option to enable market order short. // MarketOrder is the option to enable market order short.
MarketOrder bool `json:"marketOrder"` MarketOrder bool `json:"marketOrder"`
@ -47,8 +47,14 @@ func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gene
position := orderExecutor.Position() position := orderExecutor.Position()
symbol := position.Symbol symbol := position.Symbol
standardIndicator := session.StandardIndicatorSet(s.Symbol) standardIndicator := session.StandardIndicatorSet(s.Symbol)
s.pivotHigh = standardIndicator.PivotHigh(types.IntervalWindow{s.Interval, int(3. * s.PivotRightWindow.Float64()), int(s.PivotRightWindow.Float64())})
s.pivotLow = standardIndicator.PivotLow(types.IntervalWindow{s.Interval, int(3. * s.PivotRightWindow.Float64()), int(s.PivotRightWindow.Float64())}) s.pivotHigh = standardIndicator.PivotHigh(types.IntervalWindow{
Interval: s.Interval,
Window: int(3. * s.PivotRightWindow), RightWindow: &s.PivotRightWindow})
s.pivotLow = standardIndicator.PivotLow(types.IntervalWindow{
Interval: s.Interval,
Window: int(3. * s.PivotRightWindow), RightWindow: &s.PivotRightWindow})
resistancePrices := types.NewQueue(3) resistancePrices := types.NewQueue(3)
pivotHighDurationCounter := 0. pivotHighDurationCounter := 0.
@ -124,7 +130,9 @@ func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gene
} }
} }
func (s *TrendLine) placeOrder(ctx context.Context, side types.SideType, quantity fixedpoint.Value, symbol string) error { func (s *TrendLine) placeOrder(
ctx context.Context, side types.SideType, quantity fixedpoint.Value, symbol string,
) error {
market, _ := s.session.Market(symbol) market, _ := s.session.Market(symbol)
_, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{ _, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: symbol, Symbol: symbol,

View File

@ -179,7 +179,7 @@ type IntervalWindow struct {
Window int `json:"window"` Window int `json:"window"`
// RightWindow is used by the pivot indicator // RightWindow is used by the pivot indicator
RightWindow int `json:"rightWindow"` RightWindow *int `json:"rightWindow"`
} }
type IntervalWindowBandWidth struct { type IntervalWindowBandWidth struct {