MACD Levels

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

//|                                                   MACD Levels.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property description "Moving Average Convergence/Divergence and lines"

#property description "Levels of support and resistance"

#include <MovingAverages.mqh>

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   2

#property indicator_label1  "Resistance" 

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID 

#property indicator_width1  2

#property indicator_label2  "Support" 

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID 

#property indicator_width2  2

//--- input parameters

input int                  InpFastEMA=12;                // Fast EMA period

input int                  InpSlowEMA=26;                // Slow EMA period

input int                  InpSignalSMA=9;               // Signal SMA period

input ENUM_APPLIED_PRICE   InpAppliedPrice=PRICE_CLOSE;  // Applied price

input ushort               InpResistanceCode=159;        // Resistance code arrow

input ushort               InpSupportCode=159;           // Support code arrow

//--- indicator buffers

double                     ExtResistanceBuffer[];

double                     ExtSupportBuffer[];

double                     ExtMacdBuffer[];

double                     ExtSignalBuffer[];

double                     ExtFastMaBuffer[];

double                     ExtSlowMaBuffer[];

//--- MA handles

int                        ExtFastMaHandle;

int                        ExtSlowMaHandle;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtResistanceBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtSupportBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtMacdBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtSignalBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtFastMaBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,InpSupportCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpResistanceCode);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,5);

//--- An empty value 

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"MACD Levels("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")");

//--- get MA handles

   ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice);

   ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);

//--- initialization done

  }

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

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

      return(0);

//--- not all data may be calculated

   int calculated=BarsCalculated(ExtFastMaHandle);

   if(calculated<rates_total)

     {

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

      return(0);

     }

   calculated=BarsCalculated(ExtSlowMaHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtSlowMaHandle 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(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0)

     {

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

      return(0);

     }

//--- get SlowSMA buffer

   if(IsStopped()) return(0); //Checking for stop flag

   if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)

     {

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

      return(0);

     }

//---

   int limit;

   if(prev_calculated==0)

      limit=0;

   else limit=prev_calculated-1;

//--- calculate MACD

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

      ExtMacdBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];

//--- calculate Signal

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

//--- draw or move line

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

     {

      if(i<3)

        {

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

           {

            ExtResistanceBuffer[j]=0.0;

            ExtSupportBuffer[j]=0.0;

           }

         i=3;

        }

      if(ExtResistanceBuffer[i-1]!=0.0)

         ExtResistanceBuffer[i]=ExtResistanceBuffer[i-1];

      else

         ExtResistanceBuffer[i]=0.0;

      if(ExtSupportBuffer[i-1]!=0.0)

         ExtSupportBuffer[i]=ExtSupportBuffer[i-1];

      else

         ExtSupportBuffer[i]=0.0;



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

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

           {

            ExtResistanceBuffer[i]=high[i];

           }

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

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

           {

            ExtSupportBuffer[i]=low[i];

           }

     }

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