MACD Top Zero Bottom

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
MACD Histogram
0 Views
0 Downloads
0 Favorites
MACD Top Zero Bottom
ÿþ//+------------------------------------------------------------------+

//|                                         MACD Top Zero Bottom.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.000"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   3

//--- plot Top

#property indicator_label1  "Top"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Zero

#property indicator_label2  "Zero"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrSpringGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Bottom

#property indicator_label3  "Bottom"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- 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             "Arrows"

input uchar                InpTopArrowCode         = 159;         // Arrow Top: code (font Wingdings)

input int                  InpTopArrowShift        = 5;           // Arrow Top: vertical shift in pixel

input uchar                InpZeroArrowCode        = 167;         // Arrow Zero: code (font Wingdings)

input uchar                InpBottomArrowCode      = 159;         // Arrow Bottom: code (font Wingdings)

input int                  InpBottomArrowShift     = -5;          // Arrow Bottom: vertical shift in pixel

//--- indicator buffers

double   TopBuffer[];

double   ZeroBuffer[];

double   BottomBuffer[];

double   MACDBuffer[];

double   SignalBuffer[];

//---

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,TopBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ZeroBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BottomBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,MACDBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,SignalBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,Inp_MACD_signal_period-1);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"MACD Top Zero Bottom("+

                      string(Inp_MACD_fast_ema_period)+","+

                      string(Inp_MACD_slow_ema_period)+","+

                      string(Inp_MACD_signal_period)+")");

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

   PlotIndexSetInteger(0,PLOT_ARROW,InpTopArrowCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpZeroArrowCode);

   PlotIndexSetInteger(2,PLOT_ARROW,InpBottomArrowCode);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,InpTopArrowShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,0);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,InpBottomArrowShift);

//--- setting as an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

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

     }

//---

   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 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,SignalBuffer,handle_iMACD,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   if(limit<1)

     {

      limit=1;

      TopBuffer[0]=0.0;

      ZeroBuffer[0]=0.0;

      BottomBuffer[0]=0.0;

     }

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

     {

      TopBuffer[i]=TopBuffer[i-1];

      ZeroBuffer[i]=ZeroBuffer[i-1];

      BottomBuffer[i]=BottomBuffer[i-1];

      if(MACDBuffer[i]>0.0)

        {

         BottomBuffer[i]=0.0;

         if(MACDBuffer[i-1]!=EMPTY_VALUE)

           {

            if(MACDBuffer[i-1]<MACDBuffer[i])

               if(TopBuffer[i]<high[i] || TopBuffer[i]==0.0)

                  TopBuffer[i]=high[i];

           }

        }

      else

        {

         TopBuffer[i]=0.0;

         if(MACDBuffer[i-1]!=EMPTY_VALUE)

           {

            if(MACDBuffer[i-1]>MACDBuffer[i])

               if(BottomBuffer[i]>low[i] || BottomBuffer[i]==0.0)

                  BottomBuffer[i]=low[i];

           }

        }

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

        {

         ZeroBuffer[i]=(high[i]+low[i])/2.0;

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iMACD indicator               |

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

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

                           double &signal_buffer[],  // indicator buffer of the signal line of MACD

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

     }

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

   if(CopyBuffer(ind_handle,1,0,amount,signal_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);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMACD!=INVALID_HANDLE)

      IndicatorRelease(handle_iMACD);

  }

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

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