Damiani_Volatmeter_v1

Author: Copyright 2018, MetaQuotes Software Corp.
Indicators Used
Indicator of the average true rangeStandard Deviation indicator
1 Views
0 Downloads
0 Favorites
Damiani_Volatmeter_v1
ÿþ//+------------------------------------------------------------------+

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

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot M

#property indicator_label2  "M"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrOrangeRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- enums



//--- input parameters

input uint                 InpViscosity      =  7;             // Viscosity

input uint                 InpSedimentation  =  50;            // Sedimentation

input double               InpThreshold      =  1.1;           // Threshold

input bool    InpLagSuppressor  =  true;     // 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,8);

//--- 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[])

  {



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



   int limit=MathMin(rates_total-prev_calculated, rates_total-period_max-4);

   

   if(limit>=rates_total-period_max-4)

     {

      ArrayInitialize(BufferP,0);

      ArrayInitialize(BufferM,0);

      ArrayInitialize(BufferATRV,0);

      ArrayInitialize(BufferATRS,0);

      ArrayInitialize(BufferDevV,0);

      ArrayInitialize(BufferDevS,0);

     }





   int count=(limit>=rates_total-period_max-4 ? rates_total : limit+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;





      

   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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---