def average_true_range(close_data, period): """ Average True Range. Formula: ATRt = ATRt-1 * (n - 1) + TRt / n """ tr = true_range(close_data, period) atr = smoothed_moving_average(tr, period) atr[0:period - 1] = tr[0:period - 1] return atr
def average_true_range(close_data, period): """ Average True Range. Formula: ATRt = ATRt-1 * (n - 1) + TRt / n """ tr = true_range(close_data, period) atr = np.zeros(len(tr)) atr[0:period - 1] = tr[0:period - 1] atr[period - 1:period - 1 + period - 1] = tr[0:period - 1] atr[period - 1 + period - 1] = ((1 / float(period)) * sum(tr[period - 1:period - 1 + period])) for idx in range(period - 1 + period, len(tr)): atr[idx] = (atr[idx - 1] * (period - 1) + tr[idx]) / period return atr
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)
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)