Bands_v1
Download scriptThis script functions as a technical analysis tool designed to help traders visualize price volatility and identify potential "overbought" or "oversold" conditions on a financial chart. It calculates and draws three distinct lines: a center moving average and two outer boundary lines, collectively known as Bollinger Bands.
How the Logic Works
The script processes market data in real-time using three primary components:
1. The Center Line (Simple Moving Average) The core of the indicator starts by calculating a "Moving Average." It looks back at a specific number of recent price points (defined by the Period) and averages their closing prices. This creates a smooth line that represents the general trend of the asset, filtering out minor market noise to show the underlying direction.
2. Measuring Volatility (Standard Deviation) To determine how far the outer bands should be from the center line, the script calculates "volatility." It measures how much the price has fluctuated away from the moving average over the specified period. If the price is swinging wildly, the volatility measurement increases; if the price is relatively stable, the measurement decreases.
3. The Upper and Lower Boundaries Finally, the script uses the volatility measurement to plot two boundary lines:
- The Upper Band: This is calculated by adding a specific multiple of the volatility to the center moving average.
- The Lower Band: This is calculated by subtracting that same multiple from the center moving average.
Practical Application
By combining these three elements, the script creates a "channel" around the price. In theory, because most price action stays within these bands, a price that touches or breaks through the upper band may be considered overextended (overbought), while a price touching the lower band may be considered undervalued (oversold). The width of the channel expands and contracts automatically as market volatility changes, providing a dynamic view of market sentiment.
//+------------------------------------------------------------------+
//| Bands.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MovingBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpperBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,LowerBuffer);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
int i,k,counted_bars=IndicatorCounted();
double deviation;
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;
}
//----
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;
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------
Comments