Time for action – enveloping with Bollinger bands
We already know how to calculate the simple moving average. So, if you need to, please review the Time for action – computing the simple moving average section in this chapter. This example will introduce the NumPy fill
function. The fill
function sets the value of an array to a scalar value. The function should be faster than array.flat = scalar
or you have to set the values of the array one by one in a loop. Perform the following steps to envelope with Bollinger bands:
Starting with an array called
sma
that contains the moving average values, we will loop through all the data sets corresponding to those values. After forming the data set, calculate the standard deviation. Note that it is necessary, at a certain point, to calculate the difference between each data point and the corresponding average value. If we did not have NumPy, we would loop through these points and subtract each of the values one by one from the corresponding average...