MACD Extremum Separate Window

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Indicators Used
MACD Histogram
0 Views
0 Downloads
0 Favorites
MACD Extremum Separate Window
ÿþ//+------------------------------------------------------------------+

//|                                MACD Extremum Separate Window.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.003"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   5/*4*/

//--- plot MACD_Extremum_UP

#property indicator_label1  "MACD Extremum UP"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrTomato

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot MACD_Extremum_DOWN

#property indicator_label2  "MACD Extremum DOWN"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrDeepSkyBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Section Extremum UP

#property indicator_label3  "Section  Extremum UP"

#property indicator_type3   DRAW_SECTION

#property indicator_color3  clrPaleGoldenrod

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Section Extremum DOWN

#property indicator_label4  "Section  Extremum DOWN"

#property indicator_type4   DRAW_SECTION

#property indicator_color4  clrPaleGoldenrod

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot MACD

#property indicator_label5  "MACD"

#property indicator_type5   DRAW_HISTOGRAM

#property indicator_color5  clrSilver

#property indicator_style5  STYLE_SOLID

#property indicator_width5  3

//--- input parameters

input group             "MACD"

input int                  Inp_MACD_fast_ema_period= 12;          // MACD: period for Fast average calculation

input int                  Inp_MACD_slow_ema_period= 26;          // MACD: period for Slow average calculation

input int                  Inp_MACD_signal_period  = 9;           // MACD: period for their difference averaging

input ENUM_APPLIED_PRICE   Inp_MACD_applied_price  = PRICE_CLOSE; // MACD: type of price

input group             "Arrow"

input uchar                Inp_Arrow_UP_Code       = 83;          // Extremum UP: symbol code from the Wingdings font

input int                  Inp_Arrow_UP_Shift      = 10;          // Extremum UP: vertical shift of arrows in pixels

input uchar                Inp_Arrow_DOWN_Code     = 83;          // Extremum DOWN: symbol code from the Wingdings font

input int                  Inp_Arrow_DOWN_Shift    = 10;          // Extremum DOWN: vertical shift of arrows in pixels

input group             "Extremum"

input uchar                Inp_Extremum_Bars       = 1;           // Number of neighbors ('0' is equal to '1')

//--- indicator buffers

double   MACDExtremumUPBuffer[];

double   MACDExtremumDOWNBuffer[];

double   SectionExtremumUPBufferr[];

double   SectionExtremumDOWNBufferr[];

double   MACDBuffer[];

//---

int      handle_iMACD;        // variable for storing the handle of the iMACD indicator

int      bars_calculated=0;   // we will keep the number of values in the Moving Averages Convergence/Divergence indicator

uchar    m_extremum_bars=0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,MACDExtremumUPBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,MACDExtremumDOWNBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,SectionExtremumUPBufferr,INDICATOR_DATA);

   SetIndexBuffer(3,SectionExtremumDOWNBufferr,INDICATOR_DATA);

   SetIndexBuffer(4,MACDBuffer,INDICATOR_DATA/*INDICATOR_CALCULATIONS*/);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_Arrow_UP_Code);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_Arrow_DOWN_Code);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-Inp_Arrow_UP_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,Inp_Arrow_DOWN_Shift);

//--- set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_MACD_signal_period-1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Inp_MACD_signal_period-1);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"MACD Extremum("+IntegerToString(Inp_MACD_fast_ema_period)+","+

                      IntegerToString(Inp_MACD_slow_ema_period)+","+

                      IntegerToString(Inp_MACD_signal_period)+")");

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

//--- Set labels for the line

   string plot_label=StringFormat("MACD(%d,%d,%d)",Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,Inp_MACD_signal_period);

   PlotIndexSetString(4,PLOT_LABEL,plot_label);

//--- create handle of the indicator iMACD

   handle_iMACD=iMACD(Symbol(),Period(),Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,

                      Inp_MACD_signal_period,Inp_MACD_applied_price);

//--- if the handle is not created

   if(handle_iMACD==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

   m_extremum_bars=(Inp_Extremum_Bars==0)?1:Inp_Extremum_Bars;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMACD!=INVALID_HANDLE)

      IndicatorRelease(handle_iMACD);

  }

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

//| 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(rates_total<Inp_MACD_signal_period || rates_total<(m_extremum_bars*2+1))

      return(0);

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

   int values_to_copy;

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

   int calculated=BarsCalculated(handle_iMACD);

   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 iMACD 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 MACDBuffer array is greater than the number of values in the iMACD 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 arrays with values of the iMACD indicator

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

   if(!FillArraysFromBuffers(MACDBuffer,handle_iMACD,values_to_copy))

      return(0);

//--- memorize the number of values in the Moving Averages indicator Convergence/Divergence

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      /*

      #0 #1 #2 #3 #4 #5 #6 #7 #8 #9

      +  +  +                       m_extremum_bars 1 if(MACD[ext-2]

      +  +  +  +  +                 m_extremum_bars 2

      +  +  +  +  +  +  +           m_extremum_bars 3

      +  +  +  +  +  +  +  +  +     m_extremum_bars 4

      */

      limit=m_extremum_bars;

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

        {

         MACDExtremumUPBuffer[i]=0.0;

         MACDExtremumDOWNBuffer[i]=0.0;

         SectionExtremumUPBufferr[i]=0.0;

         SectionExtremumDOWNBufferr[i]=0.0;

        }

     }

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

     {

      MACDExtremumUPBuffer[i]=0.0;

      MACDExtremumDOWNBuffer[i]=0.0;

      SectionExtremumUPBufferr[i]=0.0;

      SectionExtremumDOWNBufferr[i]=0.0;

     }

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

     {

      int count_minus=0,count_plus=0;

      int done_minus=0,done_plus=0;

      //--- MACD Extremum UP

      for(int j=i-m_extremum_bars; j<i; j++)

        {

         count_minus++;

         if(MACDBuffer[j]<MACDBuffer[j+1] && MACDBuffer[j]>0.0 && MACDBuffer[j+1]>0.0)

            done_minus++;

        }

      for(int j=i+m_extremum_bars; j>i; j--)

        {

         count_plus++;

         if(MACDBuffer[j]<MACDBuffer[j-1] && MACDBuffer[j]>0.0 && MACDBuffer[j-1]>0.0)

            done_plus++;

        }

      //---

      if(count_minus>0 && count_minus==count_plus && count_minus==done_minus && count_plus==done_plus)

        {

         for(int j=i-m_extremum_bars; j<i; j++)

           {

            MACDExtremumUPBuffer[i]=0.0;

            SectionExtremumUPBufferr[i]=0.0;

           }

         for(int j=i+m_extremum_bars; j>i; j--)

           {

            MACDExtremumUPBuffer[i]=0.0;

            SectionExtremumUPBufferr[i]=0.0;

           }

         MACDExtremumUPBuffer[i]=MACDBuffer/*high*/[i];

         SectionExtremumUPBufferr[i]=MACDBuffer/*high*/[i];

        }

      else

        {

         for(int j=i-m_extremum_bars; j<i; j++)

           {

            MACDExtremumUPBuffer[i]=0.0;

            SectionExtremumUPBufferr[i]=0.0;

           }

         for(int j=i+m_extremum_bars; j>i; j--)

           {

            MACDExtremumUPBuffer[i]=0.0;

            SectionExtremumUPBufferr[i]=0.0;

           }

         MACDExtremumUPBuffer[i]=0.0;

         SectionExtremumUPBufferr[i]=0.0;

        }

      //---

      count_minus=0;

      count_plus=0;

      done_minus=0;

      done_plus=0;

      //--- MACD Extremum DOWN

      for(int j=i-m_extremum_bars; j<i; j++)

        {

         count_minus++;

         if(MACDBuffer[j]>MACDBuffer[j+1] && MACDBuffer[j]<0.0 && MACDBuffer[j+1]<0.0)

            done_minus++;

        }

      for(int j=i+m_extremum_bars; j>i; j--)

        {

         count_plus++;

         if(MACDBuffer[j]>MACDBuffer[j-1] && MACDBuffer[j]<0.0 && MACDBuffer[j-1]<0.0)

            done_plus++;

        }

      //---

      if(count_minus>0 && count_minus==count_plus && count_minus==done_minus && count_plus==done_plus)

        {

         for(int j=i-m_extremum_bars; j<i; j++)

           {

            MACDExtremumDOWNBuffer[i]=0.0;

            SectionExtremumDOWNBufferr[i]=0.0;

           }

         for(int j=i+m_extremum_bars; j>i; j--)

           {

            MACDExtremumDOWNBuffer[i]=0.0;

            SectionExtremumDOWNBufferr[i]=0.0;

           }

         MACDExtremumDOWNBuffer[i]=MACDBuffer/*low*/[i];

         SectionExtremumDOWNBufferr[i]=MACDBuffer/*low*/[i];

        }

      else

        {

         for(int j=i-m_extremum_bars; j<i; j++)

           {

            MACDExtremumDOWNBuffer[i]=0.0;

            SectionExtremumDOWNBufferr[i]=0.0;

           }

         for(int j=i+m_extremum_bars; j>i; j--)

           {

            MACDExtremumDOWNBuffer[i]=0.0;

            SectionExtremumDOWNBufferr[i]=0.0;

           }

         MACDExtremumDOWNBuffer[i]=0.0;

         SectionExtremumDOWNBufferr[i]=0.0;

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iMACD indicator               |

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

bool FillArraysFromBuffers(double &macd_buffer[],    // indicator buffer of MACD values

                           int ind_handle,           // handle of the iMACD indicator

                           int amount                // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

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

     {

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

      PrintFormat("Failed to copy data from the iMACD 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 ---