Time for action – calculating the average true range

To calculate the average true range, perform the following steps:

  1. The ATR is based on the low and high price of N days, usually the last 20 days.
    N = int(sys.argv[1])
    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:

    • h – l: The daily range (the difference between high and low price)
    • h – previousclose: The difference between high price and previous close
    • previousclose – l: The difference between the previous close and the low price
  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 maxima across arrays—meaning the maxima of the first elements in the arrays, the second elements in the arrays, and so on. Use the NumPy maximum function instead of the max function for this purpose.
    truerange = np.maximum(h - l, h - previousclose, previousclose - l) 
  4. Create an atr array of size N and initialize its values to 0.
    atr = np.zeros(N)
  5. The first value of the array is just the average of the truerange array.
    atr[0] = np.mean(truerange)

    Calculate the other values with the following formula:

    Time for action – calculating the average true range

    Here, PATR is the previous day's ATR; TR is the true range.

    for i in range(1, N):
       atr[i] = (N - 1) * atr[i - 1] + truerange[i]
       atr[i] /= N

What just happened?

We formed three arrays, one for each of the three ranges—daily range, the gap between the high of today and the close of yesterday, and the gap between the close of yesterday and the low of today. This tells us how much the stock price moved and, therefore, how volatile it is. The algorithm requires us to find the maximum value for each day. The max function that we used before can give us the maximum value within an array, but that is not what we want here. We need the maximum value across arrays, so we want the maximum value of the first elements in the three arrays, the second elements, and so on. In this Time for action tutorial, we saw that the maximum function can do this. After that, we computed a moving average of the true range values (see atr.py).

import numpy as np
import sys

h, l, c = np.loadtxt('data.csv', delimiter=',', usecols=(4, 5, 6), unpack=True)

N = int(sys.argv[1])
h = h[-N:]
l = l[-N:]

print "len(h)", len(h), "len(l)", len(l)
print "Close", c
previousclose = c[-N -1: -1]

print "len(previousclose)", len(previousclose)
print "Previous close", previousclose
truerange = np.maximum(h - l, h - previousclose, previousclose - l) 

print "True range", truerange

atr = np.zeros(N)

atr[0] = np.mean(truerange)

for i in range(1, N):
   atr[i] = (N - 1) * atr[i - 1] + truerange[i]
   atr[i] /= N

print "ATR", atr

In the following tutorials, we will learn better ways to calculate moving averages.

Have a go hero – taking the minimum function for a spin

Besides the maximum function, there is a minimum function. You can probably guess what it does. Make a small script or start an interactive session in IPython to prove your assumptions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.152