ATM_DualBollingerBands
Download scriptThis script is designed for the MetaTrader platform to provide traders with a visual tool for analyzing price volatility and potential trend reversals. It functions by plotting two distinct sets of "Bollinger Bands" on top of a price chart, allowing for a more nuanced view of market movements than a standard single-band indicator.
Core Functionality
The primary purpose of this tool is to map out the "normal" range of price action. It identifies the average price over a specific timeframe and then calculates how far the price typically fluctuates away from that average. By doing this, it creates a visual "tunnel" or boundary on your chart.
How the Logic Works
The script follows a structured mathematical process to generate its visuals:
- Establishing the Baseline: The tool first calculates a Simple Moving Average (SMA). This represents the central trend of the price over a user-defined period (e.g., the last 20 bars). On the chart, this appears as the middle line of the bands.
- Measuring Volatility: Once the average is established, the script calculates the "deviation." It looks at how much the actual closing prices differ from that moving average. The further the price swings away from the average, the wider the bands become.
- Dual-Band System: Unlike standard indicators that only show one set of boundaries, this specific logic generates two separate layers of bands simultaneously:
- Inner Bands: These are calculated using a standard deviation setting to show the immediate, tighter range of price movement.
- Outer Bands: These use a larger deviation multiplier, creating a wider "envelope." This helps traders identify extreme overbought or oversold conditions where the price is pushing significantly outside of its normal behavior.
Visual Representation
When applied to a live chart, you will see five distinct lines:
- A central line representing the moving average.
- Two inner boundary lines (upper and lower) that define the standard volatility zone.
- Two outer boundary lines (upper and lower) that define the extreme volatility zones.
Practical Application for Traders
By viewing these dual bands, a user can better visualize market "exhaustion." When price action pushes toward or through the outer bands, it often suggests that the current trend is overextended. The presence of two sets of bands allows the trader to see not just where the price usually goes, but where it is reaching an extreme limit relative to its recent history.
//+--------------------------------------------------------------------+
//| Second Bollinger Band added by accrete ATM_DualBollingerBands.mq4 |
//| Copyright © 2005, Accrete LLC |
//| http://www.accrete.com |
//+--------------------------------------------------------------------+
#property copyright "Bollinger Band Indicator hack by Accrete"
#property link "http://www.accrete.com/"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 Aqua
#property indicator_color3 Aqua
#property indicator_color4 White
#property indicator_color5 White
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=1;
extern double BandsDeviations2=1.5;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double UpperBuffer2[];
double LowerBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,0,1);
SetIndexBuffer(0,MovingBuffer);
SetIndexStyle(1,DRAW_LINE,0,1);
SetIndexBuffer(1,UpperBuffer);
SetIndexStyle(2,DRAW_LINE,0,1);
SetIndexBuffer(2,LowerBuffer);
SetIndexStyle(3,DRAW_LINE,0,1);
SetIndexBuffer(3,UpperBuffer2);
SetIndexStyle(4,DRAW_LINE,0,1);
SetIndexBuffer(4,LowerBuffer2);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
SetIndexDrawBegin(3,BandsPeriod+BandsShift);
SetIndexDrawBegin(4,BandsPeriod+BandsShift);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
int i,k,counted_bars=IndicatorCounted();
double deviation;
double deviation2;
double sum,oldval,newres;
//----
if(Bars<=BandsPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=BandsPeriod;i++)
{
MovingBuffer[Bars-i]=EMPTY_VALUE;
UpperBuffer[Bars-i]=EMPTY_VALUE;
LowerBuffer[Bars-i]=EMPTY_VALUE;
UpperBuffer2[Bars-i]=EMPTY_VALUE;
LowerBuffer2[Bars-i]=EMPTY_VALUE;
}
//----
int limit=Bars-counted_bars;
if(counted_bars>0) limit++;
for(i=0; i<limit; i++)
MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
//----
i=Bars-BandsPeriod+1;
if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1;
while(i>=0)
{
sum=0.0;
k=i+BandsPeriod-1;
oldval=MovingBuffer[i];
while(k>=i)
{
newres=Close[k]-oldval;
sum+=newres*newres;
k--;
}
deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[i]=oldval+deviation;
LowerBuffer[i]=oldval-deviation;
deviation2=BandsDeviations2*MathSqrt(sum/BandsPeriod);
UpperBuffer2[i]=oldval+deviation2;
LowerBuffer2[i]=oldval-deviation2;
i--;
}
//----
Comment("");
return(0);
}
//+------------------------------------------------------------------+
Comments