Volatility_Ratio2

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Volatility_Ratio2
ÿþ//+------------------------------------------------------------------+

//|                                            Volatility_Ratio2.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 "Volatility Ratio2 oscillator"

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   1

//--- plot VR

#property indicator_label1  "VR2"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDeepPink

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1, // Yes

   INPUT_NO    =  0  // No

  };

//--- input parameters

input uint              InpPeriod      =  14;         // Period

input double            InpThreshold   =  2.0;        // Threshold

input ENUM_INPUT_YES_NO InpUseAlert    =  INPUT_YES;  // Show alerts

//--- indicator buffers

double         BufferVR[];

double         BufferTR[];

double         BufferEMATR[];

//--- global variables

double         threshold;

int            period_vr;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_vr=int(InpPeriod<1 ? 1 : InpPeriod);

   threshold=InpThreshold;

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferVR,INDICATOR_DATA);

   SetIndexBuffer(1,BufferTR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,BufferEMATR,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Volatility Ratio2 ("+(string)period_vr+","+(string)threshold+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,1);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,threshold);

   IndicatorSetString(INDICATOR_LEVELTEXT,0,"Threshold");

   IndicatorSetDouble(INDICATOR_MINIMUM,0);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferVR,true);

   ArraySetAsSeries(BufferTR,true);

   ArraySetAsSeries(BufferEMATR,true);

//---

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

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

   if(rates_total<2) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferVR,EMPTY_VALUE);

      ArrayInitialize(BufferTR,0);

      ArrayInitialize(BufferEMATR,0);

     }



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

   static datetime last_alert=0;

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

     {

      BufferTR[i]=fmax(fmax(high[i]-low[i],fabs(high[i]-close[i+1])),fabs(close[i+1]-low[i]));

      BufferEMATR[i]=EMA(rates_total,BufferTR[i],BufferEMATR[i+1],period_vr,i);

      BufferVR[i]=(BufferEMATR[i]!=0 ? BufferTR[i]/BufferEMATR[i] : 0);

      //--- alerts

      if(InpUseAlert && i==0 && time[0]>last_alert && BufferVR[0]>threshold)

        {

         Alert(Symbol()+","+TimeframeToString(Period())+": Volatility Ratio > "+(string)threshold+")");

         last_alert=TimeCurrent();

        }

     }

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

   return(rates_total);

  }

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

//| EMA                                                              |

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

double EMA(const int rates_total,const double price,const double prev_value,const int period,const int shift)

  {

   return (shift<rates_total-2 ? prev_value+2.0/(1+period)*(price-prev_value) : price);

  }

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

//| Timeframe to string                                              |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  { 

   return StringSubstr(EnumToString(timeframe),7);

  }

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

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