I'm pretty sure this calculation is off from correct math.. believe you want lowest(low, length) and highest(high, length) instead of low[length] and high[length] respectively.
It isn't entirely clear if ATR over the range is a decent solution or if you should ideally use the DR for the particular algo but this is much closer to being 'correct'