-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathpivotlow.go
76 lines (58 loc) · 1.35 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type PivotLow
type PivotLow struct {
types.SeriesBase
types.IntervalWindow
Lows floats.Slice
Values floats.Slice
EndTime time.Time
updateCallbacks []func(value float64)
}
func (inc *PivotLow) Length() int {
return inc.Values.Length()
}
func (inc *PivotLow) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *PivotLow) Update(value float64) {
if len(inc.Lows) == 0 {
inc.SeriesBase.Series = inc
}
inc.Lows.Push(value)
if len(inc.Lows) < inc.Window {
return
}
if inc.RightWindow == nil {
inc.RightWindow = &inc.Window
}
low, ok := calculatePivotLow(inc.Lows, inc.Window, *inc.RightWindow)
if !ok {
return
}
if low > 0.0 {
inc.Values.Push(low)
}
}
func (inc *PivotLow) PushK(k types.KLine) {
if k.EndTime.Before(inc.EndTime) {
return
}
inc.Update(k.Low.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last(0))
}
func calculatePivotHigh(highs floats.Slice, left, right int) (float64, bool) {
return floats.FindPivot(highs, left, right, func(a, pivot float64) bool {
return a < pivot
})
}
func calculatePivotLow(lows floats.Slice, left, right int) (float64, bool) {
return floats.FindPivot(lows, left, right, func(a, pivot float64) bool {
return a > pivot
})
}