Author: Pedro Puado
Flat

This script is designed as a custom technical analysis tool for trading platforms. Its primary purpose is to measure and visualize price volatility trends by transforming raw market data into a normalized percentage scale.

Core Logic Overview

The script operates through a multi-step calculation process that moves from standard market movements to a specialized volatility profile:

1. Volatility Foundation The tool begins by calculating the Standard Deviation of the closing prices over a specific moving window. This provides a base measurement of how much the price is "stretching" or fluctuating away from its average at any given moment.

2. Smoothing and Averaging To ensure the data is readable and not overly erratic, the script performs a secondary layer of processing. It takes the results from the first step and calculates a custom weighted average. This helps filter out "noise" and focuses on the underlying strength of the price movements.

3. High and Low Range Detection The script looks back over a defined historical period (a specific number of previous bars) to identify two critical values:

  • The Highest Peak reached within that timeframe.
  • The Lowest Trough reached within that timeframe.

4. Normalization (The Final Result) Finally, the script takes the current volatility value and compares it against the high and low boundaries identified in the previous step. It converts this relationship into a percentage.

Practical Interpretation

Instead of showing you raw price numbers, the final output on your chart represents where the current volatility stands relative to its recent history. By displaying this as a percentage (ranging from 0% to 100%), it allows traders to see at a glance if the market is currently experiencing extreme fluctuations compared to the recent past, or if the market is remaining relatively calm.

Indicators Used
Standard Deviation indicator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Flat
//+------------------------------------------------------------------+
//|                                                         Flat.mq4 |
//|                                                      Pedro Puado |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Pedro Puado"
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Green
//---- input parameters
extern int       MA=20;
extern int       HLRef=100;
extern int       MaxBars=1000;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(3);
   IndicatorShortName("Flat");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer3);

   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//---- 
   int i=Bars-1;
   if(counted_bars>0) i=Bars-counted_bars+0;
   i = MathMax(i, MaxBars);
   int p = i;
   double m1, m2, n;
   while (i>=0)
   {
      ExtMapBuffer1[i] = iStdDev(NULL,0,MA,MODE_SMA,0,PRICE_CLOSE,i);
      i--;
   }
   
   int x = 0;
   double s, s1, nL, nH;   
   while (p >= 0)
   {
      s = 0;
      x = 0;
      for (x=0; x < MA+0; x++)
      {
         s += ExtMapBuffer1[p+x];  
      }
      s1 = s/MA;
      
      
      s = 0;
      x = 0;
      for (x=0; x < MA+0; x++)
      {
         s += MathAbs((ExtMapBuffer1[p+x]-s1)*2);  
      }  
      
      ExtMapBuffer2[p] = MathSqrt(s/MA);
      nH = HVal(ExtMapBuffer2, HLRef, p);
      nL = LVal(ExtMapBuffer2, HLRef, p);
      ExtMapBuffer3[p] = ((ExtMapBuffer2[p] - nL)/ (nH-nL))*100;
      
      p--;
   }   

//----
   return(0);
  }
//+------------------------------------------------------------------+

double HVal( double aA[], int p, int shift)
{
   int i;
   double rtn = 0, pr = 0;
   for (i = 0; i < p; i++)
   {
      if (aA[i+shift] > pr)
      {
         pr = aA[i+shift];
      }
   }
   return (pr);
}

double LVal( double aA[], int p, int shift)
{
   int i;
   double rtn = 0, pr = 99999;
   for (i = 0; i < p; i++)
   {
      if (aA[i+shift] < pr)
      {
         pr = aA[i+shift];
      }
   }
   return (pr);
}

Comments