forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivotlow.go
39 lines (33 loc) · 883 Bytes
/
pivotlow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package indicatorv2
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
type PivotLowStream struct {
*types.Float64Series
rawValues floats.Slice
window, rightWindow int
}
func PivotLow(source types.Float64Source, window int, args ...int) *PivotLowStream {
rightWindow := window
if len(args) > 0 {
rightWindow = args[0]
}
s := &PivotLowStream{
Float64Series: types.NewFloat64Series(),
window: window,
rightWindow: rightWindow,
}
s.Subscribe(source, func(x float64) {
s.rawValues.Push(x)
if low, ok := s.calculatePivotLow(s.rawValues, s.window, s.rightWindow); ok {
s.PushAndEmit(low)
}
})
return s
}
func (s *PivotLowStream) calculatePivotLow(lows floats.Slice, left, right int) (float64, bool) {
return floats.FindPivot(lows, left, right, func(a, pivot float64) bool {
return a > pivot
})
}