Author: Copyright � 2005, MetaQuotes Software Corp.
StdDev

This tool is designed to help traders measure market volatility by calculating the "Standard Deviation" of price movements. In simple terms, while many indicators tell you where the price is going, this one tells you how much the price is swinging or "shaking."

How the Logic Works

The script analyzes the history of a financial instrument (like a currency pair or stock) and performs the following steps to generate its visual data:

1. Defining the Timeframe (Period) The tool looks back at a specific number of past price candles (for example, the last 20 periods). By looking at a fixed window of time, it creates a consistent "snapshot" of recent market behavior.

2. Establishing a Baseline Before it can measure volatility, the script first identifies a "Moving Average." This acts as the central baseline or the "average price" for that specific timeframe. It helps the tool understand what the "normal" price should be before it starts measuring the deviations.

3. Measuring the Variance The core logic then calculates how far away each individual price point in that period is from the moving average.

  • If prices are hugging the average line closely, the volatility is low.
  • If prices are jumping wildly far above or below the average, the volatility is high.

The script takes these differences, squares them (to ensure they are all treated as positive numbers), adds them together, and finds the average of those differences. Finally, it performs a square root calculation to bring the number back into a readable scale.

4. Customizing the Price Source The logic is flexible regarding which part of the candle it watches. Depending on your settings, it can calculate these movements based on:

  • The closing price (the most common method)
  • The opening price
  • The highest or lowest points reached during that timeframe
  • A custom average of several different price points combined

What This Means for the User

Instead of just seeing a line that goes up and down, this indicator provides a dedicated window that visualizes "market tension." When the line on the chart spikes upward, it signals that the market is becoming unpredictable and volatile. When the line stays flat or low, it suggests a period of stability or consolidation.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
StdDev
//+------------------------------------------------------------------+
//|                                           Standard Deviation.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_separate_window
#property indicator_minimum 0
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int ExtStdDevPeriod=20;
extern int ExtStdDevMAMethod=0;
extern int ExtStdDevAppliedPrice=0;
extern int ExtStdDevShift=0;
//---- buffers
double ExtStdDevBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string sShortName;
//---- indicator buffer mapping
   SetIndexBuffer(0,ExtStdDevBuffer);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
//---- line shifts when drawing
   SetIndexShift(0,ExtStdDevShift);   
//---- name for DataWindow and indicator subwindow label
   sShortName="StdDev("+ExtStdDevPeriod+")";
   IndicatorShortName(sShortName);
   SetIndexLabel(0,sShortName);
//---- first values aren't drawn
   SetIndexDrawBegin(0,ExtStdDevPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Standard Deviation                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    i,j,nLimit,nCountedBars;
   double dAPrice,dAmount,dMovingAverage;  
//---- insufficient data
   if(Bars<=ExtStdDevPeriod) return(0);
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();
//----Standard Deviation calculation
   i=Bars-ExtStdDevPeriod-1;
   if(nCountedBars>ExtStdDevPeriod) 
      i=Bars-nCountedBars;  
   while(i>=0)
     {
      dAmount=0.0;
      dMovingAverage=iMA(NULL,0,ExtStdDevPeriod,0,ExtStdDevMAMethod,ExtStdDevAppliedPrice,i);
      for(j=0; j<ExtStdDevPeriod; j++)
        {
         dAPrice=GetAppliedPrice(ExtStdDevAppliedPrice,i+j);
         dAmount+=(dAPrice-dMovingAverage)*(dAPrice-dMovingAverage);
        }
      ExtStdDevBuffer[i]=MathSqrt(dAmount/ExtStdDevPeriod);
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAppliedPrice(int nAppliedPrice, int nIndex)
  {
   double dPrice;
//----
   switch(nAppliedPrice)
     {
      case 0:  dPrice=Close[nIndex];                                  break;
      case 1:  dPrice=Open[nIndex];                                   break;
      case 2:  dPrice=High[nIndex];                                   break;
      case 3:  dPrice=Low[nIndex];                                    break;
      case 4:  dPrice=(High[nIndex]+Low[nIndex])/2.0;                 break;
      case 5:  dPrice=(High[nIndex]+Low[nIndex]+Close[nIndex])/3.0;   break;
      case 6:  dPrice=(High[nIndex]+Low[nIndex]+2*Close[nIndex])/4.0; break;
      default: dPrice=0.0;
     }
//----
   return(dPrice);
  }
//+------------------------------------------------------------------+

Comments