Choppy_Market_ADX_Indicator

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Movement directional index
0 Views
0 Downloads
0 Favorites
Choppy_Market_ADX_Indicator
ÿþ//+------------------------------------------------------------------+

//|                                  Choppy_Market_ADX_Indicator.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 "Choppy Market ADX indicator"

#property indicator_chart_window

#property indicator_buffers 16

#property indicator_plots   4

//--- plot UP

#property indicator_label1  "UP"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DN

#property indicator_label2  "DN"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot LineUP

#property indicator_label3  "LineUP"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot LineDN

#property indicator_label4  "LineDN"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input uint              InpPeriod      =  14;            // Smoothed ADX Period

input double            InpAlpha1      =  0.25;          // Smoothed ADX Primary smoothing factor

input double            InpAlpha2      =  0.33;          // Smoothed ADX Secondary smoothing factor

input double            InpLevel       =  25.0;          // Level

input color             InpColorZone   =  clrSilver;     // Zone color

input ENUM_LINE_STYLE   InpStyleBorder =  STYLE_DOT;     // Zone border style

input uint              InpWidthBorder =  0;             // Zone border width

input bool              InpFilledZone  =  true;          // Filled zone

input bool              InpBackground  =  true;          // Draw zone as background

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferLineUP[];

double         BufferLineDN[];

double         BufferSecond[];

double         BufferSignal[];

double         BufferCross[];

//---

double         BufferSDIP[];

double         BufferSDIM[];

double         BufferSADX[];

double         BufferDIPtmp[];

double         BufferDIMtmp[];

double         BufferADXtmp[];

double         BufferDIP[];

double         BufferDIM[];

double         BufferADX[];

//--- global variables

string         prefix;

double         alpha1;

double         alpha2;

double         level;

int            period;

int            handle_adx;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_";

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

   alpha1=(InpAlpha1<0 ? 0 : InpAlpha1);

   alpha2=(InpAlpha2<0.109 ? 0.109 : InpAlpha2);

   level=(InpLevel>100.0 ? 100.0 : InpLevel<0.1 ? 0.1 : InpLevel);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferLineUP,INDICATOR_DATA);

   SetIndexBuffer(3,BufferLineDN,INDICATOR_DATA);

   SetIndexBuffer(4,BufferSecond,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferSignal,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferCross,INDICATOR_CALCULATIONS);

   //---

   SetIndexBuffer(7,BufferSADX,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferSDIP,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferSDIM,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferDIPtmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,BufferDIMtmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(12,BufferADXtmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(13,BufferDIP,INDICATOR_CALCULATIONS);

   SetIndexBuffer(14,BufferDIM,INDICATOR_CALCULATIONS);

   SetIndexBuffer(15,BufferADX,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,115);

   PlotIndexSetInteger(1,PLOT_ARROW,115);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Choppy Market ADX ("+(string)period+","+DoubleToString(alpha1,2)+","+DoubleToString(alpha2,2)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetString(0,PLOT_LABEL,"ChMADX("+(string)period+")");

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferLineUP,true);

   ArraySetAsSeries(BufferLineDN,true);

   ArraySetAsSeries(BufferSecond,true);

   ArraySetAsSeries(BufferSignal,true);

   ArraySetAsSeries(BufferCross,true);

   ArraySetAsSeries(BufferSADX,true);

   ArraySetAsSeries(BufferSDIP,true);

   ArraySetAsSeries(BufferSDIM,true);

   ArraySetAsSeries(BufferDIPtmp,true);

   ArraySetAsSeries(BufferDIMtmp,true);

   ArraySetAsSeries(BufferADXtmp,true);

   ArraySetAsSeries(BufferDIP,true);

   ArraySetAsSeries(BufferDIM,true);

   ArraySetAsSeries(BufferADX,true);

//--- create ADX handle

   ResetLastError();

   handle_adx=iADX(NULL,PERIOD_CURRENT,period);

   if(handle_adx==INVALID_HANDLE)

     {

      Print("The iADX (",(string)period,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//--- timer

   ObjectsDeleteAll(0,prefix,0);

   ChartRedraw();

//---

  }

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

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

   ArraySetAsSeries(time,true);

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

   if(rates_total<4) 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(BufferLineUP,EMPTY_VALUE);

      ArrayInitialize(BufferLineDN,EMPTY_VALUE);

      ArrayInitialize(BufferSecond,0);

      ArrayInitialize(BufferSignal,0);

      ArrayInitialize(BufferCross,0);

      //---

      ArrayInitialize(BufferSDIP,0);

      ArrayInitialize(BufferSDIM,0);

      ArrayInitialize(BufferSADX,0);

      ArrayInitialize(BufferDIPtmp,0);

      ArrayInitialize(BufferDIMtmp,0);

      ArrayInitialize(BufferADXtmp,0);

      ArrayInitialize(BufferDIP,0);

      ArrayInitialize(BufferDIM,0);

      ArrayInitialize(BufferADX,0);

     }

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

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

   copied=CopyBuffer(handle_adx,MAIN_LINE,0,count,BufferADX);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_adx,PLUSDI_LINE,0,count,BufferDIP);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_adx,MINUSDI_LINE,0,count,BufferDIM);

   if(copied!=count) return 0;

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

     {

      BufferDIPtmp[i]=2.0*BufferDIP[i]+(alpha1-2.0)*BufferDIP[i+1]+(1.0-alpha1)*BufferDIPtmp[i+1];

      BufferDIMtmp[i]=2.0*BufferDIM[i]+(alpha1-2.0)*BufferDIM[i+1]+(1.0-alpha1)*BufferDIMtmp[i+1];

      BufferADXtmp[i]=2.0*BufferADX[i]+(alpha1-2.0)*BufferADX[i+1]+(1.0-alpha1)*BufferADXtmp[i+1];



      BufferSDIP[i]=alpha2*BufferDIPtmp[i]+(1.0-alpha2)*BufferSDIP[i+1];

      BufferSDIM[i]=alpha2*BufferDIMtmp[i]+(1.0-alpha2)*BufferSDIM[i+1];

      BufferSADX[i]=alpha2*BufferADXtmp[i]+(1.0-alpha2)*BufferSADX[i+1];

     }

   double line=0,X=0;

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

     {

      double ADX0=BufferSADX[i];

      double DIP0=BufferSDIP[i];

      double DIP1=BufferSDIP[i+1];

      double DIM0=BufferSDIM[i];

      double DIM1=BufferSDIM[i+1];

      BufferSignal[i]=(ADX0<level ? 1 : 0);

      BufferCross[i]=(DIP0>DIM0 && DIP1<=DIM1 ? 1 : DIP0<DIM0 && DIP1>=DIM1 ? -1 : 0);

      Last(rates_total,i,line,X,high,low);

      BufferSecond[i]=(X==1 && close[i]>line && close[i+1]<=line ? 1 : X==-1 && close[i]<line && close[i+1]>=line ? -1 : 0);

     }

   

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

   int p1=0,p2=0,p3=0;

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

     {

      if(BufferSignal[i]==1 && BufferSignal[i+1]!=1)

        {

         p1=i;

         p2=0;

        }

      if((BufferSignal[i+1]==1 && BufferSignal[i]!=1) || (BufferSignal[i]==1 && i==0))

         p2=i;

      

      if(p1!=0 && p2!=0)

        {

         int bl=ArrayMinimum(low,p2,p1-p2+1);

         int bh=ArrayMaximum(high,p2,p1-p2+1);

         if(bl==WRONG_VALUE || bh==WRONG_VALUE)

            continue;

         double min=low[bl];

         double max=high[bh];

         DrawRectangle(prefix+"Zone_"+TimeToString(time[i]),0,min,max,time[p1],time[p2],InpColorZone,InpWidthBorder,InpStyleBorder,InpBackground,InpFilledZone);

        }

      if(BufferCross[i]==1)

        {

         BufferUP[i]=low[i];

         p3=FindNext(i,high[i],1,close);

         if(p3>-1)

            BufferUP[p3]=low[p3];

        }

      else if(BufferCross[i]==-1)

        {

         BufferDN[i]=high[i];

         p3=FindNext(i,low[i],-1,close);

         if(p3>-1)

            BufferDN[p3]=high[p3];

        }

     }

   

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

   return(rates_total);

  }

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

//|                                                                  |

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

void Last(const int rates_total,const int index,double &line,double &x,const double &high[],const double &low[])

  {

   line=0;

   x=0;

   for(int i=index; i<rates_total-2; i++)

     {

      if(BufferSignal[i]==0)

         continue;

      x=BufferSignal[i];

      line=(BufferSignal[i]==1 ? high[i] : low[i]);

      return;

     }

   return;

  }

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

//|                                                                  |

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

int FindNext(const int index,const double cross_level,const int side,const double &close[])

  {

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

      if((close[i]>cross_level && close[i+1]<=cross_level && side==1) || (close[i]<cross_level && close[i+1]>=cross_level && side==-1))

         return i;

   return WRONG_VALUE;

  }

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

//|  8AC5B ?@O<>C3>;L=8:                                             |

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

void DrawRectangle(const string name,

                   const int sub_win,

                   const double price1,

                   const double price2,

                   const datetime time1,

                   const datetime time2,

                   const color rect_color,

                   const int border_width,

                   const ENUM_LINE_STYLE border_style,

                   const bool background=true,

                   const bool fill=false,

                   const string text="\n",

                   const string tooltip="\n")

  {

   if(ObjectFind(0,name)<0)

     {

      ObjectCreate(0,name,OBJ_RECTANGLE,sub_win,time1,price1,time2,price2);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

     }

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time1);

   ObjectSetInteger(0,name,OBJPROP_TIME,1,time2);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price1);

   ObjectSetDouble(0,name,OBJPROP_PRICE,1,price2);

//---

   ObjectSetInteger(0,name,OBJPROP_FILL,fill);

   ObjectSetInteger(0,name,OBJPROP_BACK,background);

   ObjectSetInteger(0,name,OBJPROP_WIDTH,border_width);

   ObjectSetInteger(0,name,OBJPROP_COLOR,rect_color);

   ObjectSetInteger(0,name,OBJPROP_STYLE,border_style);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);

  }

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

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