2022-04-26 08:32:31 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: Triangular Moving Average
|
|
|
|
// Refer URL: https://ja.wikipedia.org/wiki/移動平均
|
2023-05-31 11:35:44 +00:00
|
|
|
//
|
2022-04-26 08:32:31 +00:00
|
|
|
//go:generate callbackgen -type TMA
|
|
|
|
type TMA struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-04-26 08:32:31 +00:00
|
|
|
types.IntervalWindow
|
|
|
|
s1 *SMA
|
|
|
|
s2 *SMA
|
|
|
|
UpdateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TMA) Update(value float64) {
|
|
|
|
if inc.s1 == nil {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2022-04-26 08:32:31 +00:00
|
|
|
w := (inc.Window + 1) / 2
|
2022-08-24 10:17:37 +00:00
|
|
|
inc.s1 = &SMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: w}}
|
|
|
|
inc.s2 = &SMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: w}}
|
2022-04-26 08:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inc.s1.Update(value)
|
2023-05-31 11:35:44 +00:00
|
|
|
inc.s2.Update(inc.s1.Last(0))
|
2022-04-26 08:32:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (inc *TMA) Last(i int) float64 {
|
|
|
|
return inc.s2.Last(i)
|
2022-04-26 08:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TMA) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return inc.Last(i)
|
2022-04-26 08:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *TMA) Length() int {
|
|
|
|
if inc.s2 == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.s2.Length()
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &TMA{}
|
2022-04-26 08:32:31 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *TMA) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
}
|