Why my MACD has two buffers?

 

I want to put TEMA and MACD together to use. But my code did not work well.

//+------------------------------------------------------------------+
//|         TEMA+MACD                                     testEA.mq5 |
//+------------------------------------------------------------------+
#include  <Trade\Trade.mqh>
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   2
//--- plot MACD
#property indicator_label1  "MACD"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  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  2

input int                InpFastEMA=2;               // Fast EMA period
input int                InpSlowEMA=5;               // Slow EMA period
input int                InpSignalSMA=2;              // Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

input    int      fastTEMA_Period=5;
input    int      slowTEMA_Period=30;
input    int      EA_Magic=234508;
input    double   Lot=0.01;

int      MACD_Handle;
int      fastTEMAHandle;
int      slowTEMAHandle;

double   MACDBuffer[];
double   SignalBuffer[];

double   fastTEMAVal[];
double   slowTEMAVal[];

CTrade trade;
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,fastTEMAVal,INDICATOR_DATA);
   SetIndexBuffer(3,slowTEMAVal,INDICATOR_DATA);
   ArraySetAsSeries(fastTEMAVal,true);
   ArraySetAsSeries(slowTEMAVal,true);
   MACD_Handle = iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice);
   fastTEMAHandle=iTEMA(NULL,0,fastTEMA_Period,0,PRICE_CLOSE);
   slowTEMAHandle=iTEMA(NULL,0,slowTEMA_Period,0,PRICE_CLOSE);
   if(fastTEMAHandle<0 || slowTEMAHandle<0 || MACD_Handle<0)
     {
      Alert("Error in creating handles. Wrong:",GetLastError(),"!!");
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(MACD_Handle);
   IndicatorRelease(fastTEMAHandle);
   IndicatorRelease(slowTEMAHandle);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
  }
//+------------------------------------------------------------------+
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[])
  {
   double MACD[];
   double Signal[];
   ArraySetAsSeries(MACD,true);
   ArraySetAsSeries(Signal,true);
   ArraySetAsSeries(MACDBuffer,true);
   ArraySetAsSeries(SignalBuffer,true);
   CopyBuffer(MACD_Handle,0,0,3000,MACD);
   CopyBuffer(MACD_Handle,1,0,3000,Signal);
   for(int i=0; i<3000; i++)
     {
      MACDBuffer[i]=MACD[i];
      SignalBuffer[i]=Signal[i];
     }
   return(rates_total);
  }

I set MACD buffer reverse then copied data into buffer. But after I test this code, I found there are two MACD but without signal line. As you can see from screencutting, the first MACD is right while the second MACD's data is actually the data of signal. And in the meantime, the signal data became 0. Then I cannot plot the signal line. 

Why this happened? I only set one MACD buffer and one MACD indicator_label in code. By the way, the second MACD window is set by using MT5's insert indicator window funtion. I just want to use this window's data to check whether I plot right. The problem is in the blue circle's data......

two MACDs

 
coderelease:

I want to put TEMA and MACD together to use. But my code did not work well.

I set MACD buffer reverse then copied data into buffer. But after I test this code, I found there are two MACD but without signal line. As you can see from screencutting, the first MACD is right while the second MACD's data is actually the data of signal. And in the meantime, the signal data became 0. Then I cannot plot the signal line. 

Why this happened? I only set one MACD buffer and one MACD indicator_label in code. By the way, the second MACD window is set by using MT5's insert indicator window funtion. I just want to use this window's data to check whether I plot right. The problem is in the blue circle's data......


COLOR_HISTOGRAM needs two buffers. It takes the signal line as color buffer for the previous histogram. check attached file.


Files:
test.mq5  7 kb
 

Really thank you! I am study MT5 alonly, problems made me a little bit  frustrated. Your reply helped me. Thank you very much!!


Yashar Seyyedin #:

COLOR_HISTOGRAM needs two buffers. It takes the signal line as color buffer for the previous histogram. check attached file.


 

Then I want to move MACD code from "int OnCalculate()"  into "void OnTick()", and delete the "int OnCalculate()"  . The code has been changed like this.

//+------------------------------------------------------------------+
//|         TEMA+MACD                                     testEA.mq5 |
//+------------------------------------------------------------------+
#include  <Trade\Trade.mqh>

input int                InpFastEMA=2;               // Fast EMA period
input int                InpSlowEMA=5;               // Slow EMA period
input int                InpSignalSMA=2;              // Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

input    int      fastTEMA_Period=5;
input    int      slowTEMA_Period=30;
input    int      EA_Magic=234508;
input    double   Lot=0.01;

int      MACD_Handle;
int      fastTEMAHandle;
int      slowTEMAHandle;

double   MACDBuffer[];
double   SignalBuffer[];
double   fastTEMAVal[];
double   slowTEMAVal[];

CTrade trade;
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,fastTEMAVal,INDICATOR_DATA);
   SetIndexBuffer(3,slowTEMAVal,INDICATOR_DATA);
  
   ArraySetAsSeries(fastTEMAVal,true);
   ArraySetAsSeries(slowTEMAVal,true);
   
   MACD_Handle = iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice);
   fastTEMAHandle=iTEMA(NULL,0,fastTEMA_Period,0,PRICE_CLOSE);
   slowTEMAHandle=iTEMA(NULL,0,slowTEMA_Period,0,PRICE_CLOSE);
   if(fastTEMAHandle<0 || slowTEMAHandle<0 || MACD_Handle<0)
     {
      Alert("Error in creating handles. Wrong:",GetLastError(),"!!");
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(MACD_Handle);
   IndicatorRelease(fastTEMAHandle);
   IndicatorRelease(slowTEMAHandle);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTick           latest_tick;
   MqlTradeRequest   mrequest;
   MqlTradeResult    mresult;
   ZeroMemory(mrequest);
   double MACD[];
   double Signal[];
   ArraySetAsSeries(MACD,true);
   ArraySetAsSeries(Signal,true);
   ArraySetAsSeries(MACDBuffer,true);
   ArraySetAsSeries(SignalBuffer,true);
   CopyBuffer(MACD_Handle,0,0,3000,MACD);
   CopyBuffer(MACD_Handle,1,0,3000,Signal);
   for(int i=0; i<3000; i++)
     {
      MACDBuffer[i]=MACD[i];
      SignalBuffer[i]=Signal[i];
     }
  bool Buy_Condition;
  bool Sell_Condition;
  if (Buy_Condition)  {
     }
  if (Sell_Condition){
     }
}

But when I put this code into back test,  there is an "array out of range" error where is in the " MACDBuffer[i]" in for loop.

array out of range error

Why this happened?  This for loop has been checked in "int OnCalculate()" where it worked well. But this for loop cannot work well in  " "void OnTick()".....

 
coderelease #:

Then I want to move MACD code from "int OnCalculate()"  into "void OnTick()", and delete the "int OnCalculate()"  . The code has been changed like this.

But when I put this code into back test,  there is an "array out of range" error where is in the " MACDBuffer[i]" in for loop.

Why this happened?  This for loop has been checked in "int OnCalculate()" where it worked well. But this for loop cannot work well in  " "void OnTick()".....

You are not allowed to use OnTick in indicators.
Reason: