forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
till.go
109 lines (96 loc) · 2.75 KB
/
till.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
package indicator
import (
"github.com/c9s/bbgo/pkg/types"
)
const defaultVolumeFactor = 0.7
// Refer: Tillson T3 Moving Average
// Refer URL: https://tradingpedia.com/forex-trading-indicator/t3-moving-average-indicator/
//go:generate callbackgen -type TILL
type TILL struct {
types.IntervalWindow
VolumeFactor float64
e1 *EWMA
e2 *EWMA
e3 *EWMA
e4 *EWMA
e5 *EWMA
e6 *EWMA
c1 float64
c2 float64
c3 float64
c4 float64
UpdateCallbacks []func(value float64)
}
func (inc *TILL) Update(value float64) {
if inc.e1 == nil || inc.e1.Length() == 0 {
if inc.VolumeFactor == 0 {
inc.VolumeFactor = defaultVolumeFactor
}
inc.e1 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
inc.e2 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
inc.e3 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
inc.e4 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
inc.e5 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
inc.e6 = &EWMA{IntervalWindow: types.IntervalWindow{inc.Interval, inc.Window}}
square := inc.VolumeFactor * inc.VolumeFactor
cube := inc.VolumeFactor * square
inc.c1 = -cube
inc.c2 = 3.*square + 3.*cube
inc.c3 = -6.*square - 3*inc.VolumeFactor - 3*cube
inc.c4 = 1 + 3*inc.VolumeFactor + cube + 3*square
}
inc.e1.Update(value)
inc.e2.Update(inc.e1.Last())
inc.e3.Update(inc.e2.Last())
inc.e4.Update(inc.e3.Last())
inc.e5.Update(inc.e4.Last())
inc.e6.Update(inc.e5.Last())
}
func (inc *TILL) Last() float64 {
if inc.e1.Length() == 0 {
return 0
}
e3 := inc.e3.Last()
e4 := inc.e4.Last()
e5 := inc.e5.Last()
e6 := inc.e6.Last()
return inc.c1*e6 + inc.c2*e5 + inc.c3*e4 + inc.c4*e3
}
func (inc *TILL) Index(i int) float64 {
if inc.e1.Length() <= i {
return 0
}
e3 := inc.e3.Index(i)
e4 := inc.e4.Index(i)
e5 := inc.e5.Index(i)
e6 := inc.e6.Index(i)
return inc.c1*e6 + inc.c2*e5 + inc.c3*e4 + inc.c4*e3
}
func (inc *TILL) Length() int {
return inc.e1.Length()
}
var _ types.Series = &TILL{}
func (inc *TILL) calculateAndUpdate(allKLines []types.KLine) {
doable := false
if inc.e1.Length() == 0 {
doable = true
}
for _, k := range allKLines {
if !doable && k.StartTime.After(inc.e1.LastOpenTime) {
doable = true
}
if doable {
inc.Update(k.Close.Float64())
inc.EmitUpdate(inc.Last())
}
}
}
func (inc *TILL) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.calculateAndUpdate(window)
}
func (inc *TILL) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}