2022-12-09 08:52:42 +00:00
|
|
|
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
|
2023-05-31 11:35:44 +00:00
|
|
|
//
|
2022-12-09 08:52:42 +00:00
|
|
|
//go:generate callbackgen -type TSI
|
|
|
|
type TSI struct {
|
|
|
|
types.SeriesBase
|
2022-12-19 10:27:07 +00:00
|
|
|
types.Interval
|
|
|
|
FastWindow int
|
|
|
|
SlowWindow int
|
2022-12-09 08:52:42 +00:00
|
|
|
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 {
|
2022-12-19 10:27:07 +00:00
|
|
|
if inc.FastWindow == 0 {
|
|
|
|
inc.FastWindow = 13
|
|
|
|
}
|
|
|
|
if inc.SlowWindow == 0 {
|
|
|
|
inc.SlowWindow = 25
|
|
|
|
}
|
2022-12-09 08:52:42 +00:00
|
|
|
inc.Pcs = &EWMA{
|
|
|
|
IntervalWindow: types.IntervalWindow{
|
2022-12-19 10:27:07 +00:00
|
|
|
Window: inc.SlowWindow,
|
2022-12-09 08:52:42 +00:00
|
|
|
Interval: inc.Interval,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inc.Pcds = &EWMA{
|
|
|
|
IntervalWindow: types.IntervalWindow{
|
2022-12-19 10:27:07 +00:00
|
|
|
Window: inc.FastWindow,
|
2022-12-09 08:52:42 +00:00
|
|
|
Interval: inc.Interval,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inc.Apcs = &EWMA{
|
|
|
|
IntervalWindow: types.IntervalWindow{
|
2022-12-19 10:27:07 +00:00
|
|
|
Window: inc.SlowWindow,
|
2022-12-09 08:52:42 +00:00
|
|
|
Interval: inc.Interval,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inc.Apcds = &EWMA{
|
|
|
|
IntervalWindow: types.IntervalWindow{
|
2022-12-19 10:27:07 +00:00
|
|
|
Window: inc.FastWindow,
|
2022-12-09 08:52:42 +00:00
|
|
|
Interval: inc.Interval,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inc.SeriesBase.Series = inc
|
|
|
|
inc.PrevValue = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pc := value - inc.PrevValue
|
2022-12-19 10:27:07 +00:00
|
|
|
inc.PrevValue = value
|
2022-12-09 08:52:42 +00:00
|
|
|
inc.Pcs.Update(pc)
|
|
|
|
apc := math.Abs(pc)
|
|
|
|
inc.Apcs.Update(apc)
|
2022-12-19 10:27:07 +00:00
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.Pcds.Update(inc.Pcs.Last(0))
|
|
|
|
inc.Apcds.Update(inc.Apcs.Last(0))
|
2022-12-19 10:27:07 +00:00
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
tsi := (inc.Pcds.Last(0) / inc.Apcds.Last(0)) * 100.
|
2022-12-09 08:52:42 +00:00
|
|
|
inc.Values.Push(tsi)
|
|
|
|
if inc.Values.Length() > MaxNumOfEWMA {
|
|
|
|
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
|
|
|
|
}
|
2022-12-19 10:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TSI) Length() int {
|
|
|
|
return inc.Values.Length()
|
2022-12-09 08:52:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (inc *TSI) Last(i int) float64 {
|
|
|
|
return inc.Values.Last(i)
|
2022-12-09 08:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TSI) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return inc.Last(i)
|
2022-12-09 08:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TSI) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.SeriesExtend = &TSI{}
|
|
|
|
|
2023-01-18 10:11:23 +00:00
|
|
|
func (inc *TSI) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
|
|
|
|
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
|
2022-12-09 08:52:42 +00:00
|
|
|
}
|