-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
tsi.go
100 lines (88 loc) · 2.05 KB
/
tsi.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
package indicator
import (
"math"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// Refer: True Strength Index
// Refer URL: https://www.investopedia.com/terms/t/tsi.asp
//
//go:generate callbackgen -type TSI
type TSI struct {
types.SeriesBase
types.Interval
FastWindow int
SlowWindow int
PrevValue float64
Values floats.Slice
Pcs *EWMA
Pcds *EWMA
Apcs *EWMA
Apcds *EWMA
updateCallbacks []func(value float64)
}
func (inc *TSI) Update(value float64) {
if inc.Pcs == nil {
if inc.FastWindow == 0 {
inc.FastWindow = 13
}
if inc.SlowWindow == 0 {
inc.SlowWindow = 25
}
inc.Pcs = &EWMA{
IntervalWindow: types.IntervalWindow{
Window: inc.SlowWindow,
Interval: inc.Interval,
},
}
inc.Pcds = &EWMA{
IntervalWindow: types.IntervalWindow{
Window: inc.FastWindow,
Interval: inc.Interval,
},
}
inc.Apcs = &EWMA{
IntervalWindow: types.IntervalWindow{
Window: inc.SlowWindow,
Interval: inc.Interval,
},
}
inc.Apcds = &EWMA{
IntervalWindow: types.IntervalWindow{
Window: inc.FastWindow,
Interval: inc.Interval,
},
}
inc.SeriesBase.Series = inc
inc.PrevValue = value
return
}
pc := value - inc.PrevValue
inc.PrevValue = value
inc.Pcs.Update(pc)
apc := math.Abs(pc)
inc.Apcs.Update(apc)
inc.Pcds.Update(inc.Pcs.Last(0))
inc.Apcds.Update(inc.Apcs.Last(0))
tsi := (inc.Pcds.Last(0) / inc.Apcds.Last(0)) * 100.
inc.Values.Push(tsi)
if inc.Values.Length() > MaxNumOfEWMA {
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
}
}
func (inc *TSI) Length() int {
return inc.Values.Length()
}
func (inc *TSI) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *TSI) Index(i int) float64 {
return inc.Last(i)
}
func (inc *TSI) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
var _ types.SeriesExtend = &TSI{}
func (inc *TSI) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
}