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

This technical indicator is designed to provide traders with a visual representation of price volatility and trend direction by calculating specific "bands" around a moving average. By analyzing the relationship between the high and low prices of an asset over a set period, it helps identify whether the market is currently in a state of standard movement or experiencing significant expansion.

Core Logic Overview

The script operates through a multi-step mathematical process to transform raw price data into three distinct visual components: a central moving average and two outer boundary lines (upper and lower bands).

1. Establishing the Baseline (Moving Average) The first step the logic performs is calculating a Simple Moving Average (SMA). This acts as the "anchor" or the baseline for the indicator. It takes the median prices over a user-defined number of previous candles (the Period) to smooth out short-term price "noise," helping to show the underlying trend of the market.

2. Measuring Price Deviation Instead of simply looking at how far the price is from the average, this script calculates the intensity of the price swings. It measures the distance between the highest and lowest prices of each candle and compares those points to the Moving Average. By squaring these distances, the logic ensures that large moves are weighted more heavily than small fluctuations, providing a clearer picture of true market volatility.

3. Dynamic Band Calculation The indicator then uses these deviation values to create the upper and lower bands.

  • Standardization: It calculates an average of the deviations over the same period used for the moving average.
  • Volatility Adjustment: It applies a multiplier (the Deviation factor) to this average. This allows the bands to expand significantly when the market is highly volatile and contract inward when the market is trading calmly.
  • Final Positioning: The final Upper Band is created by adding this calculated volatility to the Moving Average, while the Lower Band is created by subtracting it.

How it Appears on a Chart

The result is a visual "channel." When the price stays within these bands, it suggests the market is behaving normally relative to its recent history. When the price breaks out of or touches these bands, it indicates that the current price movement is statistically significant compared to the recent average volatility, which can serve as a signal for potential trend reversals or high-momentum breakouts.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
BB - HL_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/


//|                                                        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