Author: 2019 © NELODI.com
0 Views
0 Downloads
0 Favorites
NLD_ADX
ÿþ//+------------------------------------------+

//|                              NLD_ADX.mq5 |

//|                    http://www.nelodi.com |

//|            Copyright (c) 2019 NELODI.com |

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

#property copyright   "2019 © NELODI.com"

#property link        "http://www.nelodi.com"

#property description "NELODI Average Directional Movement Index"



#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   2



#property indicator_maximum  125

#property indicator_minimum  5



#property indicator_type2   DRAW_LINE

#property indicator_color2  C'200,200,0'

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1



#property indicator_type1   DRAW_FILLING

#property indicator_color1  C'0,150,0',C'150,0,0'



#property indicator_label1  "DI"

#property indicator_label2  "ADX"



//--- input parameters

input int InpPeriodADX=15; // Period

//---- buffers

double    ExtADXBuffer[];

double    ExtPDIBuffer[];

double    ExtNDIBuffer[];

double    ExtPDBuffer[];

double    ExtNDBuffer[];

double    ExtTmpBuffer[];

//--- global variables

int       ExtADXPeriod;

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

//| Custom indicator initialization function                         |

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

void OnInit() {

//--- check for input parameters

   if(InpPeriodADX<=0) {

      ExtADXPeriod=15;

      printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);

   } else ExtADXPeriod=InpPeriodADX;

//---- indicator buffers

   SetIndexBuffer(0,ExtPDIBuffer);

   SetIndexBuffer(1,ExtNDIBuffer);

   SetIndexBuffer(2,ExtADXBuffer);

   SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS);

//--- indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- set draw begin

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);

//--- indicator short name

   string short_name="NLD_ADX("+string(ExtADXPeriod)+")";

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- change 1-st index label

   PlotIndexSetString(1,PLOT_LABEL,short_name);

//---- end of initialization function

}

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

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

   ArraySetAsSeries(ExtPDIBuffer,false);

   ArraySetAsSeries(ExtNDIBuffer,false);

   ArraySetAsSeries(ExtADXBuffer,false);



   ArraySetAsSeries(ExtPDBuffer,false);

   ArraySetAsSeries(ExtNDBuffer,false);

   ArraySetAsSeries(ExtTmpBuffer,false);



   ArraySetAsSeries(high,false);

   ArraySetAsSeries(low,false);

   ArraySetAsSeries(open,false);

   ArraySetAsSeries(close,false);



//--- checking for bars count

   if(rates_total<ExtADXPeriod)

      return(0);

//--- detect start position

   int start;

   if(prev_calculated>1) start=prev_calculated-1;

   else {

      start=1;

      ExtPDIBuffer[0]=0.0;

      ExtNDIBuffer[0]=0.0;

      ExtADXBuffer[0]=0.0;



      ExtPDBuffer[0]=0.0;

      ExtNDBuffer[0]=0.0;

      ExtTmpBuffer[0]=0.0;

   }



   double Pdi,Ndi,Adx;

   double Pdo,Ndo,Ado;

   double Hi,prevHi,Lo,prevLo,prevCl;

   double dTmpP,dTmpN,tr,dTmp;



//--- main cycle

   for(int i=start; i<rates_total && !IsStopped(); i++) {

      //--- ADX calculation ...

      Hi=high[i];

      prevHi=high[i-1];

      Lo=low[i];

      prevLo=low[i-1];

      prevCl=close[i-1];

      dTmpP=Hi-prevHi;

      if(dTmpP<0.0) dTmpP=0.0;

      dTmpN=prevLo-Lo;

      if(dTmpN<0.0) dTmpN=0.0;

      if(dTmpP>dTmpN) dTmpN=0.0;

      else if(dTmpP<dTmpN) dTmpP=0.0;

      else {

         dTmpP=0.0;

         dTmpN=0.0;

      }

      tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl));

      if(tr!=0.0) {

         ExtPDBuffer[i]=100.0*dTmpP/tr;

         ExtNDBuffer[i]=100.0*dTmpN/tr;

      } else {

         ExtPDBuffer[i]=0.0;

         ExtNDBuffer[i]=0.0;

      }

      // fill smoothed positive and negative buffers

      Pdo=ExtPDBuffer[i-1];

      Pdi=ExponentialMA(ExtPDBuffer[i],ExtADXPeriod,Pdo);

      ExtPDBuffer[i]=Pdi;

      Ndo=ExtNDBuffer[i-1];

      Ndi=ExponentialMA(ExtNDBuffer[i],ExtADXPeriod,Ndo);

      ExtNDBuffer[i]=Ndi;

      // fill ADXTmp buffer

      dTmp=Pdi+Ndi;

      if(dTmp!=0.0) dTmp=100.0*MathAbs((Pdi-Ndi)/dTmp);

      else dTmp=0.0;

      ExtTmpBuffer[i]=dTmp;

      // fill smoothed ADX buffer

      Ado=ExtTmpBuffer[i-1];

      Adx=ExponentialMA(ExtTmpBuffer[i],ExtADXPeriod,Ado);

      ExtTmpBuffer[i]=Adx;



      ExtADXBuffer[i]=(Adx*2+(Pdi-Ndi)/2-(Ndi-Pdi)/2)/2;

      ExtPDIBuffer[i]=Adx+Pdi; // Adx+(Pdi-Ndi)/2-(Ndi-Pdi)/2;

      ExtNDIBuffer[i]=Adx+Ndi; // Adx;

   }

//---- OnCalculate done. Return new prev_calculated.

   return(rates_total);

}

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

//| Exponential Moving Average                                       |

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

double ExponentialMA(const double price_at_position,const int period,const double prev_value) {

   if(period>0) {

      double pr=2.0/(period+1.0);

      return(price_at_position*pr+prev_value*(1-pr));

   } else

      return(0);

}

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

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