def calculate_atr(self, window=14):
""" Average True Range Shows volatility of market
ATR(t) = ((n-1) * ATR(t-1) + Tr(t)) / n
where Tr(t) = Max(Abs(High - Low), Abs(High - Close(t - 1)), Abs(Low - Close(t - 1));
"""
self.set_max_win(window)
i = 0
tr_l = [0]
for i in range(self.df.index[-1]):
tr = max(self.df.get_value(i + 1, 'High'),
self.df.get_value(i, 'Adj_Close')) - min(self.df.get_value(i + 1, 'Low'),
self.df.get_value(i, 'Adj_Close'))
tr_l.append(tr)
name = 'atr_%s' % (window)
self.df[name] = pd.ewma(pd.Series(tr_l), span=window, min_periods=window)
return name
评论列表
文章目录