Author: Copyright � 2005, David W. Thomas
BB - HL

This tool is a specialized technical analysis indicator designed to help traders visualize price movement and volatility on a chart. It functions by calculating a dynamic "envelope" around the current price, helping users identify whether the market is behaving normally or experiencing extreme fluctuations.

Core Logic and Components

The script operates through three primary stages of calculation:

1. Establishing the Baseline (Moving Average) The foundation of the indicator is a Simple Moving Average (SMA). This represents the "average" price of an asset over a specific number of previous candles (defined by the user as the Period). Think of this as the equilibrium point; it smooths out minor market noise to show the general direction of the trend.

2. Measuring Volatility (Deviation Calculation) Unlike standard indicators that only look at the average price, this script looks at how far the High and Low prices of each candle stray from that average baseline.

  • It calculates the distance between the highest price reached and the average.
  • It calculates the distance between the lowest price reached and the average.
  • It identifies the maximum of these two values to determine the specific "deviation" for every individual bar.

3. Constructing the Bands The final step involves taking those individual deviations and averaging them over the same period used for the baseline. This creates a standardized measure of current market volatility. The script then uses this measurement to draw two outer lines:

  • Upper Band: The average price plus a multiple of the volatility.
  • Lower Band: The average price minus a multiple of the volatility.

How to Interpret the Results

The visual output provides three distinct lines on your chart. The central line represents the trend, while the upper and lower bands act as boundaries.

When the price moves significantly outside of these bands, it often suggests that the market is overextended—meaning the price has moved further than usual relative to its recent history. Traders use these boundaries to identify potential "overbought" or "oversold" conditions, where the price might be due for a correction back toward the middle average line.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
15 Views
0 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