-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathtrix.go
61 lines (51 loc) · 1.55 KB
/
trix.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
// 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"
)
const (
// DefaultTrixPeriod is the default time period for TRIX.
DefaultTrixPeriod = 15
)
// Trix represents the configuration parameters for calculating the Triple Exponential Average (TRIX).
// TRIX indicator is an oscillator used to identify oversold and overbought markets, and it can also
// be used as a momentum indicator. Like many oscillators, TRIX oscillates around a zero line.
//
// EMA1 = EMA(period, values)
// EMA2 = EMA(period, EMA1)
// EMA3 = EMA(period, EMA2)
// TRIX = (EMA3 - Previous EMA3) / Previous EMA3
//
// Example:
//
// trix := trend.NewTrix[float64]()
// result := trix.Compute(values)
type Trix[T helper.Number] struct {
// Time period.
Period int
}
// NewTrix function initializes a new TRIX instance with the default parameters.
func NewTrix[T helper.Number]() *Trix[T] {
return &Trix[T]{
Period: DefaultTrixPeriod,
}
}
// Compute function takes a channel of numbers and computes the TRIX and the signal line.
func (t *Trix[T]) Compute(c <-chan T) <-chan T {
ema1 := NewEmaWithPeriod[T](t.Period)
ema2 := NewEmaWithPeriod[T](t.Period)
ema3 := NewEmaWithPeriod[T](t.Period)
emas := ema3.Compute(
ema2.Compute(
ema1.Compute(c),
),
)
trix := helper.ChangeRatio[T](emas, 1)
return trix
}
// IdlePeriod is the initial period that TRIX won't yield any results.
func (t *Trix[T]) IdlePeriod() int {
return (t.Period * 3) - 3 + 1
}