ADXWilder Extremum

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
ADXWilder Extremum
ÿþ//+------------------------------------------------------------------+

//|                                           ADXWilder Extremum.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   2

//--- plot ExtremumUp

#property indicator_label1  "ExtremumUp"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot ExtremumDown

#property indicator_label2  "ExtremumDown"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input group             "ADXWilder"

input int                  Inp_ADXWilder_adx_period   = 14;       // ADXWilder: averaging period

input group             "Arrows"

input uchar                InpUpArrowCode             = 115;      // Arrow Up: code (font Wingdings)

input int                  InpUpArrowShift            = 5;        // Arrow Up: vertical shift in pixel

input uchar                InpDownArrowCode           = 115;      // Arrow Down: code (font Wingdings)

input int                  InpDownArrowShift          = 5;        // Arrow Down: vertical shift in pixel

//--- indicator buffers

double   ExtremumUpBuffer[];

double   ExtremumDownBuffer[];

double   ADXBuffer[];

double   DI_plusBuffer[];

double   DI_minusBuffer[];

//---

int      handle_iADXWilder;      // variable for storing the handle of the iADXWilder indicator

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

bool     m_error=false;

double   m_last_high=0.0;

double   m_last_low=0.0;

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

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

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

      m_error=true;

      return(INIT_SUCCEEDED);

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtremumUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtremumDownBuffer,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,InpUpArrowCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpDownArrowCode);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-InpUpArrowShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,InpDownArrowShift);

//--- an empty value for plotting, for which there is no drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator

   handle_iADXWilder=iADXWilder(Symbol(),Period(),Inp_ADXWilder_adx_period);

//--- if the handle is not created

   if(handle_iADXWilder==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

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

  {

//---

   if(m_error)

      return(0);

   if(rates_total<Inp_ADXWilder_adx_period)

      return(0);

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iADXWilder);

   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 iADXWilder 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 iADXWilder 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 by Welles Wilder indicator

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

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

      return(0);

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      ExtremumUpBuffer[0]=0.0;

      ExtremumDownBuffer[0]=0.0;

      m_last_high=0.0;

      m_last_low=0.0;

      limit=1;

     }

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

     {

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

        {

         int bars_d1=iBarShift(Symbol(),PERIOD_D1,time[i]);

         if(bars_d1>-1)

           {

            double high_d1=iHigh(Symbol(),PERIOD_D1,bars_d1);

            m_last_high=high_d1;

           }

        }

      else

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

           {

            int bars_d1=iBarShift(Symbol(),PERIOD_D1,time[i]);

            if(bars_d1>-1)

              {

               double low_d1=iLow(Symbol(),PERIOD_D1,bars_d1);

               m_last_low=low_d1;

              }

           }

      //---

      ExtremumUpBuffer[i]=m_last_high;

      ExtremumDownBuffer[i]=m_last_low;

     }

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

   bars_calculated=calculated;

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iADXWilder 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 iADXWilder 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 iADXWilder 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 iADXWilder 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 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 iADXWilder 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);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iADXWilder!=INVALID_HANDLE)

      IndicatorRelease(handle_iADXWilder);

  }

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

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