VARMA_Signal_Line_Overlay

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
VARMA_Signal_Line_Overlay
ÿþ//+------------------------------------------------------------------+

//|                                    VARMA_Signal_Line_Overlay.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 "VARMA Signal Line Overlay Color Candle indicator"

#property indicator_chart_window

#property indicator_buffers 15

#property indicator_plots   3

//--- plot UP

#property indicator_label1  "Up direction"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DN

#property indicator_label2  "Down direction"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Candle

#property indicator_label3  "Open;High;Low;Close"

#property indicator_type3   DRAW_COLOR_CANDLES

#property indicator_color3  clrLimeGreen,clrDarkSeaGreen,clrOrangeRed,clrBurlyWood,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- enums

enum ENUM_MODE_IND

  {

   MODE_IND_FS,         // Fast vs Slow VARMA

   MODE_IND_SIG         // MA of VARMA signal

  };

//--- input parameters

input ENUM_MODE_IND        InpModeIND        =  MODE_IND_FS;   // Mode

input uint                 InpPeriodFast     =  9;             // Fast VARMA period

input uint                 InpSmoothingFast  =  2;             // Fast VARMA smoothing

input uint                 InpPeriodSlow     =  27;            // Slow VARMA period

input uint                 InpSmoothingSlow  =  5;             // Slow VARMA smoothing

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // VARMA applied price

input uint                 InpPeriodSignal   =  27;            // Signal MA period

input ENUM_MA_METHOD       InpMethodSignal   =  MODE_EMA;      // Signal MA method

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

//--- VARMA

double         BufferMA[];

double         BufferSig[];

//--- Fast/Slow CMO

double         BufferPOS[];

double         BufferNEG[];

double         BufferCMOF[];

double         BufferCMOS[];

//--- Fast/Slow VARMA

double         BufferVMAF[];

double         BufferVMAS[];

//--- global variables

int            period_max;

int            handle_ma;

//--- Fast VARMA

double         sc_fast;

int            period_fast;

int            smooth_fast;

//--- Slow VARMA

double         sc_slow;

int            period_slow;

int            smooth_slow;

//--- Signal ma

int            period_sig;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

//--- Fast VARMA

   period_fast=int(InpPeriodFast<1 ? 1 : InpPeriodFast);

   smooth_fast=int(InpSmoothingFast<1 ? 1 : InpSmoothingFast);

   sc_fast=2.0/(smooth_fast+1.0);

//--- Slow VARMA

   period_slow=int(InpPeriodSlow<1 ? 1 : InpPeriodSlow);

   smooth_slow=int(InpSmoothingSlow<1 ? 1 : InpSmoothingSlow);

   sc_slow=2.0/(smooth_slow+1.0);

//--- Signal

   period_sig=int(InpPeriodSignal<1 ? 1 : InpPeriodSignal);

   period_max=fmax(smooth_fast,smooth_slow);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferCandleO,INDICATOR_DATA);

   SetIndexBuffer(3,BufferCandleH,INDICATOR_DATA);

   SetIndexBuffer(4,BufferCandleL,INDICATOR_DATA);

   SetIndexBuffer(5,BufferCandleC,INDICATOR_DATA);

   SetIndexBuffer(6,BufferColors,INDICATOR_COLOR_INDEX);

//--- VARMA

   SetIndexBuffer(7,BufferMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferSig,INDICATOR_CALCULATIONS);

//--- Fast/Slow CMO

   SetIndexBuffer(9,BufferPOS,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferNEG,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,BufferCMOF,INDICATOR_CALCULATIONS);

   SetIndexBuffer(12,BufferCMOS,INDICATOR_CALCULATIONS);

//--- Fast/Slow VARMA

   SetIndexBuffer(13,BufferVMAF,INDICATOR_CALCULATIONS);

   SetIndexBuffer(14,BufferVMAS,INDICATOR_CALCULATIONS);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,32);

   PlotIndexSetInteger(1,PLOT_ARROW,32);

//--- setting indicator parameters

   string label=(InpModeIND==MODE_IND_FS ? "Fast vs Slow VARMA" : "MA of VARMA signal");

   IndicatorSetString(INDICATOR_SHORTNAME,label);

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(2,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferCandleO,true);

   ArraySetAsSeries(BufferCandleH,true);

   ArraySetAsSeries(BufferCandleL,true);

   ArraySetAsSeries(BufferCandleC,true);

   ArraySetAsSeries(BufferColors,true);

//--- VARMA

   ArraySetAsSeries(BufferMA,true);

   ArraySetAsSeries(BufferSig,true);

//--- Fast/Slow CMO

   ArraySetAsSeries(BufferPOS,true);

   ArraySetAsSeries(BufferNEG,true);

   ArraySetAsSeries(BufferCMOF,true);

   ArraySetAsSeries(BufferCMOS,true);

//--- Fast/Slow VARMA

   ArraySetAsSeries(BufferVMAF,true);

   ArraySetAsSeries(BufferVMAS,true);

//--- create MA handles

   ResetLastError();

   handle_ma=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice);

   if(handle_ma==INVALID_HANDLE)

     {

      Print("The iMA(1) 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[])

  {

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

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<period_max+3) 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(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

      ArrayInitialize(BufferCandleO,EMPTY_VALUE);

      ArrayInitialize(BufferCandleH,EMPTY_VALUE);

      ArrayInitialize(BufferCandleL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,4);

      ArrayInitialize(BufferMA,0);

      ArrayInitialize(BufferSig,0);

      ArrayInitialize(BufferPOS,0);

      ArrayInitialize(BufferNEG,0);

      ArrayInitialize(BufferCMOF,0);

      ArrayInitialize(BufferCMOS,0);

      ArrayInitialize(BufferVMAF,0);

      ArrayInitialize(BufferVMAS,0);

     }

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

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

   copied=CopyBuffer(handle_ma,0,0,count,BufferMA);

   if(copied!=count) return 0;

//--- CMO

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

     {

      double diff=BufferMA[i]-BufferMA[i+1];

      if(diff>0)

        {

         BufferPOS[i]=diff;

         BufferNEG[i]=0;

        }

      else if(diff<0)

        {

         BufferPOS[i]=0;

         BufferNEG[i]=-diff;

        }

     }

//--- CMO

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

     {

      //--- Fast

      double fs1=SMA(rates_total,BufferPOS,period_fast,i);

      double fs2=SMA(rates_total,BufferNEG,period_fast,i);

      double fv=(fs1+fs2)*100.0;

      BufferCMOF[i]=(fv!=0 ? (fs1-fs2)/fv : 0);

      //--- Slow

      double ss1=SMA(rates_total,BufferPOS,period_slow,i);

      double ss2=SMA(rates_total,BufferNEG,period_slow,i);

      double sv=(ss1+ss2)*100.0;

      BufferCMOS[i]=(sv!=0 ? (ss1-ss2)/sv : 0);

     }

//--- VARMA

   for(int i=limit; i>=0; i--)

     {

      if(i==rates_total-2)

        {

         BufferVMAF[i]=BufferVMAS[i]=BufferMA[i];

        }

      else

        {

         //--- Fast

         double abs_cmof=fabs(BufferCMOF[i])*100.0;

         BufferVMAF[i]=(sc_fast*abs_cmof*BufferMA[i])+(1.0-sc_fast*abs_cmof)*BufferVMAF[i+1];

         //--- Slow

         double abs_cmos=fabs(BufferCMOS[i])*100.0;

         BufferVMAS[i]=(sc_slow*abs_cmos*BufferMA[i])+(1.0-sc_slow*abs_cmos)*BufferVMAS[i+1];

        }

     }

//--- Signal

   if(InpModeIND==MODE_IND_SIG)

     {

      for(int i=limit; i>=0; i--)

         BufferSig[i]=GetMA(rates_total,i,InpMethodSignal,period_sig,BufferVMAF,BufferSig);

     }

   

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

   for(int i=limit; i>=0; i--)

     {

      BufferCandleO[i]=open[i];

      BufferCandleH[i]=high[i];

      BufferCandleL[i]=low[i];

      BufferCandleC[i]=close[i];

      BufferColors[i]=(CheckLines(i)>0 ? 0 : CheckLines(i)<0 ? 1 : 2);

      BufferColors[i]=4;

      if(CheckLines(i)>0)

        {

         BufferUP[i]=open[i];

         BufferColors[i]=(close[i]>open[i] ? 0 : close[i]<open[i] ? 1 : 4);

        }

      if(CheckLines(i)<0)

        {

         BufferDN[i]=open[i];

         BufferColors[i]=(close[i]<open[i] ? 2 : close[i]>open[i] ? 3 : 4);

        }

     }

   

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

   return(rates_total);

  }

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

//| @>25@O5B A>>B=>H5=85 ;8=89                                      |

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

int CheckLines(const int shift)

  {

   return

     (

      InpModeIND==MODE_IND_FS ? 

      (BufferVMAF[shift]>BufferVMAS[shift] ? 1 : BufferVMAF[shift]<BufferVMAS[shift] ? -1 : 0) :

      (BufferVMAF[shift]>BufferSig[shift]  ? 1 : BufferVMAF[shift]<BufferSig[shift]  ? -1 : 0)

     );

  }

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

//| >72@0I05B MA ?> B8?C                                            |

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

double GetMA(const int rates_total,const int shift,const ENUM_MA_METHOD method,const int period_ma,const double &buffer_price[],const double &buffer_ma[])

  {

   switch(method)

     {

      case MODE_EMA  : return EMA(rates_total,buffer_price[shift],buffer_ma[shift+1],period_ma,shift);

      case MODE_SMMA : return SMMA(rates_total,buffer_price,buffer_ma[shift+1],period_ma,shift);

      case MODE_LWMA : return LWMA(rates_total,buffer_price,period_ma,shift);

      //---MODE_SMA

      default        : return SMA(rates_total,buffer_price,period_ma,shift);

     }

  }

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

//| Simple Moving Average                                            |

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

double SMA(const int rates_total,const double &array_src[],const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return array_src[shift];

   double sum=0;

   for(int i=0; i<period; i++)

      sum+=array_src[shift+i];

   return(sum/period);

  }

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

//| Exponential Moving Average                                       |

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

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

  {

   return(shift>=rates_total-2 || period<1 ? price : prev+2.0/(1+period)*(price-prev));

  }

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

//| Linear Weighted Moving Average                                   |

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

double LWMA(const int rates_total,const double &array_src[],const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return 0;

   double sum=0;

   double weight=0;

   for(int i=0; i<period; i++)

     {

      weight+=(period-i);

      sum+=array_src[shift+i]*(period-i);

     }

   return(weight>0 ? sum/weight : 0);

  }

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

//| Smoothed Moving Average                                          |

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

double SMMA(const int rates_total,const double &array_src[],const double prev,const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return 0;

   double smma=0;

   if(shift==rates_total-period-1)

      smma=SMA(rates_total,array_src,period,shift);

   else if(shift<rates_total-period-1)

     {

      double sum=0;

      for(int i = 0; i<period; i++)

         sum+=array_src[shift+i+1];

      smma=(sum-prev+array_src[shift])/period;

     }

   return smma;

  }

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

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