Bollinger Bands Part 2
The other operator that one can use is &, which will be the same as and in this case. So if we wanted to find all points outside the bounds, we could check the two conditions below:
#Check points within the bounds
print(df[(df < upper_band) & (df > lower_band)])
You will notice that the first 21 points are not in here? Why is that? If you look into the bands it will become obvious.
print(upper_band.head(21))
Because the windows are rolling over 22 days, the first 21 points are null. If we are testing to find the truth in both of these cases the null values will always return false because they are not valid numbers. One work around to this is that we can take an index and reverse the values with ~. So for example, let's grab an index for outside the bands and then reverse it.
#Get an index and then the opposite of the index
i1 = (df > upper_band) | (df < lower_band)
print(i1[:5])
print()
i2 = ~i1
print(i2[:5])
A useful application of this is to find the percent of the time that the price is outside the bounds.
#What percent is outside of the bands?
print(len(df[i1]) / len(df))