Time for action – calculating the Average True Range

To calculate the ATR, perform the following steps:

  1. The ATR is based on the low and high price of N days, usually the last 20 days.
    N = 5
    h = h[-N:]
    l = l[-N:]
  2. We also need to know the close price of the previous day:
    previousclose = c[-N -1: -1]

    For each day, we calculate the following:

    The daily range—the difference between the high and low price:

    h – l
    

    The difference between the high and previous close:

    h – previousclose
    

    The difference between the previous close and the low price:

    previousclose – l
    
  3. The max() function returns the maximum of an array. Based on those three values, we calculate the so-called true range, which is the maximum of these values. We are now interested in the element-wise ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.