-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
408 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,58 @@ | ||
// 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" | ||
|
||
const ( | ||
// DefaultRmaPeriod is the default RMA period. | ||
DefaultRmaPeriod = 20 | ||
) | ||
|
||
// Rma represents the parameters for calculating Rolling Moving Average (RMA). | ||
// | ||
// R[0] to R[p-1] is SMA(values) | ||
// R[p] and after is R[i] = ((R[i-1]*(p-1)) + v[i]) / p | ||
// | ||
// Example: | ||
// | ||
// rma := trend.NewRma[float64]() | ||
// rma.Period = 10 | ||
// | ||
// result := rma.Compute(c) | ||
type Rma[T helper.Number] struct { | ||
// Time period. | ||
Period int | ||
} | ||
|
||
// NewRma function initializes a new RMA instance with the default parameters. | ||
func NewRma[T helper.Number]() *Rma[T] { | ||
return &Rma[T]{ | ||
Period: DefaultRmaPeriod, | ||
} | ||
} | ||
|
||
// Compute function takes a channel of numbers and computes the RMA over the specified period. | ||
func (rma *Rma[T]) Compute(c <-chan T) <-chan T { | ||
result := make(chan T, cap(c)) | ||
|
||
go func() { | ||
defer close(result) | ||
|
||
// Initial RMA value is the SMA. | ||
sma := NewSma[T]() | ||
sma.Period = rma.Period | ||
|
||
before := <-sma.Compute(helper.Head(c, rma.Period)) | ||
result <- before | ||
|
||
for n := range c { | ||
before = ((before * T(rma.Period-1)) + n) / T(rma.Period) | ||
result <- before | ||
} | ||
}() | ||
|
||
return result | ||
} |
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,41 @@ | ||
// 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 TestRma(t *testing.T) { | ||
type Data struct { | ||
Close float64 | ||
Rma float64 | ||
} | ||
|
||
input, err := helper.ReadFromCsvFile[Data]("testdata/rma.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.Rma }) | ||
|
||
rma := trend.NewRma[float64]() | ||
rma.Period = 15 | ||
|
||
actual := rma.Compute(closing) | ||
actual = helper.RoundDigits(actual, 2) | ||
|
||
expected = helper.Skip(expected, rma.Period-1) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.