Example 3: Trend Reversal Strategy Using Bollinger Bands
Another well-liked and adaptable tool for trend research is the Bollinger Band. A simple moving average (SMA) and two standard deviations above and below the SMA make up the three lines that make them up. The standard deviations show the price range and volatility, while the SMA shows the direction of the trend. Because Bollinger Bands tend to shrink when the price moves within a limited range and to expand when the price breaks out of the range, they may be used to spot trend reversals. I’ll use the daily closing Bitcoin (BTC-USD) values from January 1, 2020, to December 31, 2020, for this example. For the Bollinger Bands, I’ll use a 20-day SMA and a 2-standard deviation; for the momentum indicator, I’ll use a 14-day stochastic oscillator. The strategy is as follows:
- But when the price touches the lower band and the stochastic oscillator is below 20.
- Sell when the price touches the upper band and the stochastic oscillator is above 80.
- Hold cash otherwise.
Here is the code for this strategy:
Python3
# Import libraries import pandas as pd import yfinance as yf import matplotlib.pyplot as plt # Download data data = yf.download( "BTC-USD" , start = "2020-01-01" , end = "2020-12-31" ) # Calculate Bollinger Bands data[ "SMA_20" ] = data[ "Close" ].rolling( 20 ).mean() data[ "std_20" ] = data[ "Close" ].rolling( 20 ).std() data[ "upper_band" ] = data[ "SMA_20" ] + 2 * data[ "std_20" ] data[ "lower_band" ] = data[ "SMA_20" ] - 2 * data[ "std_20" ] # Calculate stochastic oscillator high_14 = data[ "High" ].rolling( 14 ). max () low_14 = data[ "Low" ].rolling( 14 ). min () data[ "%K" ] = (data[ "Close" ] - low_14) / (high_14 - low_14) * 100 data[ "%D" ] = data[ "%K" ].rolling( 3 ).mean() # Define signals data[ "signal" ] = 0 data.loc[(data[ "Close" ] < = data[ "lower_band" ]) & (data[ "%D" ] < = 20 ), "signal" ] = 1 data.loc[(data[ "Close" ] > = data[ "upper_band" ]) & (data[ "%D" ] > = 80 ), "signal" ] = - 1 # Calculate returns data[ "return" ] = data[ "Close" ].pct_change() data[ "strategy_return" ] = data[ "return" ] * data[ "signal" ].shift( 1 ) # Plot results plt.figure(figsize = ( 12 , 8 )) plt.subplot( 211 ) plt.plot(data[ "Close" ], label = "Price" ) plt.plot(data[ "upper_band" ], label = "Upper Band" ) plt.plot(data[ "lower_band" ], label = "Lower Band" ) plt.scatter(data.index, data[ "Close" ], c = data[ "signal" ], cmap = "coolwarm" , marker = "o" , alpha = 0.5 , label = "Signal" ) plt.title( "BTC-USD Trend Reversal Strategy Using Bollinger Bands" ) plt.xlabel( "Date" ) plt.ylabel( "Price" ) plt.legend() plt.subplot( 212 ) plt.plot(( 1 + data[ "strategy_return" ]).cumprod(), label = "Strategy" ) plt.plot(( 1 + data[ "return" ]).cumprod(), label = "Buy and Hold" ) plt.title( "Cumulative Returns" ) plt.xlabel( "Date" ) plt.ylabel( "Return" ) plt.legend() plt.tight_layout() plt.show() |
Output:
As you can see, the strategy generates buy and sell signals based on the Bollinger Bands and the stochastic oscillator. The strategy performs well in capturing some major trend reversals. There are many types of trend trading strategies, such as trend following, trend reversal, and trend breakout. Each strategy has its advantages and disadvantages, depending on the market conditions, the trader’s risk appetite, and the time horizon. Some of the common tools and indicators used for trend trading are:
- Moving averages: These are the average prices of a security over a specified period. They smooth out the price fluctuations and show the trend direction and strength. A common trend trading strategy is to use two moving averages of different lengths and trade based on their crossovers.
- Trend lines: These are straight lines that connect the highs or lows of the price movements and show the slope and direction of the trend. A common trend trading strategy is to use trend lines as support or resistance levels and trade based on their breakouts or bounces.
- Momentum indicators: These measure the speed and change of price movements and show the strengths and weaknesses of the trend. A common trend trading strategy is to use momentum indicators as filters or confirmations and trade based on their divergences or convergences with the price.
Understanding Trend Analysis and Trend Trading Strategies
Consider being able to forecast future changes in the financial markets, such as the stock market. Here’s where trend trading tactics and trend analysis are useful. We will explain trend analysis fundamentals in this post and provide newbies with a thorough overview of comprehending and using trend trading techniques. Trend analysis and trend trading are two popular techniques that traders use to identify and profit from the market’s direction.
In this article, we will explain these techniques, how they work, and how you can apply them to your trading.
Table of Content
- What is Trend Analysis?
- Steps in Trend Analysis
- What is Trend Trading?
- Trend Trading Strategies
- How to Trade the Trend – Trend Trading Strategies
- Example 1: Using a synthetic dataset
- Example 2: Trend Following Strategy Using Moving Averages
- Example 3: Trend Reversal Strategy Using Bollinger Bands
- Trend Trading Strategy – Pros and Cons
- Final Word – Why Trend Trading is a Highly Effective Technique to Trade Financial Markets?