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

//|                                                   MACDOnTEMA.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 Triple Exponential Moving Average indicators"

#include <MovingAverages.mqh>

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot MACD

#property indicator_label1  "MACD"

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_color1  clrSilver

#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_tema_period  = 12;          // MACD: period for Fast average calculation

input int                  Inp_MACD_slow_tema_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   ExtSignalBuffer[];

double   ExtFastTEMABuffer[];

double   ExtSlowTEMABuffer[];

//---

int      ExtFastTEMAHandle;

int      ExtSlowTEMAHandle;

//---

int      m_start=0;

bool     m_init_error=false;  // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Inp_MACD_fast_tema_period>=Inp_MACD_slow_tema_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,ExtSignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtFastTEMABuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtSlowTEMABuffer,INDICATOR_CALCULATIONS);

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

   m_start=Inp_MACD_slow_tema_period*3+1;

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,m_start);

//--- name for indicator subwindow label

   string short_name=StringFormat("MACDOnTEMA(%d,%d,%d)",Inp_MACD_fast_tema_period,Inp_MACD_slow_tema_period,Inp_MACD_signal_period);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- get MA handles

   ExtFastTEMAHandle=iTEMA(Symbol(),Period(),Inp_MACD_fast_tema_period,0,Inp_MACD_applied_price);

   ExtSlowTEMAHandle=iTEMA(Symbol(),Period(),Inp_MACD_slow_tema_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(ExtFastTEMAHandle);

   if(calculated<rates_total)

     {

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

      return(0);

     }

   calculated=BarsCalculated(ExtSlowTEMAHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtSlowTEMAHandle 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 TEMA buffer

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

      return(0);

   if(CopyBuffer(ExtFastTEMAHandle,0,0,to_copy,ExtFastTEMABuffer)<=0)

     {

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

      return(0);

     }

//--- get Slow TEMA buffer

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

      return(0);

   if(CopyBuffer(ExtSlowTEMAHandle,0,0,to_copy,ExtSlowTEMABuffer)<=0)

     {

      Print("Getting slow TEMA 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]=ExtFastTEMABuffer[i]-ExtSlowTEMABuffer[i];

//--- calculate Signal

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

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

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

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