MACDOnDEMA Four Colors

Author: Copyright © 2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
MACDOnDEMA Four Colors
ÿþ//+------------------------------------------------------------------+

//|                                       MACDOnDEMA Four Colors.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.000"

#property description "Moving Average Convergence/Divergence on Double Exponential Moving Average indicators"

#include <MovingAverages.mqh>

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   2

//--- plot MACD

#property indicator_label1  "MACD"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrSilver,clrRed,clrBlue,clrOrange,clrYellow

#property indicator_style1  STYLE_SOLID

#property indicator_width1  3

//--- plot Signal

#property indicator_label2  "Signal"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  3

//--- input parameters

input group             "MACD"

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

input int                  Inp_MACD_slow_dema_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

//--- indicator buffers

double   ExtMacdBuffer[];

double   ExtMacdColorsBuffer[];

double   ExtSignalBuffer[];

double   ExtFastDEMABuffer[];

double   ExtSlowDEMABuffer[];

//---

int      ExtFastDEMAHandle;

int      ExtSlowDEMAHandle;

//---

int      m_start=0;

bool     m_init_error=false;  // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Inp_MACD_fast_dema_period>=Inp_MACD_slow_dema_period)

     {

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

                      "The 'Fast' period cannot be longer than the 'Slow' period!":

                      "'Fast' ?5@8>4 =5 <>65B 1KBL 1>;LH5 ?5@8>40 'Slow'!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

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

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

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

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtMacdColorsBuffer,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtSignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtFastDEMABuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtSlowDEMABuffer,INDICATOR_CALCULATIONS);

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

   m_start=Inp_MACD_slow_dema_period*3+1;

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,m_start);

//--- name for indicator subwindow label

   string short_name=StringFormat("MACDOnDEMA(%d,%d,%d)",Inp_MACD_fast_dema_period,Inp_MACD_slow_dema_period,Inp_MACD_signal_period);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- get MA handles

   ExtFastDEMAHandle=iDEMA(Symbol(),Period(),Inp_MACD_fast_dema_period,0,Inp_MACD_applied_price);

   ExtSlowDEMAHandle=iDEMA(Symbol(),Period(),Inp_MACD_slow_dema_period,0,Inp_MACD_applied_price);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Moving Averages Convergence/Divergence                           |

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

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

      return(0);

//--- not all data may be calculated

   int calculated=BarsCalculated(ExtFastDEMAHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtFastDEMAHandle is calculated (",calculated," bars). Error ",GetLastError());

      return(0);

     }

   calculated=BarsCalculated(ExtSlowDEMAHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtSlowDEMAHandle is calculated (",calculated," bars). Error ",GetLastError());

      return(0);

     }

//--- we can copy not all data

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<0)

      to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      if(prev_calculated>0)

         to_copy++;

     }

//--- get Fast DEMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtFastDEMAHandle,0,0,to_copy,ExtFastDEMABuffer)<=0)

     {

      Print("Getting fast DEMA is failed! Error ",GetLastError());

      return(0);

     }

//--- get Slow DEMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtSlowDEMAHandle,0,0,to_copy,ExtSlowDEMABuffer)<=0)

     {

      Print("Getting slow DEMA is failed! Error ",GetLastError());

      return(0);

     }

//---

   int start;

   if(prev_calculated==0)

     {

      start=m_start;

      for(int j=0; j<start; j++)

        {

         ExtMacdBuffer[j]=0.0;

         ExtSignalBuffer[j]=0.0;

        }

     }

   else

      start=prev_calculated-1;

//--- calculate MACD

   for(int i=start; i<rates_total && !IsStopped(); i++)

      ExtMacdBuffer[i]=ExtFastDEMABuffer[i]-ExtSlowDEMABuffer[i];

//--- calculate Signal

   SimpleMAOnBuffer(rates_total,prev_calculated,0,Inp_MACD_signal_period,ExtMacdBuffer,ExtSignalBuffer);

//--- main loop

   for(int i=start; i<rates_total && !IsStopped(); i++)

     {

      ExtMacdColorsBuffer[i]=0.0;

      /*

       'MACD' > previous MACD && 'MACD' < 0.0 -> 'Color'=1.0

       'MACD' > previous MACD && 'MACD' > 0.0 -> 'Color'=2.0

       'MACD' < previous MACD && 'MACD' > 0.0 -> 'Color'=3.0

       'MACD' < previous MACD && 'MACD' < 0.0 -> 'Color'=4.0

      */

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

         ExtMacdColorsBuffer[i]=1.0;

      else

         if(ExtMacdBuffer[i]>ExtMacdBuffer[i-1] && ExtMacdBuffer[i]>0.0)

            ExtMacdColorsBuffer[i]=2.0;

         else

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

               ExtMacdColorsBuffer[i]=3.0;

            else

               if(ExtMacdBuffer[i]<ExtMacdBuffer[i-1] && ExtMacdBuffer[i]<0.0)

                  ExtMacdColorsBuffer[i]=4.0;

     }

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

   return(rates_total);

  }

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

Comments