StdDivBands

Download script
Author: Copyright � 2006, Ronald Verwer @ Forex MetaSoft
StdDivBands

This tool is designed to provide visual boundaries on a financial chart to help traders understand price volatility and identify potential market extremes. By analyzing historical price movements, the indicator creates a "channel" that fluctuates based on how much the market is swinging.

Core Logic and Functionality

The script operates by calculating three distinct components on your chart:

1. The Baseline (Middle Band) The foundation of the tool is a moving average. Rather than looking at just the most recent price, it calculates an average value over a specific period (defined by the user). This represents the "equilibrium" or the trend of the market during that timeframe.

2. Volatility Measurement (Standard Deviation) The script looks at how far the actual closing prices stray from that baseline. It calculates the variance—mathematically determining if the price is behaving normally or if it is moving in an unusually large direction.

3. The Dynamic Bands (Upper and Lower) Using the volatility measurement, the script projects two boundaries:

  • The Upper Band: This is calculated by adding a specific multiple of the deviation to the baseline.
  • The Lower Band: This is calculated by subtracting that same multiple from the baseline.

How It Works in Practice

Because the bands are based on "Standard Deviation," they are not static lines. They expand when the market becomes highly volatile (large price swings) and contract when the market becomes calm orrange-side.

  • Expansion: When prices move rapidly, the bands widen to account for the increased uncertainty and movement range.
  • Contraction: During periods of consolidation or low activity, the bands tighten toward the center.

User Objectives

The primary purpose of this logic is to assist in identifying "overextended" market conditions. When the price pushes toward or outside of these magenta bands, it suggests that the move may be reaching an extreme relative to recent history, providing a visual cue for potential reversals or trend exhaustion.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
StdDivBands
//+------------------------------------------------------------------+
//|                                                  StdDivBands.mq4 |
//|                 Copyright © 2006, Ronald Verwer @ Forex MetaSoft |
//|                                     http://www.forexmetasoft.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer @ Forex MetaSoft"
#property link      "http://www.forexmetasoft.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Magenta
#property indicator_color2 Magenta
#property indicator_width1 2
#property indicator_width2 2

extern int    StdDivPeriod          =  20;
extern double StdDeviation          = 2.0;
extern int    Shift                 =   0;
extern int    Applied_Price         =   5;

double MiddleBuffer[];
double UpperBand[];
double LowerBand[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
   {
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,UpperBand);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,LowerBand);
   SetIndexBuffer(2,MiddleBuffer);

   SetIndexDrawBegin(0,StdDivPeriod+Shift);
   SetIndexDrawBegin(1,StdDivPeriod+Shift);
   SetIndexDrawBegin(2,StdDivPeriod+Shift);

   return(0);
   }

//+------------------------------------------------------------------+
//| Standard Deviation Bands                                         |
//+------------------------------------------------------------------+

int start()
   {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation;
   double sum,oldval,newres;

   if(Bars<=StdDivPeriod) return(0);

   if(counted_bars<1)
      for(i=1;i<=StdDivPeriod;i++)
         {
         MiddleBuffer[Bars-i]=EMPTY_VALUE;
         UpperBand[Bars-i]=EMPTY_VALUE;
         LowerBand[Bars-i]=EMPTY_VALUE;
         }

   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      MiddleBuffer[i]=iMA(NULL,0,StdDivPeriod,Shift,MODE_SMA,Applied_Price,i);

   i=Bars-StdDivPeriod+1;
   if(counted_bars>StdDivPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
      {
      sum=0.0;
      k=i+StdDivPeriod-1;
      oldval=MiddleBuffer[i];
      while(k>=i)
         {
         newres=Close[k]-oldval;
         sum+=newres*newres;
         k--;
         }
      deviation=StdDeviation*MathSqrt(sum/StdDivPeriod);
      UpperBand[i]=oldval+deviation;
      LowerBand[i]=oldval-deviation;
      i--;
      }

   return(0);
   }

//+------------------------------------------------------------------+

Comments