|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from tests.sample_data import SampleData |
| 5 | +from pyti import stochrsi |
| 6 | + |
| 7 | + |
| 8 | +class TestStochRSI(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + """Create data to use for testing.""" |
| 11 | + self.data = SampleData().get_sample_close_data() |
| 12 | + |
| 13 | + self.stochrsi_period_6_expected = [np.nan, np.nan, np.nan, np.nan, |
| 14 | + np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, |
| 15 | + 44.471222157460431, 0.0, 0.0, 11.492848409063964, 0.0, 0.0, 0.0, |
| 16 | + 12.996028967803408, 0.0, 0.0, 100.0, 100.0, 88.834308685247748, |
| 17 | + 72.485626510038344, 68.948591144212699, 81.54139590783565, |
| 18 | + 72.069361538856384, 0.0, 100.0, 45.968610199984546, 9.057017058300568, |
| 19 | + 0.0, 72.780883175761147, 97.098272486847122, 100.0, 100.0, |
| 20 | + 78.357655504172754, 0.0, 3.3759971697837599, 23.819966852076472, |
| 21 | + 68.973352093845421, 100.0, 100.0, 88.361969800298795, 100.0, 100.0, |
| 22 | + 100.0, 0.0, 0.0, 25.101888829188145, 0.0, 0.0, 0.0, 0.0, |
| 23 | + 47.597870253604583, 52.570804308995712, 100.0, 0.14199939183552529, |
| 24 | + 24.059960445031265, 10.896141995479166, 30.513406175435293, |
| 25 | + 8.9065608580022868, 100.0, 68.068787936734168, 100.0, 100.0, |
| 26 | + 22.231267706492098, 100.0, 100.0, 10.017481472275215, 0.0, 0.0, |
| 27 | + 18.16126672568527, 40.521482277525919, 0.0, 0.0, 100.0, |
| 28 | + 35.793893628113686, 100.0, 100.0, 100.0, 33.57757547925862, |
| 29 | + 8.6247722964853466, 0.0, 17.302011810094726, 49.159702249924187, |
| 30 | + 60.932783640150376, 31.942145934203868, 82.095023751511064, 100.0, |
| 31 | + 100.0, 100.0, 100.0, 100.0, 15.042425515475383, 66.462588569115553, |
| 32 | + 74.790326011378099, 88.557628253482306, 92.354570404206441, |
| 33 | + 61.458440388782407, 0.0, 36.577660953654245, 0.0, 15.598256159532903, |
| 34 | + 31.358012359220449, 48.116305853565294, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 35 | + 33.85258109852839, 57.650642272894835, 100.0, 75.843338626788722, |
| 36 | + 74.436615187163184, 0.0, 4.4097493664417158, 0.0, 94.669594611808265, |
| 37 | + 65.534738257501118, 27.780745298221497, 0.0, 47.709624772979161, |
| 38 | + 32.163553172474437, 100.0] |
| 39 | + |
| 40 | + self.stochrsi_period_8_expected = [np.nan, np.nan, np.nan, np.nan, |
| 41 | + np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, |
| 42 | + np.nan, np.nan, 0.0, 0.0, 0.0, 5.9170858267051587, 0.0, 0.0, |
| 43 | + 44.731067481598593, 100.0, 89.261888846566762, 73.506626263784852, |
| 44 | + 70.124518845179722, 92.473144438199071, 91.742030577620142, |
| 45 | + 15.037735074756913, 100.0, 47.306749309021193, 10.140545886437421, 0.0, |
| 46 | + 72.278552326472152, 97.679213884699251, 100.0, 100.0, |
| 47 | + 80.347390204472063, 47.828534810102333, 49.538188855519977, |
| 48 | + 27.611275977441203, 67.963793266686267, 78.134869525725151, |
| 49 | + 99.252804098374185, 90.150479498192112, 100.0, 100.0, 100.0, |
| 50 | + 4.9013099702443785, 0.0, 24.68617122074053, 0.0, 0.0, 0.0, 0.0, |
| 51 | + 39.802519009096748, 29.010598833949796, 53.053222984371452, 0.0, |
| 52 | + 21.631906841014569, 8.3458733315535447, 25.22414006544841, |
| 53 | + 4.5780668059713552, 49.007350471187422, 34.273386605258764, 100.0, |
| 54 | + 100.0, 22.403954556738867, 100.0, 100.0, 12.105988070501787, 0.0, 0.0, |
| 55 | + 16.657204131414304, 37.35692115954221, 0.0, 0.0, 100.0, |
| 56 | + 32.997720377696496, 100.0, 100.0, 100.0, 51.334083599997683, |
| 57 | + 31.853266142141972, 8.1527836280558503, 22.745953245334995, |
| 58 | + 45.508879820782681, 27.946619216922286, 14.815380622177496, |
| 59 | + 80.088315251347964, 100.0, 100.0, 100.0, 100.0, 100.0, |
| 60 | + 56.481950024728867, 86.652579488979171, 93.049895919142116, 100.0, |
| 61 | + 100.0, 69.687224483627773, 4.7487429986927374, 40.824255220794718, 0.0, |
| 62 | + 14.82258516102512, 25.574101538565898, 33.214453816875086, 0.0, 0.0, |
| 63 | + 0.0, 0.0, 0.0, 12.028791696947424, 5.742958082055595, |
| 64 | + 46.151131514517758, 73.698400050314845, 72.171996877638023, 0.0, |
| 65 | + 3.9883199142603543, 0.0, 63.887076434178304, 43.192059380396671, |
| 66 | + 18.535460030504883, 0.0, 44.316525695640522, 29.344038948303997, 100.0] |
| 67 | + |
| 68 | + self.stochrsi_period_10_expected = [np.nan, np.nan, np.nan, np.nan, |
| 69 | + np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, |
| 70 | + np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 0.0, 0.0, |
| 71 | + 27.66288026615667, 68.42179408282631, 60.95511012399831, |
| 72 | + 73.056366879654533, 69.632283416324114, 91.673870575592005, |
| 73 | + 90.973445004595106, 62.013426837224038, 99.412333298280316, |
| 74 | + 56.029604272708319, 9.5954111342724602, 0.0, 71.203658595987136, |
| 75 | + 97.102435963871216, 100.0, 100.0, 81.393245300884587, |
| 76 | + 49.654269940792076, 51.293355239394344, 60.927269295952172, |
| 77 | + 83.862439770570063, 81.039109157752904, 100.0, 90.97577696869304, 100.0, |
| 78 | + 100.0, 100.0, 58.073925890407921, 41.358292066458077, |
| 79 | + 25.654266421595828, 0.0, 0.0, 0.0, 0.0, 23.713848713037535, |
| 80 | + 16.806950663591991, 47.466120473538204, 0.0, 10.796474428727892, |
| 81 | + 4.690894374748388, 21.71719912790817, 1.5761771583204254, |
| 82 | + 41.050006983973205, 27.2736467372333, 100.0, 100.0, 21.771640646517728, |
| 83 | + 100.0, 100.0, 26.039977920480617, 0.0, 0.0, 15.737216917199534, |
| 84 | + 35.412360099771497, 0.0, 0.0, 50.704904555903788, 14.994477423420522, |
| 85 | + 100.0, 100.0, 100.0, 51.87903440907273, 31.867669596262672, |
| 86 | + 26.242262653493135, 37.395093360643585, 49.651518706252737, |
| 87 | + 34.777475814569073, 14.222945117349619, 34.122394180512103, |
| 88 | + 59.589894426757162, 100.0, 100.0, 100.0, 100.0, 60.387458746010005, |
| 89 | + 93.702909932596725, 100.0, 100.0, 100.0, 81.695205658391245, |
| 90 | + 18.390800382020387, 51.052985817884824, 0.0, 14.606027212675402, |
| 91 | + 25.21593064024956, 32.720972201555149, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 92 | + 10.169626915478698, 3.627127999972207, 19.385791765467346, |
| 93 | + 13.371820642369503, 22.43112726583951, 0.0, 3.4785405767313109, 0.0, |
| 94 | + 54.371077241311575, 35.289162480238765, 7.8549519711197089, 0.0, |
| 95 | + 28.494276007229587, 18.19959392470588, 100.0] |
| 96 | + |
| 97 | + def test_stochrsi_period_6(self): |
| 98 | + period = 6 |
| 99 | + sr = stochrsi.stochrsi(self.data, period) |
| 100 | + np.testing.assert_array_equal(sr, self.stochrsi_period_6_expected) |
| 101 | + |
| 102 | + def test_stochrsi_period_8(self): |
| 103 | + period = 8 |
| 104 | + sr = stochrsi.stochrsi(self.data, period) |
| 105 | + np.testing.assert_array_equal(sr, self.stochrsi_period_8_expected) |
| 106 | + |
| 107 | + def test_stochrsi_period_10(self): |
| 108 | + period = 10 |
| 109 | + sr = stochrsi.stochrsi(self.data, period) |
| 110 | + np.testing.assert_array_equal(sr, self.stochrsi_period_10_expected) |
| 111 | + |
| 112 | + def test_stochrsi_invalid_period(self): |
| 113 | + period = 128 |
| 114 | + with self.assertRaises(Exception) as cm: |
| 115 | + stochrsi.stochrsi(self.data, period) |
| 116 | + expected = "Error: data_len < period" |
| 117 | + self.assertEqual(str(cm.exception), expected) |
0 commit comments