I've coded up a Custom Factor of ATR - thanks go to Anthony Garner, I used his code for the Kaufman Efficiency Ratio as a base. This is the code:
class Average_True_Range(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.high, USEquityPricing.low]
window_length = 15
def compute(self, today, assets, out, close, high, low):
lb = self.window_length
atr = np.zeros(len(assets), dtype=np.float64)
a=np.array(([high[1:(lb)]-low[1:(lb)],abs(high[1:(lb)]-close[0:(lb-1)]),abs(low[1:(lb)]-close[0:(lb-1)])]))
b=a.T.max(axis=2)
c=b.sum(axis=1)
atr=c /(lb-1)
out[:] = atr
Interestingly, and a little bit annoyingly, I can't get my results to line up exactly with the results that TALIB.ATR generates. I don't if this is because TALIB uses a slightly different formulation of ATR? I haven't been able to find docs showing exactly how they calculate their version. Any ideas / suggestions / bugs spotted, please let me know, thanks.