MACD Custom Averaging

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MACD Custom Averaging
ÿþ//+------------------------------------------------------------------+

//|                                        MACD Custom Averaging.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 description "Moving Average Convergence/Divergence Custom Averaging"

#include <MovingAverages.mqh>

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_type2   DRAW_LINE

#property indicator_color1  clrSilver

#property indicator_color2  clrRed

#property indicator_width1  2

#property indicator_width2  1

#property indicator_label1  "MACD"

#property indicator_label2  "Signal"

//--- input parameters

input group             "MA Fast"

input int                  Inp_MA_Fast_ma_period     = 12;           // MA Fast: averaging period

input ENUM_MA_METHOD       Inp_MA_Fast_ma_method     = MODE_SMA;     // MA Fast: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price = PRICE_CLOSE;  // MA Fast: type of price

input group             "MA Slow"

input int                  Inp_MA_Slow_ma_period     = 26;           // MA Slow: averaging period

input ENUM_MA_METHOD       Inp_MA_Slow_ma_method     = MODE_SMA;     // MA Slow: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price = PRICE_CLOSE;  // MA Slow: type of price

input group             "MACD"

input int                  InpSignalSMA               = 9;           // Signal SMA period

//--- indicator buffers

double   MacdMainBuffer[];

double   MacdSignalBuffer[];

double   MaFastBuffer[];

double   MaSlowBuffer[];

//--- MA handles

int      handle_iMA_Fast;                             // variable for storing the handle of the iMA indicator

int      handle_iMA_Slow;                             // variable for storing the handle of the iMA indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,MacdMainBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,MacdSignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,MaFastBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,MaSlowBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,/*"MACD"+*/

                      " Fast("+IntegerToString(Inp_MA_Fast_ma_period)+","+EnumToString(Inp_MA_Fast_ma_method)+","+EnumToString(Inp_MA_Fast_applied_price)+")"+

                      " Slow("+IntegerToString(Inp_MA_Slow_ma_period)+","+EnumToString(Inp_MA_Slow_ma_method)+","+EnumToString(Inp_MA_Slow_applied_price)+")"+

                      ","+IntegerToString(InpSignalSMA));

//--- create handle of the indicator iMA

   handle_iMA_Fast=iMA(Symbol(),Period(),Inp_MA_Fast_ma_period,0,

                       Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price);

//--- if the handle is not created

   if(handle_iMA_Fast==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA ('Fast') indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_Slow=iMA(Symbol(),Period(),Inp_MA_Slow_ma_period,0,

                       Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price);

//--- if the handle is not created

   if(handle_iMA_Slow==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA ('Slow') indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   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[])

  {

//--- check for data

   if(rates_total<InpSignalSMA && rates_total<Inp_MA_Fast_ma_period)

      return(0);

//--- not all data may be calculated

   int calculated=BarsCalculated(handle_iMA_Fast);

   if(calculated<rates_total)

     {

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

      return(0);

     }

   calculated=BarsCalculated(handle_iMA_Slow);

   if(calculated<rates_total)

     {

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

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA_Fast,0,0,to_copy,MaFastBuffer)<=0)

     {

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

      return(0);

     }

//--- get SlowSMA buffer

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA_Slow,0,0,to_copy,MaSlowBuffer)<=0)

     {

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

      return(0);

     }

//---

   int limit;

   if(prev_calculated==0)

     {

      limit=Inp_MA_Fast_ma_period;

      double SmoothFactor=2.0/(1.0+Inp_MA_Fast_ma_period);

      MaFastBuffer[0]=close[0];

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

         MaFastBuffer[i]=close[i]*SmoothFactor+MaFastBuffer[i-1]*(1.0-SmoothFactor);

      //---

      limit=Inp_MA_Slow_ma_period;

      SmoothFactor=2.0/(1.0+Inp_MA_Slow_ma_period);

      MaSlowBuffer[0]=close[0];

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

         MaSlowBuffer[i]=close[i]*SmoothFactor+MaSlowBuffer[i-1]*(1.0-SmoothFactor);

      //---

      limit=0;

     }

   else

      limit=prev_calculated-1;

//--- calculate MACD

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

      MacdMainBuffer[i]=MaFastBuffer[i]-MaSlowBuffer[i];

//--- calculate Signal

   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,MacdMainBuffer,MacdSignalBuffer);

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

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