ADX Extremum

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Movement directional index
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
ADX Extremum
ÿþ//+------------------------------------------------------------------+

//|                                                 ADX Extremum.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   2

//--- plot BUY

#property indicator_label1  "BUY"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRoyalBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SELL

#property indicator_label2  "SELL"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrOrangeRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input int   Inp_ADX_adx_period=14;    // ADX: averaging period

//--- indicator buffers

double   BUYBuffer[];

double   SELLBuffer[];

double   ADXBuffer[];

double   DI_plusBuffer[];

double   DI_minusBuffer[];

//---

int      handle_iADX;         // variable for storing the handle of the iADX indicator

int      bars_calculated=0;   // we will keep the number of values in the Average Directional Movement Index indicator 

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Period()>PERIOD_D1)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      ""09<D@59< =5 <>65B 1KBL 1>;LH5 D1!":

                      "Timeframe can not be greater than D1!";

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_FAILED);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,BUYBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SELLBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ADXBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,DI_plusBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,DI_minusBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

//--- set the vertical shift of arrows in pixels 

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,+5);

//--- set an empty value 

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- create handle of the indicator iADX

   handle_iADX=iADX(Symbol(),Period(),Inp_ADX_adx_period);

//--- if the handle is not created 

   if(handle_iADX==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code 

      PrintFormat("Failed to create handle of the iADX indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---

   string short_name=StringFormat("ADX Extremum(%d)",Inp_ADX_adx_period);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//---

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

  {

//--- number of values copied from the iADX indicator 

   int values_to_copy;

//--- determine the number of values calculated in the indicator 

   int calculated=BarsCalculated(handle_iADX);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iADX indicator changed 

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) 

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iADXBuffer array is greater than the number of values in the iADX indicator for symbol/period, then we don't copy everything  

      //--- otherwise, we copy less than the size of indicator buffers 

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() 

      //--- for calculation not more than one bar is added 

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the array with values of the Average Directional Movement Index indicator 

//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation 

   if(!FillArraysFromBuffers(ADXBuffer,DI_plusBuffer,DI_minusBuffer,handle_iADX,values_to_copy))

      return(0);

//--- memorize the number of values in the Average Directional Movement Index indicator 

   bars_calculated=calculated;

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

//---

   string comment="";

   for(int i=limit;i<rates_total;i++)

     {

      if(i==0)

        {

         BUYBuffer[i]=EMPTY_VALUE;

         SELLBuffer[i]=EMPTY_VALUE;

         continue;

        }

      if(DI_plusBuffer[i-1]<DI_minusBuffer[i-1] && DI_plusBuffer[i]>DI_minusBuffer[i])

        {

         MqlDateTime SDateTime;

         TimeToStruct(time[i],SDateTime);

         SDateTime.hour=0; SDateTime.min=0; SDateTime.sec=0;

         datetime time_d1=StructToTime(SDateTime);



         double arr_high[];

         int result=CopyHigh(Symbol(),Period(),time_d1,time[i],arr_high);

         if(result<1)

            BUYBuffer[i]=EMPTY_VALUE;

         else

            BUYBuffer[i]=arr_high[ArrayMaximum(arr_high,0,WHOLE_ARRAY)];

        }

      else

         BUYBuffer[i]=EMPTY_VALUE;

      //---

      if(DI_plusBuffer[i-1]>DI_minusBuffer[i-1] && DI_plusBuffer[i]<DI_minusBuffer[i])

        {

         MqlDateTime SDateTime;

         TimeToStruct(time[i],SDateTime);

         SDateTime.hour=0; SDateTime.min=0; SDateTime.sec=0;

         datetime time_d1=StructToTime(SDateTime);



         double arr_low[];

         int result=CopyLow(Symbol(),Period(),time_d1,time[i],arr_low);

         if(result<1)

            SELLBuffer[i]=EMPTY_VALUE;

         else

            SELLBuffer[i]=arr_low[ArrayMinimum(arr_low,0,WHOLE_ARRAY)];

        }

      else

         SELLBuffer[i]=EMPTY_VALUE;

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iADX indicator                | 

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

bool FillArraysFromBuffers(double &adx_values[],      // indicator buffer of the ADX line 

                           double &DIplus_values[],   // indicator buffer for DI+ 

                           double &DIminus_values[],  // indicator buffer for DI- 

                           int ind_handle,            // handle of the iADX indicator 

                           int amount                 // number of copied values 

                           )

  {

//--- reset error code 

   ResetLastError();

//--- fill a part of the iADXBuffer array with values from the indicator buffer that has 0 index 

   if(CopyBuffer(ind_handle,0,0,amount,adx_values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }



//--- fill a part of the DI_plusBuffer array with values from the indicator buffer that has index 1 

   if(CopyBuffer(ind_handle,1,0,amount,DIplus_values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }



//--- fill a part of the DI_minusBuffer array with values from the indicator buffer that has index 2 

   if(CopyBuffer(ind_handle,2,0,amount,DIminus_values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }

//--- everything is fine 

   return(true);

  }

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

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