forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssf.go
117 lines (103 loc) · 2.84 KB
/
ssf.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package indicator
import (
"math"
"github.com/c9s/bbgo/pkg/types"
)
// Refer: https://easylanguagemastery.com/indicators/predictive-indicators/
// Refer: https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/ssf.py
// Ehler's Super Smoother Filter
//
// John F. Ehlers's solution to reduce lag and remove aliasing noise with his
// research in aerospace analog filter design. This indicator comes with two
// versions determined by the keyword poles. By default, it uses two poles but
// there is an option for three poles. Since SSF is a (Resursive) Digital Filter,
// the number of poles determine how many prior recursive SSF bars to include in
// the design of the filter. So two poles uses two prior SSF bars and three poles
// uses three prior SSF bars for their filter calculations.
//
//go:generate callbackgen -type SSF
type SSF struct {
types.SeriesBase
types.IntervalWindow
Poles int
c1 float64
c2 float64
c3 float64
c4 float64
Values types.Float64Slice
UpdateCallbacks []func(value float64)
}
func (inc *SSF) Update(value float64) {
if inc.Poles == 3 {
if inc.Values == nil {
inc.SeriesBase.Series = inc
x := math.Pi / float64(inc.Window)
a0 := math.Exp(-x)
b0 := 2. * a0 * math.Cos(math.Sqrt(3.)*x)
c0 := a0 * a0
inc.c4 = c0 * c0
inc.c3 = -c0 * (1. + b0)
inc.c2 = c0 + b0
inc.c1 = 1. - inc.c2 - inc.c3 - inc.c4
inc.Values = types.Float64Slice{}
}
result := inc.c1*value +
inc.c2*inc.Values.Index(0) +
inc.c3*inc.Values.Index(1) +
inc.c4*inc.Values.Index(2)
inc.Values.Push(result)
} else { // poles == 2
if inc.Values == nil {
inc.SeriesBase.Series = inc
x := math.Pi * math.Sqrt(2.) / float64(inc.Window)
a0 := math.Exp(-x)
inc.c3 = -a0 * a0
inc.c2 = 2. * a0 * math.Cos(x)
inc.c1 = 1. - inc.c2 - inc.c3
inc.Values = types.Float64Slice{}
}
result := inc.c1*value +
inc.c2*inc.Values.Index(0) +
inc.c3*inc.Values.Index(1)
inc.Values.Push(result)
}
}
func (inc *SSF) Index(i int) float64 {
if inc.Values == nil {
return 0.0
}
return inc.Values.Index(i)
}
func (inc *SSF) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}
func (inc *SSF) Last() float64 {
if inc.Values == nil {
return 0.0
}
return inc.Values.Last()
}
var _ types.SeriesExtend = &SSF{}
func (inc *SSF) calculateAndUpdate(allKLines []types.KLine) {
if inc.Values != nil {
inc.Update(allKLines[len(allKLines)-1].Close.Float64())
inc.EmitUpdate(inc.Last())
return
}
for _, k := range allKLines {
inc.Update(k.Close.Float64())
inc.EmitUpdate(inc.Last())
}
}
func (inc *SSF) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.calculateAndUpdate(window)
}
func (inc *SSF) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}