# Simple SMA Difference Problem

16

Hello.

I'm looking for a simple indicator wich gives me the difference of two simple moving averages wich I could set the period of each one.

On this indicator I´ll apply two simple moving averages and that's all.

I already tried to modify some MACD formulas but if I change the code from EMA to SMA and try to take out the signal line (or just set it to 0 and the color no NONE) I can´t apply any moving averages over it. If I try to apply EMA, it shows nothing. If I try to apply SMA, it shows bugged. Funny thing is that if I set it as EMA, I can apply the moving averages.

Bellow is the code that I'm using (original MACD code from MT5). I tryed to take out the Signal Line without changing too much the code.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

//|                                                         MACD.mq5 |

//|                        Copyright 2009, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

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

#property copyright   "2009, MetaQuotes Software Corp."

#property description "Moving Average Convergence/Divergence"

#include <MovingAverages.mqh>

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

#property indicator_type1   DRAW_LINE

//#property indicator_type2   DRAW_LINE

#property indicator_color1  YellowGreen

#property indicator_color2  CLR_NONE

#property indicator_width1  2

#property indicator_width2  1

#property indicator_label1  "MACD"

#property indicator_label2  "Signal"

//--- input parameters

input int                InpFastEMA=76;               // Fast EMA period

input int                InpSlowEMA=200;               // Slow EMA period

input int                InpSignalSMA=0;              // Signal SMA period

input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

//--- indicator buffers

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,ExtMacdBuffer,INDICATOR_DATA);

//SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA);

SetIndexBuffer(2,ExtFastMaBuffer,INDICATOR_CALCULATIONS);

SetIndexBuffer(3,ExtSlowMaBuffer,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("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")");

//--- get MA handles

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

ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_SMA,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[],

{

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

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

return(rates_total);

}

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

• www.mql5.com
Files:
MACD_EMA.png 250 kb
16

4984

bdfranca:

H i,

Signal period is only 1 not 0.

@+

4984

Philippe Houy:

H i,

Signal period is only 1 not 0.

@+

Files:
macdv1.mq5 5 kb
16

I tested your code and works fine but I still can´t apply a simples SMA over your MACD.

It happens the same thing that the picture (MACD_SMAoSMA.png )shows.

Is it possible to fix or it maybe a MT5 bug?

4984

bdfranca:

I tested your code and works fine but I still can´t apply a simples SMA over your MACD.

It happens the same thing that the picture (MACD_SMAoSMA.png )shows.

Is it possible to fix or it maybe a MT5 bug?

If you want a SMA signal, the best way is to use this indicator  colormacd.mq5.

https://www.mql5.com/en/code/398

ColorMACD
• 2011.08.18
• Nikolay Kositsin
• www.mql5.com
Colored MACD histogram with the signal line that changes its color according to the trend direction.
Files:
colormacd.mq5 18 kb
16

MANY THANKS FRIEND!
Problem solved!
61854

bdfranca:
MANY THANKS FRIEND!
Problem solved!
Or use your code with a small change
Files:
_test_2.mq5 6 kb