Damiani_Volatmeter

Download script
Author: Copyright 2018, MetaQuotes Software Corp.

The Damiani Volatmeter is a custom technical analysis indicator designed to provide traders with a visual representation of market volatility and price momentum. It works by calculating the relationship between different types of price movement—specifically comparing "raw" market variance against standardized averages.

Core Logic & Functionality

The script operates by processing two primary mathematical components:

1. Volatility Comparison (The P Line) The indicator uses the Average True Range (ATR), which measures how much a price moves on average over a specific period. It calculates two different versions of this:

  • A "Fast" version based on the Viscosity setting.
  • A "Slow" version based on the Sedimentation setting.

By dividing the fast volatility by the slow volatility, the script determines the current speed of price movement relative to its recent history. To make this data more responsive and less "laggy," the logic includes an optional Lag Suppressor. This feature looks at neighboring price points to smooth out sudden jumps while maintaining the core trend of the volatility shift.

2. Threshold Mapping (The M Line) The script also utilizes Standard Deviation, which measures how much prices are deviating from the mean (average) price. It calculates this across the same "Fast" and "Slow" windows mentioned above.

  • It takes a user-defined Threshold (a baseline value).
  • It compares the fast standard deviation to the slow standard deviation.
  • The result is mapped against the threshold to create a secondary reading that helps identify if the current market "noise" is expanding or contracting relative to the baseline.

User Experience & Visuals

When you apply this script to a chart, it generates two distinct lines in a separate window:

  • The P Line (Green): Represents the normalized volatility ratio. It shows you how quickly the market is moving right now compared to its historical "speed."
  • The M Line (Red): Represents the deviation from the established threshold. This helps traders understand if the current price action is becoming an outlier or staying within normal bounds.

Key Parameters for Customization

  • Viscosity & Sedimentation: These control the "memory" of the indicator. Lower numbers make the lines react quickly to new data; higher numbers make them smoother and slower to change.
  • Threshold: This acts as your "normal" baseline. Adjusting this changes where the M line sits relative to the price action.
  • Lag Suppressor: When enabled, this attempts to refine the P line by factoring in slight price differences between candles, making the indicator feel more fluid and less like it is "trailing" behind the actual market movement.

In summary, the Damiani Volatmeter helps a trader distinguish between high-intensity price spikes and sustained trends by comparing different layers of market volatility against one another.

Price Data Components
Indicators Used
Indicator of the average true rangeStandard Deviation indicator
17 Views
0 Downloads
0 Favorites
Damiani_Volatmeter
ÿþ//+------------------------------------------------------------------+

//|                                           Damiani_Volatmeter.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Damiani volatmeter oscillator"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   2

//--- plot P

#property indicator_label1  "P"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot M

#property indicator_label2  "M"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1, // Yes

   INPUT_NO    =  0  // No

  };

//--- input parameters

input uint                 InpViscosity      =  7;             // Viscosity

input uint                 InpSedimentation  =  50;            // Sedimentation

input double               InpThreshold      =  1.1;           // Threshold

input ENUM_INPUT_YES_NO    InpLagSuppressor  =  INPUT_YES;     // Lag suppressor

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price

//--- indicator buffers

double         BufferP[];

double         BufferM[];

double         BufferATRV[];

double         BufferATRS[];

double         BufferDevV[];

double         BufferDevS[];

//--- global variables

double         threshold;

int            viscosity;

int            sedimentation;

int            handle_atrv;

int            handle_atrs;

int            handle_devv;

int            handle_devs;

int            period_max;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   viscosity=int(InpViscosity<1 ? 1 : InpViscosity);

   sedimentation=int(InpSedimentation<2 ? 2 : InpSedimentation);

   period_max=fmax(viscosity,sedimentation);

   threshold=InpThreshold;

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferM,INDICATOR_DATA);

   SetIndexBuffer(2,BufferATRV,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferATRS,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferDevV,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferDevS,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Damiani volatmeter ("+(string)viscosity+","+(string)sedimentation+","+DoubleToString(threshold,1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferP,true);

   ArraySetAsSeries(BufferM,true);

   ArraySetAsSeries(BufferATRV,true);

   ArraySetAsSeries(BufferATRS,true);

   ArraySetAsSeries(BufferDevV,true);

   ArraySetAsSeries(BufferDevS,true);

//--- create MA's handles

   ResetLastError();

   handle_atrv=iATR(NULL,PERIOD_CURRENT,viscosity);

   if(handle_atrv==INVALID_HANDLE)

     {

      Print("The iATR(",(string)viscosity,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_atrs=iATR(NULL,PERIOD_CURRENT,sedimentation);

   if(handle_atrs==INVALID_HANDLE)

     {

      Print("The iATR(",(string)sedimentation,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_devv=iStdDev(NULL,PERIOD_CURRENT,viscosity,0,MODE_LWMA,InpAppliedPrice);

   if(handle_devv==INVALID_HANDLE)

     {

      Print("The iStdDev(",(string)viscosity,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_devs=iStdDev(NULL,PERIOD_CURRENT,sedimentation,0,MODE_LWMA,InpAppliedPrice);

   if(handle_devs==INVALID_HANDLE)

     {

      Print("The iStdDev(",(string)sedimentation,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period_max,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-period_max-4;

      ArrayInitialize(BufferP,0);

      ArrayInitialize(BufferM,0);

      ArrayInitialize(BufferATRV,0);

      ArrayInitialize(BufferATRS,0);

      ArrayInitialize(BufferDevV,0);

      ArrayInitialize(BufferDevS,0);

     }

//--- >43>B>2:0 40==KE

   int count=(limit>1 ? rates_total : 1),copied=0;

   copied=CopyBuffer(handle_atrs,0,0,count,BufferATRS);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_atrv,0,0,count,BufferATRV);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_devs,0,0,count,BufferDevS);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_devv,0,0,count,BufferDevV);

   if(copied!=count) return 0;



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double ATR_V=BufferATRV[i];

      double ATR_S=BufferATRS[i];

      double StdDev_V=BufferDevV[i];

      double StdDev_S=BufferDevS[i];



      if(ATR_S!=0. && StdDev_S!=0.)

        {

         double s1=BufferP[i+1];

         double s3=BufferP[i+3];

         BufferP[i]=(InpLagSuppressor ? ATR_V/ATR_S+(s1-s3)/2.0 : ATR_V/ATR_S);

         BufferM[i]=threshold-StdDev_V/StdDev_S;

        }

     }



//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

Comments