-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathtema.go
67 lines (56 loc) · 1.43 KB
/
tema.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
// Copyright (c) 2021-2024 Onur Cinar.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator
package trend
import (
"github.com/cinar/indicator/v2/helper"
)
// Tema represents the configuration parameters for calculating the
// Triple Exponential Moving Average (TEMA).
//
// TEMA = (3 * EMA1) - (3 * EMA2) + EMA3
// EMA1 = EMA(values)
// EMA2 = EMA(EMA1)
// EMA3 = EMA(EMA2)
type Tema[T helper.Number] struct {
Ema1 *Ema[T]
Ema2 *Ema[T]
Ema3 *Ema[T]
}
// NewTema function initializes a new TEMA instance
// with the default parameters.
func NewTema[T helper.Number]() *Tema[T] {
return &Tema[T]{
Ema1: NewEma[T](),
Ema2: NewEma[T](),
Ema3: NewEma[T](),
}
}
// Compute function takes a channel of numbers and computes the TEMA
// and the signal line.
func (t *Tema[T]) Compute(c <-chan T) <-chan T {
ema1 := helper.Duplicate(
t.Ema1.Compute(c),
2,
)
ema2 := helper.Duplicate(
t.Ema2.Compute(ema1[0]),
2,
)
ema1[1] = helper.Skip(ema1[1], t.Ema2.Period-1)
ema3 := t.Ema3.Compute(ema2[0])
ema1[1] = helper.Skip(ema1[1], t.Ema3.Period-1)
ema2[1] = helper.Skip(ema2[1], t.Ema3.Period-1)
tema := helper.Add(
helper.Subtract(
helper.MultiplyBy(ema1[1], 3),
helper.MultiplyBy(ema2[1], 3),
),
ema3,
)
return tema
}
// IdlePeriod is the initial period that TEMA won't yield any results.
func (t *Tema[T]) IdlePeriod() int {
return t.Ema1.Period + t.Ema2.Period + t.Ema3.Period - 3
}