forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_average_test.go
87 lines (68 loc) · 2.89 KB
/
indicator_average_test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package techan
import (
"testing"
)
func TestAverageGainsIndicator(t *testing.T) {
t.Run("Basic", func(t *testing.T) {
ts := mockTimeSeriesFl(1, 2, 3, 5, 8, 13)
avgGains := NewAverageGainsIndicator(NewClosePriceIndicator(ts), 6)
decimalEquals(t, 0, avgGains.Calculate(0))
decimalEquals(t, 1.0/2.0, avgGains.Calculate(1))
decimalEquals(t, 2.0/3.0, avgGains.Calculate(2))
decimalEquals(t, 1.0, avgGains.Calculate(3))
decimalEquals(t, 7.0/5.0, avgGains.Calculate(4))
decimalEquals(t, 12.0/6.0, avgGains.Calculate(5))
})
t.Run("Oscillating indicator", func(t *testing.T) {
ts := mockTimeSeriesFl(0, 5, 2, 10, 12, 11)
cumGains := NewAverageGainsIndicator(NewClosePriceIndicator(ts), 6)
decimalEquals(t, 0, cumGains.Calculate(0))
decimalEquals(t, 5/2.0, cumGains.Calculate(1))
decimalEquals(t, 5/3.0, cumGains.Calculate(2))
decimalEquals(t, 13.0/4.0, cumGains.Calculate(3))
decimalEquals(t, 15.0/5.0, cumGains.Calculate(4))
decimalEquals(t, 15.0/6.0, cumGains.Calculate(5))
})
t.Run("Rolling window", func(t *testing.T) {
ts := mockTimeSeriesFl(0, 5, 2, 10, 12, 11)
cumGains := NewAverageGainsIndicator(NewClosePriceIndicator(ts), 3)
decimalEquals(t, 0, cumGains.Calculate(0))
decimalEquals(t, 5.0/2.0, cumGains.Calculate(1))
decimalEquals(t, 5.0/3.0, cumGains.Calculate(2))
decimalEquals(t, 13.0/3.0, cumGains.Calculate(3))
decimalEquals(t, 10.0/3.0, cumGains.Calculate(4))
decimalEquals(t, 10.0/3.0, cumGains.Calculate(5))
})
}
func TestNewAverageLossesIndicator(t *testing.T) {
t.Run("Basic", func(t *testing.T) {
ts := mockTimeSeriesFl(13, 8, 5, 3, 2, 1)
cumLosses := NewAverageLossesIndicator(NewClosePriceIndicator(ts), 6)
decimalEquals(t, 0, cumLosses.Calculate(0))
decimalEquals(t, 5.0/2.0, cumLosses.Calculate(1))
decimalEquals(t, 8.0/3.0, cumLosses.Calculate(2))
decimalEquals(t, 10.0/4.0, cumLosses.Calculate(3))
decimalEquals(t, 11.0/5.0, cumLosses.Calculate(4))
decimalEquals(t, 12.0/6.0, cumLosses.Calculate(5))
})
t.Run("Oscillating indicator", func(t *testing.T) {
ts := mockTimeSeriesFl(13, 16, 10, 8, 9, 8)
cumLosses := NewAverageLossesIndicator(NewClosePriceIndicator(ts), 6)
decimalEquals(t, 0, cumLosses.Calculate(0))
decimalEquals(t, 0, cumLosses.Calculate(1))
decimalEquals(t, 6.0/3.0, cumLosses.Calculate(2))
decimalEquals(t, 8.0/4.0, cumLosses.Calculate(3))
decimalEquals(t, 8.0/5.0, cumLosses.Calculate(4))
decimalEquals(t, 9.0/6.0, cumLosses.Calculate(5))
})
t.Run("Rolling window", func(t *testing.T) {
ts := mockTimeSeriesFl(13, 16, 10, 8, 9, 8)
cumLosses := NewAverageLossesIndicator(NewClosePriceIndicator(ts), 3)
decimalEquals(t, 0, cumLosses.Calculate(0))
decimalEquals(t, 0, cumLosses.Calculate(1))
decimalEquals(t, 6.0/3.0, cumLosses.Calculate(2))
decimalEquals(t, 8.0/3.0, cumLosses.Calculate(3))
decimalEquals(t, 8.0/3.0, cumLosses.Calculate(4))
decimalEquals(t, 1.0, cumLosses.Calculate(5))
})
}