Author: Copyright © 2022, Vladimir Karputov
Price Data Components
Indicators Used
Movement directional index
0 Views
0 Downloads
0 Favorites
ADX Trend
ÿþ//+------------------------------------------------------------------+

//|                                                    ADX Trend.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 7

#property indicator_plots   4

//--- plot SIDEWAY_BUY

#property indicator_label1  "SIDEWAY_BUY"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDeepSkyBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SIDEWAY_SELL

#property indicator_label2  "SIDEWAY_SELL"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrCoral

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot TREND_BUY

#property indicator_label3  "TREND_BUY"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot TREND_SELL

#property indicator_label4  "TREND_SELL"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input group             "ADX"

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

input group             "Arrows"

input uchar                Inp_SIDEWAY_BUY_Code       = 246;         // Arrow 'SIDEWAY_BUY': code (font Wingdings)

input int                  Inp_SIDEWAY_BUY_Shift      = 10;          // Arrow 'SIDEWAY_BUY': vertical shift in pixel

//---

input uchar                Inp_SIDEWAY_SELL_Code      = 248;         // Arrow 'SIDEWAY_SELL': code (font Wingdings)

input int                  Inp_SIDEWAY_SELL_Shift     = 10;          // Arrow 'SIDEWAY_SELL': vertical shift in pixel

//---

input uchar                Inp_TREND_BUY_Code         = 241;         // Arrow 'TREND_BUY': code (font Wingdings)

input int                  Inp_TREND_BUY_Shift        = 10;          // Arrow 'TREND_BUY': vertical shift in pixel

//---

input uchar                Inp_TREND_SELL_Code        = 242;         // Arrow 'TREND_SELL': code (font Wingdings)

input int                  Inp_TREND_SELL_Shift       = 10;          // Arrow 'TREND_SELL': vertical shift in pixel

//--- indicator buffers

double   SIDEWAY_BUYBuffer[];

double   SIDEWAY_SELLBuffer[];

double   TREND_BUYBuffer[];

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

bool     m_init_error               = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,SIDEWAY_BUYBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SIDEWAY_SELLBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,TREND_BUYBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,TREND_SELLBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,ADXBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,DI_plusBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,DI_minusBuffer,INDICATOR_CALCULATIONS);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

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

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_SIDEWAY_BUY_Code);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_SIDEWAY_SELL_Code);

   PlotIndexSetInteger(2,PLOT_ARROW,Inp_TREND_BUY_Code);

   PlotIndexSetInteger(3,PLOT_ARROW,Inp_TREND_SELL_Code);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,Inp_SIDEWAY_BUY_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-Inp_SIDEWAY_SELL_Shift);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,Inp_TREND_BUY_Shift);

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,-Inp_TREND_SELL_Shift);

//--- set an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(3,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

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

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

      return(0);

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

//---

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

     {

      SIDEWAY_BUYBuffer[i]=EMPTY_VALUE;

      SIDEWAY_SELLBuffer[i]=EMPTY_VALUE;

      TREND_BUYBuffer[i]=EMPTY_VALUE;

      TREND_SELLBuffer[i]=EMPTY_VALUE;

      if(ADXBuffer[i]>DI_plusBuffer[i] && ADXBuffer[i]>DI_minusBuffer[i] && DI_plusBuffer[i]>DI_minusBuffer[i])

         SIDEWAY_BUYBuffer[i]=low[i];

      if(ADXBuffer[i]>DI_plusBuffer[i] && ADXBuffer[i]>DI_minusBuffer[i]&& DI_plusBuffer[i] < DI_minusBuffer[i])

         SIDEWAY_SELLBuffer[i]=high[i];

      if(ADXBuffer[i]<DI_plusBuffer[i] && DI_plusBuffer[i]>DI_minusBuffer[i])

         TREND_BUYBuffer[i]=low[i];

      if(ADXBuffer[i]<DI_minusBuffer[i] && DI_plusBuffer[i]<DI_minusBuffer[i])

         TREND_SELLBuffer[i]=high[i];

     }

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

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iADX!=INVALID_HANDLE)

      IndicatorRelease(handle_iADX);

  }

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

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