def testMACD(self): values = [16.39, 16.4999, 16.45, 16.43, 16.52, 16.51, 16.423, 16.41, 16.47, 16.45, 16.32, 16.36, 16.34, 16.59, 16.54, 16.52, 16.44, 16.47, 16.5, 16.45, 16.28, 16.07, 16.08, 16.1, 16.1, 16.09, 16.43, 16.4899, 16.59, 16.65, 16.78, 16.86, 16.86, 16.76] # These expected values were generated using TA-Lib macdValues = [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0.0067, 0.0106, 0.0028, -0.0342, -0.0937, -0.1214, -0.1276, -0.125, -0.1195, -0.0459, 0.0097, 0.0601, 0.0975, 0.139, 0.1713, 0.1816, 0.1598] signalValues = [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0.0036, 0.0056, 0.0048, -0.0064, -0.0313, -0.057, -0.0772, -0.0909, -0.0991, -0.0839, -0.0571, -0.0236, 0.011, 0.0475, 0.0829, 0.1111, 0.125] histogramValues = [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0.0031, 0.005, -0.002, -0.0279, -0.0624, -0.0643, -0.0504, -0.0342, -0.0205, 0.0379, 0.0668, 0.0838, 0.0865, 0.0914, 0.0884, 0.0705, 0.0348] ds = dataseries.SequenceDataSeries() macdDs = macd.MACD(ds, 5, 13, 6) for i, value in enumerate(values): ds.append(value) self.assertEqual(common.safe_round(macdDs[i], 4), macdValues[i]) self.assertEqual(common.safe_round(macdDs.getSignal()[i], 4), signalValues[i]) self.assertEqual(common.safe_round(macdDs.getHistogram()[i], 4), histogramValues[i])
def testStockChartsATR(self): # Test data from http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:average_true_range_a high = [48.70, 48.72, 48.90, 48.87, 48.82, 49.05, 49.20, 49.35, 49.92, 50.19, 50.12, 49.66, 49.88, 50.19, 50.36, 50.57, 50.65, 50.43, 49.63, 50.33, 50.29, 50.17, 49.32, 48.50, 48.32, 46.80, 47.80, 48.39, 48.66, 48.79] low = [47.790, 48.140, 48.390, 48.370, 48.240, 48.635, 48.940, 48.860, 49.500, 49.870, 49.200, 48.900, 49.430, 49.725, 49.260, 50.090, 50.300, 49.210, 48.980, 49.610, 49.200, 49.430, 48.080, 47.640, 41.550, 44.283, 47.310, 47.200, 47.900, 47.730] close = [48.160, 48.610, 48.750, 48.630, 48.740, 49.030, 49.070, 49.320, 49.910, 50.130, 49.530, 49.500, 49.750, 50.030, 50.310, 50.520, 50.410, 49.340, 49.370, 50.230, 49.238, 49.930, 48.430, 48.180, 46.570, 45.410, 47.770, 47.720, 48.620, 47.850] expected = [None, None, None, None, None, None, None, None, None, None, None, None, None, 0.56, 0.59, 0.59, 0.57, 0.62, 0.62, 0.64, 0.67, 0.69, 0.78, 0.78, 1.21, 1.30, 1.38, 1.37, 1.34, 1.32] # Build a bar dataseries using the test data. barDataSeries = bards.BarDataSeries() atrDS = atr.ATR(barDataSeries, 14) now = datetime.datetime(2000, 1, 1) for i in xrange(len(high)): b = bar.BasicBar(now + datetime.timedelta(days=i), close[i], high[i], low[i], close[i], 100, close[i], bar.Frequency.DAY) barDataSeries.append(b) self.assertEqual(common.safe_round(atrDS[-1], 2), expected[i])