Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
HA_Delta
ÿþ//+------------------------------------------------------------------+

//|                                                     HA_Delta.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 "Heiken Ashi Delta indicator"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   2

//--- plot Delta

#property indicator_label1  "Delta"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Average

#property indicator_label2  "Average"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- enums

enum ENUM_CALC_MODE

  {

   CALC_MODE_OPEN,   // HA Open

   CALC_MODE_HIGH,   // HA High

   CALC_MODE_LOW,    // HA Low

   CALC_MODE_CLOSE,  // HA Close

  };

//--- input parameters

input uint           InpRange       =  1;                // Period

input uint           InpPeriodAVG   =  3;                // Smoothing period

input ENUM_MA_METHOD InpMethod      =  MODE_SMA;         // Smoothing method

input ENUM_CALC_MODE InpModeCalc    =  CALC_MODE_OPEN;   // Heiken Ashi applied price

//--- indicator buffers

double         BufferDelta[];

double         BufferAverage[];

double         BufferHA_O[];

double         BufferHA_H[];

double         BufferHA_L[];

double         BufferHA_C[];

//--- global variables

int            range;

int            period_avg;

int            period_max;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   range=int(InpRange<1 ? 1 : InpRange);

   period_avg=int(InpPeriodAVG<1 ? 1 : InpPeriodAVG);

   period_max=fmax(range,(period_avg*(InpMethod==MODE_EMA ? 4 : 2)));

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferDelta,INDICATOR_DATA);

   SetIndexBuffer(1,BufferAverage,INDICATOR_DATA);

   SetIndexBuffer(2,BufferHA_O,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferHA_H,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferHA_L,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferHA_C,INDICATOR_CALCULATIONS);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferDelta,true);

   ArraySetAsSeries(BufferAverage,true);

   ArraySetAsSeries(BufferHA_O,true);

   ArraySetAsSeries(BufferHA_H,true);

   ArraySetAsSeries(BufferHA_L,true);

   ArraySetAsSeries(BufferHA_C,true);

//--- setting plot buffer parameters

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,period_max);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,period_max);

   PlotIndexSetString(0,PLOT_LABEL,"Delta ("+(string)range+")");

   PlotIndexSetString(1,PLOT_LABEL,"Average ("+(string)period_avg+")");

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi Delta("+(string)range+","+(string)period_avg+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//---

   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<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-range-2;

      ArrayInitialize(BufferDelta,0);

      ArrayInitialize(BufferAverage,0);

      ArrayInitialize(BufferHA_O,0);

      ArrayInitialize(BufferHA_H,0);

      ArrayInitialize(BufferHA_L,0);

      ArrayInitialize(BufferHA_C,0);

     }

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

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

     {

      BufferHA_O[i]=(i==rates_total-2 ? (open[i+1]+close[i+1])/2.0 : (BufferHA_O[i+1]+BufferHA_C[i+1])/2.0);

      BufferHA_C[i]=(open[i]+high[i]+low[i]+close[i])/4.0;

      BufferHA_H[i]=fmax(BufferHA_O[i],fmax(BufferHA_C[i],high[i]));

      BufferHA_L[i]=fmin(BufferHA_O[i],fmin(BufferHA_C[i],low[i]));

     }



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

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

     {

      BufferDelta[i]=

        (

         InpModeCalc==CALC_MODE_OPEN ? BufferHA_O[i]-BufferHA_O[i+range] : 

         InpModeCalc==CALC_MODE_HIGH ? BufferHA_H[i]-BufferHA_H[i+range] :

         InpModeCalc==CALC_MODE_LOW  ? BufferHA_L[i]-BufferHA_L[i+range] :

         BufferHA_C[i]-BufferHA_C[i+range]

        );

      

      switch(InpMethod)

        {

         case MODE_EMA  :  BufferAverage[i]=EMA(rates_total,BufferDelta[i],BufferAverage[i+1],period_avg,i); break;

         case MODE_SMMA :  BufferAverage[i]=SMMA(rates_total,BufferDelta,BufferAverage[i+1],period_avg,i); break;

         case MODE_LWMA :  BufferAverage[i]=LWMA(rates_total,BufferDelta,period_avg,i); break;

         default        :  BufferAverage[i]=SMA(rates_total,BufferDelta,period_avg,i); break;

        }

     }



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

   return(rates_total);

  }

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

//| 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));

  }

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

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

  }

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

//| 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);

  }

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

Comments