Amibroker AFL code snippet for calculating Bollinger BandWidth and Bollinger %B

Bollinger band is an universally used volatility indicator by traders to identify squeeze and breakouts. There are couple of indicators derived out of Bollinger Bands and they are Band Width and Bollinger %B. In Amibroker Formula Language you need to calculate them before using them as there is no inbuilt indicator.

What is Bollinger Band Width?

As mentioned above Bollinger BandWidth is an indicator derived from Bollinger Bands and it usually measures the percentage difference between upper and lower bands. Bollinger BandWidth is calculated as follows:

Bollinger BandWidth = (Upper Bollinger Band - Lower Bollinger Band) / Middle Band

Middle Band is usually the Simple Moving Average of the period you have selected and by default its 20 period. You can multiply the above calculated value by 100 to get the percentage.

When the volatility decreases the Bollinger BandWidth value also decreases and therefore it can be used to identify squeeze. Usually the BandWidth value for squeeze can vary and its usually less than 10%. The value depends on the kind of security and you need to understand the behaviour of the security before finalizing the BandWidth value for squeeze. Usually the assumption is that price will make a quick move in either direction after the squeeze.

Amibroker AFL code for Bollinger BandWidth calculation:

MA_20 = MA (C,20);   		//This is the middle line
BBTop = BBandTop (Close,20,2);	//Upper Bollinger Band
BBBottom = BBandBot (Close,20,2);	//Lower Bollinger  Band
BBWidth = ( (BBTop - BBBottom)  / MA_20 ) * 100;  // Bollinger Band Width Calculation

What is Bollinger %B?

Bollinger %B is again a derivative of Bollinger band and usually indicates the relative position of the price of the security with respect to the upper and lower Bollinger bands. %B is calculated using the following formulae:

%B	 = 	( Price of the security  - Lower Bollinger Band)
________________________________________
(Upper Bollinger Band - Lower Bollinger Band)
1. %B = 1 means that Price is sitting on the Upper Bollinger Band
2. %B > 1 means that Price has crossed above the Upper Bollinger Band
3. %B = 0 means that Price is sitting on the Lower Bollinger Band
4. %B < 0 means that Price has crossed below the Lower Bollinger Band
5. %B = 0.5 means that Price is sitting on the Middle Band
6. %B > 0.5 and %B < 1 means that Price is between Upper Bollinger Band and Middle Band
7. %B < 0.5 and %B > 0 means that Price is between Lower Bollinger Band and Middle Band

Some traders use %B to identify oversold and overbought areas and I personally prefer using %B to identify the breakouts from the Bollinger Band Squeeze.

Amibroker AFL code for Bollinger %B calculation:

MA_20 = MA (C,20);   		//This is the middle line
BBTop = BBandTop (Close,20,2);	//Upper Bollinger Band
BBBottom = BBandBot (Close,20,2);	//Lower Bollinger  Band
PercentB = (Close - BBBottom) / (BBTop - BBBottom);  // Bollinger %B indicator Calculation

Note that % can’t be used as the starting char in naming a variable

Please note that above piece of code is for only informational purpose and you need to personally validate and test it before using it