Bollinger Bands & Trading System | How to Exploit the Bandwidth | With code

by Andrea Unger

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.


Bollinger Bands are a quite versatile indicator and can be used both in reversal and trend following strategies.

In this video, our coach Alessandro explains how to use them in a trend-following strategy using the 'Bollinger Band Width' (the difference between the upper and the lower band) to evaluate variations in volatility and improve the effectiveness of entries that follow the direction of the trend.

In the video, Alessandro shows the code of a trend-following trading system that enters the market on the high and low of the previous session and tests its performance on a portfolio of futures with and without the use of the Bollinger Band Width.



Hi, I'm one of the coaches of the Unger Academy and today I'll be showing you an alternative way to use the Bollinger bands, by using them as a volatility filter for trend following entries. 

To support our work, I’d like to remind you to subscribe to our channel and click on the bell so as not to miss any new content. Please, write your thoughts of the video in the comments so we can talk about it together. Let's get started!

Theory and basics of statistics

We have already talked about the Bollinger Bands in a previous video, but I'd like to further explore the possible applications of this indicator in order to show you how to best exploit it. Bollinger Bands can be used in different ways. In our first video, we saw how to use them in a reversal strategy, looking for counter-trend entries on the lower or higher band depending on whether you are trading long or short. 

Most probably, many of you already know how to calculate the Bollinger Bands. The formula to use, which you can see in this slide, is very simple. To calculate the upper band you simply add a certain  number of standard deviations to the arithmetic mean of the last N periods, while to calculate the lower band you subtract the same number of standard deviations from the mean.

In statistics, the standard deviation is used to express the dispersion of data around an index (in this case, it is the mean of the prices). The greater the number of standard deviations, the more likely the prices will remain within the range identified. For example, in a normal (or Gaussian) distribution, a standard deviation of 1 means a 68% probability that the values will fall between the average value and the first negative or positive deviation. With 2 standard deviations, this probability rises to 95%, and with 3 standard deviations it almost reaches 100%, more precisely, it is 99.7%. 

That said, we can say that an increase in the volatility of prices will be followed by an increase in the values of the Bollinger Bands. 

By default, these bands are used with 20 periods and 2 standard deviations. Instead, in this example, we'll use them in a different way. In fact, we'll use the BandWidth, which is the distance between the upper band and the lower band, to estimate the volatility of the market. The Band Width is very easy to calculate, you simply need to make a subtraction between the upper band and the lower band.

This value can be used in our trading systems to identify the moments when the market goes from a state of flat calm, in which volatility is almost absent, to a situation characterized by more extensive and sudden movements, and take advantage of those movements with trend-following positions. For example, here you can see the distance between the upper band and the lower band at a time of low volatility, and then immediately after it, at a time of high volatility, which can give good signals for our trend following entries.

Strategy script and test

Let's go and see how to apply this formula and what results we can get. 

This is the Portfolio Trader of MultiCharts, a very useful tool to carry out these types of calculations. This portfolio includes several futures, for example energy instruments such as Crude Oil, metals such as Gold, stock market indices, commodities, and bond indices. The backtests we're going to run will go from 2010 to 2020. This means that we will have 10 years of history to explore and then draw our conclusions. The trading system we are using enters in a trend following direction on the high or low of yesterday and closes all the positions at the end of the day. As a first test, we'll evaluate the performance of the system without the Band Width filter. Then, we will compare it with the performance of the system with the filter activated.

Let's take a look at the code of the strategy. The script contains some conditions for the management of the position, 'oklong' and 'okshort' (which are useful to limit the number of entries during the same day), and then a bar counter that resets to the last bar of the previous session. The purpose of this bar counter is to avoid working, as you can see here, on the first bar of the session, which may create some problems in live trading. 

Then there is the condition for the BandWidth, which must  be less than a certain threshold. In this case, we decided to use a percentage so we can evenly apply this condition to all the instruments in the portfolio. At the moment, this input is set to 1%, as you can see here but later we will optimize it, look for a low-volatility situation, and enter the market with stop orders on the high and low of the previous session. Here you can see the "LevelLong" and "LevelShort" variables, which correspond to the high of yesterday's session and the low of yesterday's session. 

Now let's disable the condition of the Band Width by simply adding two backslashes at the beginning of the line, so we can evaluate the performance of this simple trend-following strategy.

Let's run the backtest... I'll save you the wait, as always... And we see that the results are quite good. The average trade is $17, the net profit is $463,000 but, unfortunately, there's also a sore point. In fact, we have a drawdown of about $140,000.

At this point, let's activate the BandWidth filter and run an optimization for the best value of the band width between the upper and the lower band. To do this, we can simply delete the backslashes at the beginning of the line and compile the strategy. Now we can run our optimization from 0.1% to 3% by steps of 0.1%. Let's see the results and comment on them together. 

By looking at the report we see that 0.005, which corresponds to a 0.5% excursion, is in a stability area, because 0.4% and 0.6% give similar results, and produces a significant decrease in the drawdown of the portfolio. It is true that by setting the band width value to 0.5% we get a net profit of $367,000, so a decrease of about $100,000 occurs, but it's also true that the drawdown is now about 1/3 of the one produced by the system without using the band width filter. So let's choose 0.5% as the final result and look at how the strategy performance changes.

The average trade is now $25, which is about 50% more than before activating the filter. At this moment this value is not large enough to cover the trading costs but there is certainly room for further investigation. As we can see, many markets seem to perform better than others with this rule, for example Crude Oil and Gold, so we could decide to focus on them for the development of some trading systems using the Bollinger Band Width. The shape of the equity line has also improved compared to the one you get with the classic trend-following approach. This is due to the fact that this filter let us reduce the impact of the drawdown on the final profit. 


So we’ve seen how the Bollinger Band Width can lead to a decent improvement in the performance of an approach, the classic trend-following one, which is already acceptable. The strategy is not ready to go live yet and requires further work. In your further analysis, I advise that you pay special attention to the average trade, which should always be large enough to cover the commission and possible slippage costs of the strategy. By the way, we clearly achieved more stable results that confirmed the validity of our initial assumptions. It's now up to you to delve deeper into developing your trading systems. And please let us know in the comments if you find anything good!

If you liked this video I remind you to subscribe to our channel. Below the video, you can find a link to a completely free webinar explaining how to build your portfolio of trading systems and all the steps you need to take to achieve this goal! Goodbye and thank you for your watching, see you next time!

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We’ll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.


Andrea Unger

Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.

I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems. 

In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.

Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.

Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.