2016-07-16 16:10:44 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-10-27 22:52:38 -04:00
|
|
|
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
2016-07-16 16:10:44 -04:00
|
|
|
)
|
|
|
|
|
2016-07-17 21:54:50 -04:00
|
|
|
var (
|
2019-02-13 21:55:13 -05:00
|
|
|
emaXValues = LinearRange(1.0, 50.0)
|
2016-07-17 21:54:50 -04:00
|
|
|
emaYValues = []float64{
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2, 3, 4, 5, 4, 3, 2,
|
|
|
|
1, 2,
|
|
|
|
}
|
|
|
|
emaExpected = []float64{
|
|
|
|
1,
|
|
|
|
1.074074074,
|
|
|
|
1.216735254,
|
|
|
|
1.422903013,
|
|
|
|
1.68787316,
|
|
|
|
1.859141815,
|
|
|
|
1.943649828,
|
|
|
|
1.947823915,
|
|
|
|
1.877614736,
|
|
|
|
1.886680311,
|
|
|
|
1.969148437,
|
|
|
|
2.119581886,
|
|
|
|
2.33294619,
|
|
|
|
2.456431658,
|
|
|
|
2.496695979,
|
|
|
|
2.459903685,
|
|
|
|
2.351762671,
|
|
|
|
2.325706177,
|
|
|
|
2.375653867,
|
|
|
|
2.495975803,
|
|
|
|
2.681459077,
|
|
|
|
2.779128775,
|
|
|
|
2.795489607,
|
|
|
|
2.73656445,
|
|
|
|
2.607930047,
|
|
|
|
2.562898191,
|
|
|
|
2.595276103,
|
|
|
|
2.699329725,
|
|
|
|
2.869749746,
|
|
|
|
2.953471987,
|
|
|
|
2.956918506,
|
|
|
|
2.886035654,
|
|
|
|
2.746329309,
|
|
|
|
2.691045657,
|
|
|
|
2.713931163,
|
|
|
|
2.809195522,
|
|
|
|
2.971477335,
|
|
|
|
3.047664199,
|
|
|
|
3.044133518,
|
|
|
|
2.966790294,
|
|
|
|
2.821102124,
|
|
|
|
2.760279745,
|
|
|
|
2.778036801,
|
|
|
|
2.868552593,
|
|
|
|
3.026437586,
|
|
|
|
3.098553321,
|
|
|
|
3.091253075,
|
|
|
|
3.010419514,
|
|
|
|
2.86149955,
|
|
|
|
2.797684768,
|
|
|
|
}
|
|
|
|
emaDelta = 0.0001
|
|
|
|
)
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
func TestEMASeries(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-16 16:10:44 -04:00
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
mockSeries := mockValuesProvider{
|
2016-07-17 21:54:50 -04:00
|
|
|
emaXValues,
|
|
|
|
emaYValues,
|
2016-07-16 16:10:44 -04:00
|
|
|
}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 50, mockSeries.Len())
|
2016-07-16 16:10:44 -04:00
|
|
|
|
2016-07-17 21:54:50 -04:00
|
|
|
ema := &EMASeries{
|
2016-07-16 16:10:44 -04:00
|
|
|
InnerSeries: mockSeries,
|
2016-07-17 21:54:50 -04:00
|
|
|
Period: 26,
|
2016-07-16 16:10:44 -04:00
|
|
|
}
|
|
|
|
|
2016-07-17 21:54:50 -04:00
|
|
|
sig := ema.GetSigma()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 2.0/(26.0+1), sig)
|
2016-07-17 21:54:50 -04:00
|
|
|
|
2016-07-16 16:10:44 -04:00
|
|
|
var yvalues []float64
|
2016-07-17 21:54:50 -04:00
|
|
|
for x := 0; x < ema.Len(); x++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, y := ema.GetValues(x)
|
2016-07-16 16:10:44 -04:00
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:54:50 -04:00
|
|
|
for index, yv := range yvalues {
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertInDelta(t, yv, emaExpected[index], emaDelta)
|
2016-07-17 21:54:50 -04:00
|
|
|
}
|
2016-07-16 16:25:21 -04:00
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
lvx, lvy := ema.GetLastValues()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 50.0, lvx)
|
|
|
|
testutil.AssertInDelta(t, lvy, emaExpected[49], emaDelta)
|
2016-07-16 16:10:44 -04:00
|
|
|
}
|