Building Trading Strategies Indicators Bollinger Bands

Bollinger Bands

BollingerBands consist of three lines that form a dynamic envelope around the price chart, useful for identifying market volatility and potential price breakouts or breakdowns.

  • The middle band is a simple moving average (SMA) that is typically set to 20 periods.
  • The upper and lower bands are calculated based on the standard deviation of the price from the SMA, typically set to 2 standard deviations away.

Market Signals

Bollinger Bands are indicators of overbought or oversold conditions:

  • When the price is near the upper band, it might be considered overbought, and a reversal could be forthcoming.
  • When the price is close to the lower band, it might be oversold, indicating a possible reversal to the upside.
  • The middle band often acts as a support or resistance level.

Calculation

The typical price (TP) and bands are calculated as follows:

TP = (High + Low + Close) / 3
MidBand = SMA(TP, Period)
UpperBand = MidBand + (F * StdDev(TP))
LowerBand = MidBand - (F * StdDev(TP))

F is the multiplication factor applied to the standard deviation, usually set to 2 for the creation of the bands.

When the price moves from the lower band and crosses the middle band, the upper band can be used as a target price, and vice versa.

    import { Bar, InstrumentProcessorBase } from '@botmain/common';
    import { BollingBands } from '@botmain/indicators';

    export class InstrumentProcessor extends InstrumentProcessorBase {
        bollingBands = new BollingBands(36, 3.0);

        ...

        onBarClose(bar: Bar) {
            //Calculate the bolling bands using the bar close price
            this.bollingBands.add(bar.close);

            ...

            //Get indicator values
            const middleBand = this.bollingBands.middleBand;
            const lowerBand = this.bollingBands.lowerBand;
            const upperBand = this.bollingBands.upperBand;
            
            // Get previous values
            if(this.bollingBands.historyCount >= 2) {
                const middleBand = this.bollingBands.values[1].middleBand;
                const lowerBand = this.bollingBands.values[1].lowerBand;
                const upperBand = this.bollingBands.values[1].upperBand;
            }
        }
    }