Using the width between the Bollinger bands, called BandWidth, the system determines the low volatility time, also known as compression. After the bands are compressed, they tend to expand again in the direction of high volatility, and the price may be near the upper or lower band. This system finds low volatility and opens a new position when the price moves to the upper or lower Bollinger band.

The smoothed moving average used for Bollinger bands is also used for trailing exits. After the price has followed the upper or lower band, it returns to the middle to the moving average and the EA exits the position. The Expert Advisor calculates the position size based on volatility to work equally with any symbol and tick size, limiting losses to a fixed percentage of risk, and the lot size is calculated with a stop.

If the price moves in a favorable direction, additional lots are added (optional, specified in the input parameters) according to the pyramid system. The Expert Advisor closes only those positions that it opened itself (by coincidence, magik). John Bollinger mentions compression in his book “Bollinger on Bollinger Bands” and on his website. At times, the BandWidth value can be low, and the price moves to one band, then quickly moves to another. D. Bollinger calls this movement headfake, between the compressed bands a “saw” is formed.

[spoiler title=”Read More…”]

**Note:** the default input values are not optimized. Test the Expert Advisor on a demo and adjust the input parameters according to the acceptable risk and goals. Trend-tracking systems are based on long-term probabilities. Although such systems have lower win rates, profitability comes from large trends, in such systems losses are limited, and profitability is increased. Testing on a portfolio of symbols has shown that gains on trend symbols compensate for small losses and can provide profits when there is no trend for other symbols.

**Entrances and pyramid system:**

Since the Bollinger Bands shrink at low volatility, the EA opens a new position when the BandWidth value is within the BandWidth_Percent and the price is equal to or higher than the upper or lower Bollinger band. In the input parameter BandWidth_Bars, you can specify the number of past bars, among which you need to search for the smallest value of BandWidth. The input parameter BandWidth_Percent specifies how close the BandWidth value should be to the minimum BandWidth value to enter the market. If the value of the Max_Units parameter is greater than 1, additional pyramid inputs will be used with the ATR step specified in the ATR_between_Pyramids parameter.

**Outputs:**

Exits with trailing moving average in the middle of the Bollinger bands. The EA exits long positions when the price falls below the moving average. The EA exits short positions when the price rises above the moving average. The EA exits the position only when the BandWidth value is outside the compression limit. This additional criterion prevents an exit while the price is moving in a narrow compression corridor, in which case a stop is needed only if the price has gone in the opposite direction.

**Position and stop size:**

The Expert Advisor calculates the position size based on the volatility value, this is directly related to the stops. For stops, the ATR_Periods and Stop_Range_ATR parameters are used to calculate the ATR, which are then multiplied by a certain coefficient to set the size to the stop level from the opening price. Stops are not encoded in the position, but the EA closes the position when the price reaches the stop value. Since the position volume can increase with the pyramid system, the stops move in accordance with the last entry price. Based on the stop values, the Risk_Percent parameter, and the account information (tick size, lot size, number of decimal places, etc.), the position size selection uses the distance from the entry price to the stop level in monetary terms, and also takes into account the number of lots within the percentage you specified. This allows you to work with any symbols, price and volatility. Since the amount of funds in the account changes, the position size will also be set in accordance with the changes.

**Input parameters:**

**MA_Periods**: The number of bars for calculating the moving average in the middle of the Bollinger bands.**Deviations**: The number of standard deviations used to calculate the upper and lower Bollinger bands.**BandWidth_Bars**: The number of bars on the history to calculate the minimum BandWidth value.**BandWidth_Percent**: The percentage to add to the minimum BandWidth value. If you need to set the compression threshold line to 20% above the minimum BandWidth value, specify 20 here.**Risk_Percent**: the percentage of risk for each position at the close of the stop. For example: if the risk percentage should be equal to 2% of the equity, specify 2 in this parameter.**ATR_Periods**: The number of bars to calculate the ATR.**Stop_Range_ATR**: This value will be multiplied by ATR to determine the distance of the stop from the entry price. For example: if the stop should be at a distance of 2*ATR from the price, specify 2 in this parameter.**Max_Units**: the maximum number of position entries (including the very first initial entry) in the pyramid system when the profit on the position increases.**ATR_between_Pyramids**: This value will be multiplied by ATR and used to calculate when the next position in the pyramid should be added. For example: specify at a value of 1.5 the next entry in the pyramid system will occur when the price reaches the value of input price plus (1.5 * ATR) for long positions and input price minus (1.5 * ATR) for short positions.**Slippage**: Allowed slippage when entering a position.**Reduction_Percent**: The value by which the equity should be reduced to calculate the position size. For example: during the drawdown period, you can specify 20 in this parameter and the position size will be 20% smaller. The position size calculation function will determine the equity size as 80% of the real value to reduce risks until the drawdown ends.

In the screenshot, the Bollinger bands are displayed in pink. The additional chart window shows the BandWidth Squeeze indicator, the compression threshold line is yellow, and the green BandWidth line shows the areas when the EA can open a new position (the price has approached the band). The example shows the so-called “saw”, headfake, where the price moves to the opposite band, and for the third time enters a profitable trend.

**Disclaimer of liability:** trading is speculative in nature and may not be suitable for all investors. Use only those funds that you are ready to lose in trading, as there is always a risk of significant losses. You should carefully evaluate your personal financial situation before you start trading. Past performance does not guarantee future results. Hypothetical or simulated work results have certain limitations. Unlike real trade reports, simulation results do not represent real trade results. In addition, since the transactions were not executed, the results may be overestimated or undervalued due to the influence of certain market factors, such as lack of liquidity. Modeled trading programs in general also depend on the fact that they are designed based on past experience. No guarantee is hereby given that profits or losses similar to those presented above will be achieved or may be achieved.

[/spoiler]