Bitcoin Bollinger Bands Analysis
Published on Feb 7, 2024 6:22 PM
9
What are Bollinger Bands?¶
A Bollinger Band is a technical analysis tool defined by a set of trendlines. They are plotted as two standard deviations, both positively and negatively, away from a simple moving average (SMA) of a security's price and can be adjusted to user preferences. reference
Let's use pandas
and a Bitcoin historical dataset to put Bollinger Bands into practice! 🚀
Calculate Bollinger Bands¶
In [1]:
import pandas as pd
In [2]:
df = pd.read_csv("/data/bitstamp-btcusd-minute-previous-year/btcusd_last_year.csv")
In [3]:
df.head()
Out[3]:
timestamp | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 2023-01-01 00:00:00+00:00 | 16530.0 | 16532.0 | 16530.0 | 16532.0 | 0.912895 |
1 | 2023-01-01 00:01:00+00:00 | 16532.0 | 16532.0 | 16529.0 | 16529.0 | 0.008760 |
2 | 2023-01-01 00:02:00+00:00 | 16528.0 | 16528.0 | 16528.0 | 16528.0 | 0.073547 |
3 | 2023-01-01 00:03:00+00:00 | 16528.0 | 16528.0 | 16525.0 | 16525.0 | 0.842854 |
4 | 2023-01-01 00:04:00+00:00 | 16524.0 | 16524.0 | 16523.0 | 16523.0 | 0.160569 |
In [4]:
df["timestamp"] = pd.to_datetime(df['timestamp'])
In [5]:
df = df.groupby(by=df['timestamp'].dt.date).max()
In [6]:
# set number of days and standard deviations to use for rolling
# lookback period for Bollinger band calculation
window = 30
no_of_std = 1.5
# calculate rolling mean and standard deviation
rolling_mean = df['close'].rolling(window).mean()
rolling_std = df['close'].rolling(window).std()
# create two new DataFrame columns to hold values of upper and lower Bollinger bands
df['Rolling Mean'] = rolling_mean
df['Bollinger High'] = rolling_mean + (rolling_std * no_of_std)
df['Bollinger Low'] = rolling_mean - (rolling_std * no_of_std)
In [7]:
df.tail()
Out[7]:
timestamp | open | high | low | close | volume | Rolling Mean | Bollinger High | Bollinger Low | |
---|---|---|---|---|---|---|---|---|---|
timestamp | |||||||||
2023-12-27 | 2023-12-27 23:59:00+00:00 | 43691.0 | 43698.0 | 43681.0 | 43698.0 | 92.433358 | 42695.3 | 45756.957314 | 39633.642686 |
2023-12-28 | 2023-12-28 23:59:00+00:00 | 43804.0 | 43812.0 | 43793.0 | 43801.0 | 61.328604 | 42876.0 | 45695.184787 | 40056.815213 |
2023-12-29 | 2023-12-29 23:59:00+00:00 | 43128.0 | 43128.0 | 43058.0 | 43114.0 | 123.586184 | 43034.0 | 45548.274098 | 40519.725902 |
2023-12-30 | 2023-12-30 23:59:00+00:00 | 42586.0 | 42599.0 | 42565.0 | 42598.0 | 30.289860 | 43182.3 | 45287.929504 | 41076.670496 |
2023-12-31 | 2023-12-31 23:59:00+00:00 | 42848.0 | 42867.0 | 42802.0 | 42854.0 | 38.494573 | 43311.7 | 45051.735143 | 41571.664857 |
In [8]:
df[['close','Bollinger High','Bollinger Low']].plot()
Out[8]:
<Axes: xlabel='timestamp'>
Conclusion¶
pandas
makes it super easy to calculate and plot Bollinger Bands. Is this any useful? Well, Bollinger Bands are used in some trading strategies, buying assets when the price crosses the lower band and selling them when they cross the upper one (not financial advice 😅). Performance of the strategy will vary depending on how you tune the different bands paramenters.
Go ahead! Make a copy of this playground and do your own tests by changing params and seeing results live!