forked from kylejusticemagnuson/pyti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chaikin_money_flow.py
173 lines (162 loc) · 11.1 KB
/
test_chaikin_money_flow.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import absolute_import
import unittest
import numpy as np
from tests.sample_data import SampleData
from pyti import chaikin_money_flow
class TestChaikinMoneyFlow(unittest.TestCase):
def setUp(self):
"""Create data to use for testing."""
self.close_data = SampleData().get_sample_close_data()
self.high_data = SampleData().get_sample_high_data()
self.low_data = SampleData().get_sample_low_data()
self.volume = SampleData().get_sample_volume()
self.cmf_period_6_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
0.003312520909302368, -0.0048613550605095908, 0.0076368288342983631,
0.0065287223745628502, 0.010712462475030939, -0.016573767174777168,
-0.022118018755417572, -0.029221567084304925, -0.033484757183585401,
-0.041735293635758459, -0.042843154065898718, -0.020876675876256596,
-0.024415870658692791, -0.026707069318664234, -0.080030925467134043,
-0.057147769053649287, -0.065604808877281429, -0.065939141131154788,
-0.046243722522452443, -0.039084580765582945, -0.023732518823802458,
-0.014371385759892283, -0.0053841310735424572, -0.0073566431080532707,
-0.023993372933344703, -0.029960428245405232, -0.020848896421251553,
-0.033221845069711479, -0.023632410244811587, -0.0097050088230458322,
-0.018370449199058135, 0.010361413074362698, 0.02995516308307368,
0.035669013751713101, 0.042225745231989881, 0.016262549780505727,
0.0013941588013852452, 0.012306879131758812, 0.0035702426675349218,
0.016855940611040648, 0.017195749786707487, 0.035142495645697887,
0.081928386974877379, 0.074639303829334827, 0.059587959294833265,
0.025673521303580357, -0.0058017021658257461, -0.017649987961487128,
-0.037743680456322731, -0.030009726186828133, -0.035697538763242824,
-0.037644159883863451, 0.0048928404466888412, -0.00090102511926272368,
0.0035648800422953685, -0.015052762227444404, 0.014071386096076447,
0.03508329543861937, 0.012371868491255654, 0.021424949522295789,
0.020558640966843865, 0.064448403594026746, 0.052176586584946193,
-0.019569995625100225, -0.028186526955462123, -0.031094787908345023,
-0.032460504024932439, -0.058198150002280631, -0.043503190797697537,
-0.00095594109778578086, -0.010813889014902886, -0.011190581326110635,
0.0078677120282683397, 0.037517542650319986, 0.044658480463605946,
0.055190082593348115, 0.071897357104455467, 0.073915056586359301,
0.07101148964635963, 0.059849856842446009, 0.059094210936938292,
0.03287724077897463, 0.044715034918426266, 0.045192127365791333,
0.033795150822328299, 0.075347022889300272, 0.060429544725187107,
0.081840951426436659, 0.037721963312571133, 0.032639794237848713,
0.022413515752428483, 0.0050258198847992113, 0.010372825841295268,
-0.00083449577106293733, 0.0085669902231504426, 0.027089129950182974,
0.03956141033382661, 0.044011605196096544, 0.038891332225638738,
0.027574878189471158, 0.046321405729603714, 0.041172145145542835,
0.034642007487542176, 0.014260177160802688, 0.0082988065208204845,
0.017126773507882866, 0.014741503914037739, 0.016010639972291973,
0.025326112362985931, 0.034629648875386297, 0.014997042039988612,
0.01069599087866673, 0.011127907594170367, -0.0080168866293592808,
-0.013671648032558471, -0.026946141394489087, -0.021756121749873004,
-0.032467558230566636, -0.041891312663086974, -0.035822604814123339,
-0.031212648439507711, -0.040638532850568315]
self.cmf_period_8_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
np.nan, np.nan, 0.0081185024674913005, 0.0025371802819565883,
-0.0010644977834666388, -0.0056728301312701027, -0.0041694952276188808,
-0.027797521862243494, -0.014603048497697219, -0.019669640273245477,
-0.043772436549748425, -0.032246251569552471, -0.035485601011810967,
-0.038497579799628007, -0.056104562803820458, -0.040592663917418982,
-0.06878156913062386, -0.051868037982436971, -0.039076571889279231,
-0.050731239889772968, -0.053229551728136919, -0.030648158861072514,
-0.012411553304442827, -0.013827755337462506, -0.0087894337832712336,
-0.02372021526121806, -0.034115196226625788, -0.025887629022838941,
-0.012678892347838255, -0.010229402522448557, -0.018380015220900599,
-0.0091200962156104716, 0.0079882390294293473, 0.025256686134179286,
0.036646526288534528, 0.022578841768494146, 0.00037994399427053205,
0.0094056760373523677, 0.015170928752505568, 0.024780406255827572,
0.023985104430904548, 0.025206632383119276, 0.027437965711461888,
0.040771722434282294, 0.057773298969144654, 0.042888453362141608,
0.034118953538708363, 0.0086195557061872017, -0.0053082066228187265,
-0.0073276947410392933, -0.040339954980860432, -0.044024960336941268,
-0.008372116834863547, -0.010894944701009993, 0.002069543889197569,
-0.0070732478803152863, -0.0027650689324161004, 0.003099300977691961,
0.028980930474517174, 0.032897879595979874, 0.011512894812962913,
0.037606534276955164, 0.03161087470746074, 0.0014348407984348796,
-0.0076208128785508226, -0.02255891656436438, -0.024463692779761438,
-0.031399038040626388, -0.025163213707663782, -0.029551186437460762,
-0.03645993477631073, -0.015706223008120388, 0.0015586402908818618,
0.019379406270457197, 0.033617486164410258, 0.056610182531136054,
0.051801585060995865, 0.043696289983268924, 0.064339502557726136,
0.073499010373059309, 0.069989285678150617, 0.055791870559251916,
0.053098040911644169, 0.03330390300209949, 0.034414825993031972,
0.074668764622390055, 0.06129901630246664, 0.075081747905173607,
0.037480226477018132, 0.032889925186722319, 0.020106235192252808,
0.028204580027412218, 0.034341643352812236, 0.021887527518800701,
0.0071316698495589425, -0.0052750838209651082, 0.015188886014108748,
0.036061294242859893, 0.050278602384032256, 0.043130532971042447,
0.034563264668915718, 0.026040168606977436, 0.034587936909265836,
0.02876909144339216, 0.03469067860193259, 0.024532129642417532,
0.014883586477139587, 0.016726987370967537, 0.012188281555131105,
0.016841358495234233, 0.020078868602802388, 0.02524487617720117,
0.017970496234460627, -0.0030203029703223822, -0.0096650004775112285,
-0.016473215917856138, -0.015549623508933264, -0.022702905873274063,
-0.024710955783392941, -0.038703957202912316, -0.041452776726492772,
-0.041157742958483921]
self.cmf_period_10_expected = [np.nan, np.nan, np.nan, np.nan, np.nan,
np.nan, np.nan, np.nan, np.nan, 0.00097005442845879376,
-0.0064199268473497896, -0.00981630771032585, -0.01539385491408015,
-0.0019407376530060584, -0.021201955642506994, -0.025200322249567,
-0.016200232414935737, -0.037811531108227089, -0.044026758965205533,
-0.058534077028334068, -0.046548203377963476, -0.051983201626286779,
-0.039512066195189634, -0.046003067307706036, -0.042132407072485917,
-0.046334449727507415, -0.04133207922271704, -0.039018647891641382,
-0.027334922695207469, -0.013686335276416654, -0.025804171596085258,
-0.019944595085910129, -0.021723804450678588, -0.024950741592236047,
-0.0092280984329375388, -0.010856808489330882, -0.0096349938344056571,
0.0029175322839710091, 0.0082428396230406504, 0.019496326105300225,
0.017285768031592645, 0.001954688027274251, 0.014596209539885734,
0.012571839892109608, 0.020738139163870514, 0.030856858152494813,
0.03049958536258458, 0.031648221684272407, 0.030366540308311901,
0.01776324429095813, 0.025830127055555277, 0.040411485921986831,
0.026592245316573945, 0.027352531826117733, 0.011207243951777237,
-0.015716720734726072, -0.024814013216715024, -0.016909757167403869,
-0.020703472631734419, -0.0074657409934277136, -0.014086706455012497,
-0.0029493218051483677, 0.006586361206931457, 0.010589147609965469,
0.0058748142910560203, 0.025036458880156737, 0.042389180311841836,
0.021170442660274864, -0.00465179736632976, -0.011478833549338071,
-0.0063553451892820632, -0.0096521313596829271, -0.024853981082800104,
-0.019947952803719058, -0.012644857452069816, -0.02282098981637035,
-0.035389803360523206, -0.022302450718866752, 0.0096606229097797279,
0.023530102149541612, 0.039575306318488879, 0.041663203623864616,
0.046846269401670804, 0.049147804760104809, 0.046532703832447543,
0.064532281083369877, 0.067884435413460148, 0.063432902363722246,
0.0517389246620805, 0.043502469230294938, 0.058961336245194944,
0.057160593743307196, 0.074671739501174617, 0.04045293665095788,
0.033758229773749648, 0.022087082552981425, 0.028947816115888845,
0.030952954384161221, 0.038982492918535165, 0.028150926559148217,
0.014865469834815303, 0.01170230670122519, 0.0083995509197173843,
0.030907101733504111, 0.036899328396437309, 0.04492090321100161,
0.039661457831712481, 0.026671091384831067, 0.018272102875036875,
0.034633486248171697, 0.033332625706768454, 0.034554336623121552,
0.02330355950897256, 0.012915875614998074, 0.01725568578495628,
0.010453027043024619, 0.013367439404347276, 0.021473243280857524,
0.012916789497828559, 0.0011786722377113861, -0.010994206543604062,
-0.012037897092003943, -0.015417115757289098, -0.01932122426096974,
-0.029475944251071699, -0.027951042217520154, -0.042543393896090208]
def test_cmf_period_6(self):
period = 6
cmf = chaikin_money_flow.chaikin_money_flow(self.close_data, self.high_data, self.low_data, self.volume, period)
np.testing.assert_array_equal(cmf, self.cmf_period_6_expected)
def test_cmf_period_8(self):
period = 8
cmf = chaikin_money_flow.chaikin_money_flow(self.close_data, self.high_data, self.low_data, self.volume, period)
np.testing.assert_array_equal(cmf, self.cmf_period_8_expected)
def test_cmf_period_10(self):
period = 10
cmf = chaikin_money_flow.chaikin_money_flow(self.close_data, self.high_data, self.low_data, self.volume, period)
np.testing.assert_array_equal(cmf, self.cmf_period_10_expected)
def test_cmf_invalid_period(self):
period = 128
with self.assertRaises(Exception) as cm:
chaikin_money_flow.chaikin_money_flow(self.close_data, self.high_data, self.low_data, self.volume, period)
expected = "Error: data_len < period"
self.assertEqual(str(cm.exception), expected)
def test_cmf_invalid_data(self):
self.close_data.append(1)
period = 6
with self.assertRaises(Exception) as cm:
chaikin_money_flow.chaikin_money_flow(self.close_data, self.high_data, self.low_data, self.volume, period)
expected = ("Error: mismatched data lengths, check to ensure that all input data is the same length and valid")
self.assertEqual(str(cm.exception), expected)