# Bollinger Bands (BB)

Bollinger Bands (BB) are an indicator used to show upper and lower limits of potential price movements. Based on standard deviation, the spacing of the bands widens as markets become more volatile, then becomes narrower as prices stabilize. For signals, Bollinger Bands can be used to identify M-Tops and W-Bottoms or to determine the strength of the trend.

### **Trading application**

The upper and lower bands represent the limits of typical price move deviation. When the price breaks these limits, it can be used as an entry signal to open a new trading position. The closer the prices move to the upper band, the more overbought the market can be said to be. The closer the prices move to the lower band, the more oversold the market is, triggering a buy signal.

The Squeeze or BB Squeeze is the central concept behind using Bollinger Bands indicators. When the market becomes too slow and there is low volatility, the price moves sideways and the Bollinger upper and lower bands become closer to each other. Traders consider this a potential sign of increased volatility and possible trading opportunities. The wider the bands move, the more likely the chance of decreased volatility and the greater the possibility of exiting a trade.

### **Calculation**

$$
Middle Band = Simple Moving Average (SMA)
$$

$$
Upper Band = SMA + (Standard Deviation Of Price \* 2)
$$

$$
Lower Band = SMA - (Standard Deviation Of Price \* 2)
$$

### **Parameters**

#### Input Parameters

| Name     | Type   | Range of value       | Description                             |
| -------- | ------ | -------------------- | --------------------------------------- |
| `stdDev` | double | (0, double.MaxValue) | Standard deviation of bollinger bands.  |
| `period` | int    | <1, int.MaxValue>    | Number of bars used in the calculation. |

#### Output Parameters

Indicator ouptuts an object of values.

| Name     | Type   | Range of value       | Description               |
| -------- | ------ | -------------------- | ------------------------- |
| `Upper`  | double | (0, double.MaxValue) | Upper band output value.  |
| `Middle` | double | (0, double.MaxValue) | Middle band output value. |
| `Lower`  | double | (0, double.MaxValue) | Lower band output value.  |

### Examples

```csharp
// Prints the current upper band value of a 20 period BB with standard
// deviation set to 2 using the daily bars close price value.
bb = indicators.Bollinger(2, 20).OnSeries(dailyBars.Close);
Log("The current upper band value is " + bb.Value.Upper.ToString());

// Prints the current middle band value
bb = indicators.Bollinger(2, 20).OnSeries(dailyBars.Close);
Log("The current middle band value is " + bb.Value.Middle.ToString());

// Prints the current lower band value
bb = indicators.Bollinger(2, 20).OnSeries(dailyBars.Close);
Log("The current lower band value is " + bb.Value.Lower.ToString());

// Prints the previous upper band value
bb = indicators.Bollinger(2, 20).Keep(2).OnSeries(dailyBars.Close);
Log("The previous upper band value is " + bb.Values[1].Upper.ToString());
```

#### Complete example

```csharp
using Signals.DataSeries.Bars;
using Signals.Framework;
using Signals.Indicators.Bollinger;

public class MyStrategy : SingleMarketStrategy
{
    private Bars dailyBars;
    private Bollinger bb;

    public override void Setup(DataMarketplace data, IndicatorsMarketplace indicators)
    {
        dailyBars = data.Bars(BarPeriodType.Day, 1).WithOffset(25);
        bb = indicators.Bollinger(2, 20).Keep(2).OnSeries(dailyBars.Close);
    }

    public override void RegisterActions()
    {
        OnUpdateOf(dailyBars).Do(() =>
        {   
            // Prints the current upper band value
            Log("The current upper band value is " + bb.Value.Upper.ToString());

            // Prints the previous upper band value
            Log("The previous upper band value is " + bb.Values[1].Upper.ToString());

            // Prints the current middle band value
            Log("The current middle band value is " + bb.Value.Middle.ToString());

            // Prints the current lower band value
            Log("The current lower band value is " + bb.Value.Lower.ToString());
        });
    }
}
```
