forked from kylejusticemagnuson/pyti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_true_range.py
157 lines (147 loc) · 9.16 KB
/
test_true_range.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import absolute_import
import unittest
import numpy as np
from tests.sample_data import SampleData
from pyti import true_range
class TestTrueRange(unittest.TestCase):
def setUp(self):
"""Create data to use for testing."""
self.close_data = SampleData().get_sample_close_data()
self.tr_period_6_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
17.479999999999905, 9.32000000000005, 10.629999999999995,
7.4000000000000909, 7.4000000000000909, 5.9700000000000273,
8.0499999999999545, 8.0499999999999545, 9.9900000000000091,
9.9900000000000091, 22.720000000000027, 26.419999999999959,
39.160000000000082, 33.270000000000095, 44.990000000000009,
30.839999999999918, 27.139999999999986, 25.110000000000014,
25.110000000000014, 25.110000000000014, 25.110000000000014,
13.560000000000059, 10.440000000000055, 9.8199999999999363,
10.189999999999941, 10.189999999999941, 11.0, 32.939999999999941,
32.939999999999941, 32.939999999999941, 52.370000000000005,
58.759999999999991, 58.759999999999991, 40.230000000000018,
31.690000000000055, 30.879999999999995, 30.879999999999995,
28.799999999999955, 38.459999999999923, 37.369999999999891,
33.680000000000064, 23.069999999999936, 25.840000000000032,
18.389999999999986, 18.389999999999986, 14.110000000000014, 14.25,
28.899999999999977, 22.490000000000009, 23.009999999999991,
23.009999999999991, 17.409999999999968, 10.089999999999918,
13.459999999999923, 13.459999999999923, 13.459999999999923,
13.459999999999923, 13.789999999999964, 3.67999999999995,
3.67999999999995, 9.67999999999995, 10.350000000000023,
10.350000000000023, 12.32000000000005, 13.310000000000059,
13.300000000000068, 16.170000000000073, 20.560000000000059,
20.560000000000059, 20.560000000000059, 14.569999999999936,
12.509999999999991, 12.509999999999991, 12.75, 14.360000000000014,
19.509999999999991, 19.539999999999964, 19.539999999999964,
19.539999999999964, 18.169999999999959, 18.169999999999959,
18.169999999999959, 7.0199999999999818, 5.9700000000000273,
5.9700000000000273, 5.2900000000000773, 5.6500000000000909,
8.3500000000000227, 11.450000000000045, 11.819999999999936,
8.8299999999999272, 9.0099999999999909, 7.3999999999999773,
7.0099999999999909, 7.2999999999999545, 7.2999999999999545,
3.2599999999999909, 3.2599999999999909, 11.240000000000009,
11.240000000000009, 10.230000000000018, 9.67999999999995,
15.589999999999918, 35.100000000000023, 38.970000000000027,
43.290000000000077, 43.420000000000073, 33.82000000000005,
11.430000000000064, 7.5600000000000591, 5.8700000000000045,
6.0799999999999272, 23.649999999999977, 21.450000000000045,
27.600000000000023, 23.92999999999995, 26.0, 18.019999999999982,
28.539999999999964, 25.25, 27.620000000000005, 22.310000000000059]
self.tr_period_8_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
np.nan, np.nan, 22.75, 12.32000000000005, 10.629999999999995,
7.8500000000000227, 10.090000000000032, 8.2100000000000364,
9.9900000000000091, 9.9900000000000091, 22.720000000000027,
26.419999999999959, 41.710000000000036, 41.710000000000036,
50.879999999999995, 45.120000000000005, 45.120000000000005,
30.839999999999918, 27.139999999999986, 25.110000000000014,
25.110000000000014, 25.110000000000014, 25.110000000000014,
13.560000000000059, 13.470000000000027, 10.189999999999941, 11.0,
32.939999999999941, 32.939999999999941, 32.939999999999941,
52.370000000000005, 58.759999999999991, 58.759999999999991,
58.759999999999991, 58.759999999999991, 40.230000000000018,
31.690000000000055, 30.879999999999995, 38.459999999999923,
38.459999999999923, 41.0, 46.3599999999999, 47.32000000000005,
30.259999999999991, 25.840000000000032, 18.389999999999986,
18.389999999999986, 28.899999999999977, 31.139999999999986,
31.659999999999968, 23.009999999999991, 23.009999999999991,
23.009999999999991, 20.779999999999973, 13.459999999999923,
13.459999999999923, 13.459999999999923, 13.789999999999964,
13.789999999999964, 13.789999999999964, 9.67999999999995,
10.350000000000023, 10.350000000000023, 14.580000000000041,
15.57000000000005, 13.310000000000059, 16.170000000000073,
20.560000000000059, 20.560000000000059, 20.560000000000059,
25.490000000000009, 27.230000000000018, 16.309999999999945, 12.75,
14.360000000000014, 19.509999999999991, 19.539999999999964,
19.539999999999964, 19.539999999999964, 19.539999999999964,
19.539999999999964, 18.169999999999959, 18.169999999999959,
18.169999999999959, 7.0199999999999818, 6.7400000000000091,
7.1000000000000227, 8.3500000000000227, 11.450000000000045,
14.120000000000005, 14.120000000000005, 12.360000000000014,
10.459999999999923, 11.25, 8.8400000000000318, 7.2999999999999545,
7.2999999999999545, 7.2999999999999545, 11.240000000000009,
11.240000000000009, 11.240000000000009, 11.240000000000009,
16.139999999999986, 41.089999999999918, 44.959999999999923,
43.290000000000077, 43.420000000000073, 43.420000000000073,
46.530000000000086, 36.930000000000064, 12.430000000000064,
8.7699999999999818, 23.649999999999977, 23.649999999999977,
29.799999999999955, 27.600000000000023, 29.879999999999995,
35.629999999999995, 45.940000000000055, 28.539999999999964,
30.909999999999968, 27.620000000000005]
self.tr_period_10_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
np.nan, np.nan, np.nan, np.nan, 22.75, 12.769999999999982,
13.319999999999936, 10.090000000000032, 10.090000000000032,
9.9900000000000091, 22.720000000000027, 26.419999999999959,
41.710000000000036, 41.710000000000036, 53.42999999999995,
53.559999999999945, 51.009999999999991, 45.120000000000005,
45.120000000000005, 30.839999999999918, 27.139999999999986,
25.110000000000014, 25.110000000000014, 25.110000000000014,
25.110000000000014, 13.560000000000059, 14.280000000000086,
32.939999999999941, 32.939999999999941, 32.939999999999941,
52.370000000000005, 58.759999999999991, 58.759999999999991,
58.759999999999991, 58.759999999999991, 58.759999999999991,
58.759999999999991, 40.230000000000018, 39.269999999999982,
38.459999999999923, 41.0, 47.449999999999932, 54.639999999999986,
53.549999999999955, 47.32000000000005, 30.259999999999991,
25.840000000000032, 28.899999999999977, 31.139999999999986,
31.659999999999968, 31.659999999999968, 31.659999999999968,
23.009999999999991, 26.379999999999995, 26.379999999999995,
20.779999999999973, 13.459999999999923, 13.789999999999964,
13.789999999999964, 13.789999999999964, 13.789999999999964,
13.789999999999964, 10.350000000000023, 14.580000000000041,
15.57000000000005, 15.57000000000005, 16.170000000000073,
20.560000000000059, 20.560000000000059, 20.560000000000059,
25.490000000000009, 27.230000000000018, 27.230000000000018,
27.470000000000027, 16.549999999999955, 19.509999999999991,
19.539999999999964, 19.539999999999964, 19.539999999999964,
19.539999999999964, 19.539999999999964, 19.539999999999964,
19.539999999999964, 18.169999999999959, 18.169999999999959,
18.169999999999959, 7.1000000000000227, 9.7999999999999545,
12.899999999999977, 14.120000000000005, 14.120000000000005,
14.660000000000082, 15.75, 14.600000000000023, 11.899999999999977,
11.539999999999964, 8.8400000000000318, 7.2999999999999545,
11.240000000000009, 11.240000000000009, 11.240000000000009,
11.240000000000009, 17.149999999999977, 42.649999999999977,
45.509999999999991, 49.279999999999973, 49.409999999999968,
43.420000000000073, 46.530000000000086, 46.530000000000086,
47.530000000000086, 38.139999999999986, 30.210000000000036,
26.340000000000032, 29.799999999999955, 29.799999999999955,
32.079999999999927, 39.300000000000068, 49.82000000000005,
46.149999999999977, 48.310000000000059, 30.909999999999968]
def test_true_range_period_6(self):
period = 6
tr = true_range.true_range(self.close_data, period)
np.testing.assert_array_equal(tr, self.tr_period_6_expected)
def test_true_range_period_8(self):
period = 8
tr = true_range.true_range(self.close_data, period)
np.testing.assert_array_equal(tr, self.tr_period_8_expected)
def test_true_range_period_10(self):
period = 10
tr = true_range.true_range(self.close_data, period)
np.testing.assert_array_equal(tr, self.tr_period_10_expected)
def test_true_range_invalid_period(self):
period = 128
with self.assertRaises(Exception) as cm:
true_range.true_range(self.close_data, period)
expected = "Error: data_len < period"
self.assertEqual(str(cm.exception), expected)