MTF MACD

 

Hello, I am relatively new to MTF coding and I am having a challenge in making the MTF MACD display the signal line. I have managed display the MACD line, but the Signal Line is not showing due the wrong MTF reading. I will appreciate your help. Here is the complete code for the indicator I am editing. Thanks in advance.

Apparently, the problem is on this section.

   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; //
   MT_SignalLineBuffer[i] =  alpha*MT_MACDLineBuffer[i] + alpha_1*MT_SignalLineBuffer[i+1];
   }

Here is the entire code.

//+------------------------------------------------------------------+
//|                                                         MACD.mq4 |
//|                                Copyright © 2005, David W. Thomas |
//|                                            |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright © 2005, David W. Thomas"
#property link      "mailto"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Green
#property indicator_color4 Red
#property indicator_color5 LightBlue
//#property indicator_color6 Yellow



//---- input parameters
extern int       FastMAPeriod=12;
extern int       SlowMAPeriod=26;
extern int       SignalMAPeriod=9;

extern int       MT_FastMAPeriod=12;
extern int       MT_SlowMAPeriod=26;
extern int       MT_SignalMAPeriod=9;
enum enum_TimeFrames
      {
         Current = PERIOD_CURRENT,
         Minute_1 = PERIOD_M1,
         Minute_5 = PERIOD_M5,
         Minute_15 = PERIOD_M15,
         Minute_30 = PERIOD_M30,
         Hour_1 = PERIOD_H1,
         Hour_4 = PERIOD_H4,
         Day_1 = PERIOD_D1,
         Week_1 = PERIOD_W1,
         Month_1 = PERIOD_MN1
      };  
extern enum_TimeFrames  MT_TimeFrame= Hour_4;     //GTrend_MultiTimeframe


//extern int MT_FastMAPeriod1 = 12;
//extern int MT_SlowMAPeriod1 = 26;
//extern int MT_SignalLine1Period1 = 9;
//extern enum_TimeFrames MT_TimeFrame1 = Hour_4;      //MACD/Signal_MultiTimeframe


//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double MT_MACDLineBuffer[];
double MT_SignalLineBuffer[];
double HistogramBuffer[];
double MT_MACDLineBuffer1[];
double MT_SignalLineBuffer1[];

//---- variables
double alpha = 0;
double alpha_1 = 0;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MACDLineBuffer);
   SetIndexDrawBegin(0,SlowMAPeriod);
   
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,SignalLineBuffer);
   SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
   
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,HistogramBuffer);
   SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,MT_MACDLineBuffer);
   SetIndexDrawBegin(4,SlowMAPeriod);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MT_SignalLineBuffer);
   SetIndexDrawBegin(3,SlowMAPeriod+SignalMAPeriod);
   

  /* 
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,MT_MACDLineBuffer1);
   SetIndexDrawBegin(5,MT_SlowMAPeriod1);
   
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,MT_SignalLineBuffer1);
   SetIndexDrawBegin(6,MT_SlowMAPeriod1+MT_SignalLine1Period1);
   */
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
   SetIndexLabel(2,"MTF MACD");
   SetIndexLabel(3,"MTF Signal");
   SetIndexLabel(4,"Histogram");
   SetIndexLabel(5,"MTF MACD1");
   SetIndexLabel(6,"MTF Signal1");
   //----
        alpha = 2.0 / (SignalMAPeriod + 1.0);
        alpha_1 = 1.0 - alpha;
        
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //---- 
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{

   
   datetime TimeArray[];
   int    i,y=0,a=0,limit,counted_bars=IndicatorCounted();

   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;
   
   for(i=limit; i>=0; i--)
      {
      MACDLineBuffer[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
      HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
      }
 // Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),MT_TimeFrame); 
        
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; 
   MT_MACDLineBuffer[i] =iMA(NULL,MT_TimeFrame,MT_FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,y)-iMA(NULL,MT_TimeFrame,MT_SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,y);
   }
      
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; //
   MT_SignalLineBuffer[i] =  alpha*MT_MACDLineBuffer[i] + alpha_1*MT_SignalLineBuffer[i+1];//****NOT DISPLAYING THE MTF SIGNAL LINE.****
   }
   

   
   return(0);

}
//+------------------------------------------------------------------+

MACD - Oscillators - MetaTrader 5 Help
MACD - Oscillators - MetaTrader 5 Help
  • www.metatrader5.com
Moving Average Convergence/Divergence (MACD) is a trend-following dynamic indicator. It indicates the correlation between two Moving Averages of a...
 
ALFRED MURIITHI:

Hello, I am relatively new to MTF coding and I am having a challenge in making the MTF MACD display the signal line. I have managed display the MACD line, but the Signal Line is not showing due the wrong MTF reading. I will appreciate your help. Here is the complete code for the indicator I am editing. Thanks in advance.

Apparently, the problem is on this section.

Here is the entire code.

Got the issue in the limits and fixed it.