Author: Copyright � 2005, David W. Thomas
BB_-_HL

This technical indicator is designed to provide a visual framework for understanding price action and market volatility. It functions by identifying the "center" of current price movements and establishing boundaries based on how much the price typically fluctuates away from that center.

Core Logic Overview

The tool operates using a two-step calculation process to generate three distinct lines on your chart:

1. Establishing the Baseline (Moving Average) The script first calculates a Simple Moving Average (SMA). This acts as the "heartbeat" of the price, smoothing out short-term noise to show the general trend over a specific period (defined by the user). By identifying this average, the tool creates a steady reference point for the current market direction.

2. Measuring Volatility (Standard Deviation) Rather than using a fixed distance for its boundaries, the script dynamically measures how far the "High" and "Low" prices of each candle stray from the baseline moving average. It looks specifically at the furthest extremes to determine the intensity of the price swings.

3. Creating the Dynamic Bands The final result is the construction of an upper and lower boundary (the "Bands"). The distance of these bands from the center is determined by:

  • The average volatility over the selected period.
  • A multiplier (Deviation) that allows you to stretch or shrink the bands based on your preference.

How it works for the user

Because the bands are tied to actual price movement, they expand when the market becomes volatile and contract when the market settles into a quiet trend. This provides a visual "envelope" of normal market behavior, helping traders distinguish between standard price fluctuations and significant breakouts or shifts in momentum.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
9 Views
1 Downloads
0 Favorites
BB_-_HL
//+------------------------------------------------------------------+
//|                                                        BB-HL.mq4 |
//|                                Copyright © 2005, David W. Thomas |
//|                                           mailto:davidwt@usa.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, David W. Thomas"
#property link      "mailto:davidwt@usa.net"
//----
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 MediumSeaGreen
#property indicator_color2 MediumSeaGreen
#property indicator_color3 MediumSeaGreen
//---- input parameters
extern int       Per=20;
extern double    nDev=2.0;
//---- buffers
double MABuffer[];
double UpperBandBuffer[];
double LowerBandBuffer[];
double DevsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(4);
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MABuffer);
   SetIndexDrawBegin(0,Per);
   SetIndexEmptyValue(0, 0.0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpperBandBuffer);
   SetIndexDrawBegin(1,2*Per);
   SetIndexEmptyValue(1, 0.0);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,LowerBandBuffer);
   SetIndexDrawBegin(2,2*Per);
   SetIndexEmptyValue(2, 0.0);
   SetIndexBuffer(3,DevsBuffer);
//----
   string short_name="BB-HL(" + Per + "," + DoubleToStr(nDev, 1) + ")";
   IndicatorShortName(short_name);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double d1, d2, ma;
//---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit=Bars - counted_bars;
//---- simple moving average and deviations
   for(int i=limit; i>=0; i--)
     {
      ma=iMA(NULL, 0, Per, 0, MODE_SMA, PRICE_MEDIAN, i);
      d1=High[i] - ma;
      d1*=d1;
      d2=Low[i] - ma;
      d2*=d2;
      MABuffer[i]=ma;
      DevsBuffer[i]=MathMax(d1, d2);
     }
//---- compute the bands
   UpperBandBuffer[limit]=0.0;
   LowerBandBuffer[limit]=0.0;
   for(i=limit; i>=0; i--)
     {
      d1=0;
      for(int j=i; j<i+Per; j++)
         d1+=DevsBuffer[j];
      d1/=Per;
      d2=nDev * MathSqrt(d1);
      ma=MABuffer[i];
      UpperBandBuffer[i]=ma + d2;
      LowerBandBuffer[i]=ma - d2;
     }
   return(0);
  }
//+------------------------------------------------------------------+

Comments