-
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
Bollinger Bands¶
The idea of bollinger bands is that we may see reversion to a rolling mean over time. Essentially, what one does is takes the rolling mean as well as two standard deviations above and below and you see at what times there is a breakout from these bands. Some people believe that if there is a breakout it will reverse, but others do not believe this is a viable trading strategy. This dataset is purposely created in a way for this to work, but in the real world you will have to determine for yourself what you think is correct or not.
#Let's create bollinger bands by adding two standard deviations above and below
#Create the lower and upper bands
upper_band = rolling_mean + std*2
lower_band = rolling_mean - std*2
#Plot the bands
df.plot(kind="line",color="k")
rolling_mean.plot(kind="line",color="b")
upper_band.plot(kind="line",color="r")
lower_band.plot(kind="line",color="r")
plt.show()
To identify what points are below or above the bollinger bands, we can use our boolean indexing. Let's create two indices, one for above the upper band and one for below the lower band.
#Index for above the upper bound
i1 = df > upper_band
#Index for above the lower bound
i2 = df < lower_band
print(i1[:5])
print()
print(i2[:5])
0 False
1 False
2 False
3 False
4 False
dtype: bool
0 False
1 False
2 False
3 False
4 False
dtype: bool
We may also want to combine the two to make an index that tells us whether the price is either above or below the bands. First, we can see one example of where the price is above the band below (we also see no points are below the bands):
print(i1[55:65])
print()
print(i2[55:65])
55 False
56 False
57 False
58 False
59 True
60 False
61 False
62 False
63 False
64 False
dtype: bool
55 False
56 False
57 False
58 False
59 False
60 False
61 False
62 False
63 False
64 False
dtype: bool
To combine them as an or statement, we can use the | (pipe) operator which will go index by index comparing if either is true.
#Create an index for either i1 or i2 being true
i3 = i1 | i2
print(i3[55:65])
55 False
56 False
57 False
58 False
59 True
60 False
61 False
62 False
63 False
64 False
dtype: bool
The reason you need to use the pipe operator is because pandas uses C libraries under the hood. The shorthand way to achieve this without setting variables would be the following (notice you need parantheses around the two conditions):
#Achieves the same goal:
print( (df > upper_band) | (df < lower_band) )
0 False
1 False
2 False
3 False
4 False
...
195 False
196 False
197 False
198 False
199 False
Length: 200, dtype: bool
We are able to use this to index into the actual data.
print(df[(df > upper_band) | (df < lower_band)])
33 95.581040
34 94.909794
36 93.779819
59 100.498860
68 101.962135
75 94.446699
76 93.640495
77 93.968470
90 102.108535
91 102.793692
92 102.918407
117 102.772938
147 109.376186
148 109.608680
149 110.291232
186 107.934320
187 108.675057
188 109.455722
189 109.882610
190 111.542948
191 112.412441
192 114.817100
193 116.428156
194 117.587206
dtype: float64