-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
411 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend | ||
|
||
import ( | ||
"github.com/cinar/indicator/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/helper" | ||
"github.com/cinar/indicator/trend" | ||
) | ||
|
||
func TestTema(t *testing.T) { | ||
type Data struct { | ||
Close float64 | ||
Tema float64 | ||
} | ||
|
||
input, err := helper.ReadFromCsvFile[Data]("testdata/tema.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inputs := helper.Duplicate(input, 2) | ||
closing := helper.Map(inputs[0], func(d *Data) float64 { return d.Close }) | ||
expected := helper.Map(inputs[1], func(d *Data) float64 { return d.Tema }) | ||
|
||
tema := trend.NewTema[float64]() | ||
|
||
actual := tema.Compute(closing) | ||
actual = helper.RoundDigits(actual, 2) | ||
|
||
expected = helper.Skip(expected, tema.IdlePeriod()) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.