Author: Pedro Puado
Flat_v1

This technical indicator is designed to provide traders with a visual representation of market volatility and price movement relative to its historical extremes. By isolating specific statistical behaviors, it helps identify where the current market "energy" sits within a larger timeframe.

Core Logic Overview

The script operates through a three-stage calculation process:

1. Establishing a Volatility Baseline The tool begins by analyzing the standard deviation of the closing prices over a specific period (defined by the Moving Average setting). This represents the "normal" amount of fluctuation in the market. Instead of just looking at the price itself, the script focuses on how much the price is deviating from its recent average.

2. Measuring Relative Intensity Once the baseline volatility is established, the script calculates a secondary value that measures the magnitude of these fluctuations. It essentially takes the standard deviation data and treats it as a new data set. This allows the indicator to smooth out "noise" while highlighting significant changes in market activity.

3. Normalizing to a Percentage Scale (The "Flat" Output) The final step—and what is actually displayed on your chart—is a normalization process. The script looks back at a large historical window (the High/Low Reference) to find the highest and lowest volatility points reached during that time. It then takes the current intensity level and calculates its position between those two extremes as a percentage.

How to Interpret the Results

The resulting line moves within a percentage range. This allows you to see, at a glance, how "stretched" the current market is:

  • High Percentage Levels: Indicate that current volatility is approaching the highest levels seen in the recent historical period.
  • Low Percentage Levels: Indicate that the market is relatively calm compared to its historical highs.
  • The Trend: By watching the movement of this line, traders can identify whether market activity is expanding or contracting relative to its own history, independent of the actual price direction.
13 Views
0 Downloads
0 Favorites
Flat_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                         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