-
Pandas Basics 5
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
-
Data Transformations 6
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
Lecture2.6
-
-
Statistics 4
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
-
Reading and Writing Data 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Joins 5
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Lecture5.4
-
Lecture5.5
-
-
Grouping 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Introduction to Numpy 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Randomness 2
-
Lecture8.1
-
Lecture8.2
-
-
Numpy Data Functionality 1
-
Lecture9.1
-
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)])
21 97.376812
22 98.278403
23 98.780897
24 99.681753
25 98.998025
...
195 116.459768
196 117.562087
197 118.482369
198 119.793321
199 121.337763
Length: 155, dtype: float64
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))
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
11 NaN
12 NaN
13 NaN
14 NaN
15 NaN
16 NaN
17 NaN
18 NaN
19 NaN
20 NaN
dtype: float64
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])
0 False
1 False
2 False
3 False
4 False
dtype: bool
0 True
1 True
2 True
3 True
4 True
dtype: bool
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))
0.12