BandsMA_2dev

Download script
Author: Copyright � 2005, Robert Hill
BandsMA_2dev

This script is a customized technical analysis tool designed to project "Bands" around price movements on a trading chart. While it functions similarly to the well-known Bollinger Bands, it offers significantly higher flexibility by allowing users to customize the mathematical foundation of the central trend line and the range of the outer boundaries.

Core Functionality

The primary logic of this tool is divided into three distinct layers:

1. Customizable Trend Tracking (The Moving Average) At the heart of the indicator is a moving average, which determines the "center" of your price action. Unlike standard tools that only offer one type of calculation, this script allows you to choose from five different mathematical smoothing methods:

  • Simple Moving Average (SMA)
  • Exponential Moving Average (EMA)
  • Smoothed Moving Average (SMMA)
  • Linear Weighted Moving Average (LWMA)
  • Least Squares Moving Average (LSMA)

Additionally, the tool allows you to choose which specific price data point the average should follow (such as the open, high, low, close, or various weighted averages of those prices).

2. Dynamic Volatility Bands Once the central trend line is established, the script calculates two sets of "Bands" (upper and lower boundaries) based on standard deviation. Standard deviation measures how much the price fluctuates away from the average.

  • The script calculates these fluctuations by looking at the difference between the current price and the center line over a specific period.
  • It then squares these differences, averages them, and takes the square root to determine the "spread."

3. Dual-Layer Deviation Display A unique feature of this script is its ability to display two different sets of bands simultaneously. This allows traders to see two different levels of volatility at once:

  • Band Set 1: Uses a primary deviation value (e.g., 1.0) to create a tighter range around the price.
  • Band Set 2: Uses a secondary deviation value (e.g., 2.5) to create a wider, broader range.

How it Works for the User

When you apply this to a chart, the script automatically processes historical data to draw these lines. If you enable the "MidLine" option, you will see the central moving average clearly; if disabled, only the volatility bands will remain visible.

In essence, this tool provides a comprehensive visual map of price "extremes." By showing two different levels of deviation simultaneously, it helps users identify both immediate price fluctuations and broader, more significant market swings at a single glance.

Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
BandsMA_2dev
//+------------------------------------------------------------------+
//|                                                 BandsMA_2dev.mq4 |
//|                                     Copyright © 2005, RobertHill |
//|                                              http://www.fxdd.com |
//|                                                                  |
//| Modified Bollinger Bands by Robert Hill to use any standard MA   |
//| as well as LSMA to replace SMA                                   |
//| Two standard deviation lines are displayed based on user input   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Robert Hill"
#property link      "http://www.fxdd.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
#property indicator_color4 Red
#property indicator_color5 Red
//---- indicator parameters
extern string  m = "--Moving Average Types--";
extern string  m0 = " 0 = SMA";
extern string  m1 = " 1 = EMA";
extern string  m2 = " 2 = SMMA";
extern string  m3 = " 3 = LWMA";
extern string  m4 = " 4 = LSMA";
extern int     MA_Type=0;
extern string  p = "--Applied Price Types--";
extern string  p0 = " 0 = close";
extern string  p1 = " 1 = open";
extern string  p2 = " 2 = high";
extern string  p3 = " 3 = low";
extern string  p4 = " 4 = median(high+low)/2";
extern string  p5 = " 5 = typical(high+low+close)/3";
extern string  p6 = " 6 = weighted(high+low+close+close)/4";
extern int     MA_AppliedPrice = 0;
extern string  b = "--Bands Inputs--";
extern int     BandsPeriod=20;
extern int     BandsShift=0;
extern double  BandsDeviations1 = 1.0;
extern double  BandsDeviations2 = 2.5;
extern bool    DisplayMidLine = true;

//---- buffers
double MovingBuffer[];
double UpperBuffer1[];
double LowerBuffer1[];
double UpperBuffer2[];
double LowerBuffer2[];

//---- variables

int    MA_Mode;
string strMAType;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpperBuffer1);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,LowerBuffer1);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,UpperBuffer2);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,LowerBuffer2);
//----
   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
   SetIndexDrawBegin(3,BandsPeriod+BandsShift);
   SetIndexDrawBegin(4,BandsPeriod+BandsShift);
//----

switch (MA_Type)
   {
      case 1: strMAType="EMA"; MA_Mode=MODE_EMA; break;
      case 2: strMAType="SMMA"; MA_Mode=MODE_SMMA; break;
      case 3: strMAType="LWMA"; MA_Mode=MODE_LWMA; break;
      case 4: strMAType="LSMA"; break;
      default: strMAType="SMA"; MA_Mode=MODE_SMA; break;
   }
   IndicatorShortName( strMAType+ " (" +BandsPeriod + ") ");

   return(0);
  }

//+------------------------------------------------------------------+
//| LSMA with PriceMode                                              |
//| LSMA - Least Squares Moving Average function calculation         |
//|        Indicator plots the end of the linear regression line     |
//| PrMode  0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2,    |
//| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4  |
//+------------------------------------------------------------------+

double LSMA(int TimeFrame, int Rperiod, int prMode, int shift)
{
   int i, myShift;
   double sum, pr;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     myShift = length - i + shift;
     switch (prMode)
     {
     case 0: pr = iClose(NULL,TimeFrame,myShift);break;
     case 1: pr = iOpen(NULL,TimeFrame,myShift);break;
     case 2: pr = iHigh(NULL,TimeFrame,myShift);break;
     case 3: pr = iLow(NULL,TimeFrame,myShift);break;
     case 4: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift))/2;break;
     case 5: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/3;break;
     case 6: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/4;break;
     }
     tmp = ( i - lengthvar)*pr;
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    wt = MathFloor(wt/Point)*Point;
    
    return(wt);
}

//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation1, 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;
         UpperBuffer1[Bars-i]=EMPTY_VALUE;
         LowerBuffer1[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++)
   {
     if (MA_Type == 4)
      MovingBuffer[i]=LSMA(0, BandsPeriod, MA_AppliedPrice,i);
     else
      MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MA_Mode,MA_AppliedPrice,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--;
        }
      deviation1=BandsDeviations1*MathSqrt(sum/BandsPeriod);
      deviation2=BandsDeviations2*MathSqrt(sum/BandsPeriod);
      UpperBuffer1[i]=oldval+deviation1;
      LowerBuffer1[i]=oldval-deviation1;
      UpperBuffer2[i]=oldval+deviation2;
      LowerBuffer2[i]=oldval-deviation2;
      i--;
     }
   if (DisplayMidLine == false)
   {
      for(i=1;i<=Bars;i++)
         MovingBuffer[i]=EMPTY_VALUE;
   }
   //----
   return(0);
  }
//+------------------------------------------------------------------+

Comments