how to compare two consecutive peaks on indicator value

 

Hi

I tried to make an indicator that shows MACD trend. Can anyone tell me why MACD_trend function is not working?

#include <Ehsan\\Objects.mqh>
#property indicator_chart_window
#property strict
#property indicator_buffers 6
//----------------------------------------------------------------------[ Line properties ]
//A
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrWhite
#property indicator_width1 2
//B
#property indicator_type2 DRAW_NONE
#property indicator_style2 STYLE_SOLID
#property indicator_color2 clrGreen
#property indicator_width2 1
//C
#property indicator_type3 DRAW_NONE
#property indicator_style3 STYLE_SOLID
#property indicator_color3 clrGreen
#property indicator_width3 1
//D
#property indicator_type4 DRAW_NONE
#property indicator_style4 STYLE_SOLID
#property indicator_color4 clrLime
#property indicator_width4 1
//E
#property indicator_type5 DRAW_LINE
#property indicator_style5 STYLE_SOLID
#property indicator_color5 clrLime
#property indicator_width5 2
//F
#property indicator_type6 DRAW_LINE
#property indicator_style6 STYLE_SOLID
#property indicator_color6 clrRed
#property indicator_width6 2
//----------------------------------------------------------------------[ parameters ]
//MACD12 - 5m
int f12=12;   //MACD12- Fast EMA
int s12=26;   //MACD12- Small EMA
int p12=9;    //MACD12- MACD SMA


//vars
double lineA[], lineB[], lineC[], lineD[], lineE[], lineF[];
int counter;
datetime TimeLastUpdate;
int newpeak, oldpeak, newval, oldval;
bool condBu, condBe;
//----------------------------------------------------------------------[ OnInit ]
int OnInit(){

//---delete objects
ObjectsDeleteAll( 0 );

SetIndexBuffer(0, lineA);
SetIndexBuffer(1, lineB);
SetIndexBuffer(2, lineC);
SetIndexBuffer(3, lineD);
SetIndexBuffer(4, lineE);
SetIndexBuffer(5, lineF);

return(INIT_SUCCEEDED);}

//----------------------------------------------------------------------[ OcCalculate ]
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[]) {
int limit;
if( prev_calculated == 0) limit = rates_total - 1;
else limit= rates_total-prev_calculated;

MACD_Trend( f12, s12, p12, limit, 
            counter, 
            lineA, lineB, lineC, lineD, 
            oldpeak, oldval, condBu, condBe, 
            rates_total, prev_calculated);


//for( int i=limit-1; i>=0 && !IsStopped();i--)
//{
//
//if(lineD[i]!=EMPTY_VALUE)
//{  
//   int stat= (int) lineD[i];
//   
//   if(stat==1) lineE[i]= imacd_M(f12, s12, p12, i)+10*Point;
//   if(stat==0) lineF[i]= imacd_M(f12, s12, p12, i)-10*Point;
//}
//
//}



return(rates_total-1); }

//----------------------------------------------------------------------[ Function ]


void MACD_Trend ( int f, int s, int p, 
                  int limit,
                  int &lastCrs,
                  double &input_Buffer[], 
                  double &peaksBuffer[], 
                  double &valleysBuffer[], 
                  double &trendBuffer[],
                  int &peakOld, int &valleyOld,
                  bool &conBu, bool &conBe,
                  int ratesT, int prevC )
{   
static datetime candleTime;
if(Time[0]==candleTime) return;

int peakNew, valleyNew, limit1=0;

MACD_PV(f, s, p, limit, lastCrs, input_Buffer, peaksBuffer, valleysBuffer, ratesT, prevC);
   
   
if (prevC==0)
{

}


else
{  
   int limit2= ratesT-prevC;
   for( int i=0; i<limit2 ;i++)
   { 
      //---Bearish
      if(valleysBuffer[i]!=EMPTY_VALUE)
         {       
          valleyNew= ratesT-i-1-(int)valleysBuffer[i];
          DrawLine(3,clrBlue,"", ratesT-i-1-valleyOld);
          valleyOld= ratesT-i-1-(int)valleysBuffer[i];
         }

     //---Bullish
     if(peaksBuffer[i]!=EMPTY_VALUE)
        {
          peakNew= ratesT-i-1-(int)peaksBuffer[i];
          DrawLine(3,clrBlue,"", ratesT-i-1-peakOld);
          peakOld= ratesT-i-1-(int)peaksBuffer[i];
       }
   }
}
 
candleTime=Time[0];
}

//----------------------------------------------------------------------[ iMACD ]
double imacd_S(int FastEMA, int SlowEMA, int SignalPeriod, int i)
{return iMACD(Symbol(), Period(), FastEMA, SlowEMA, SignalPeriod, PRICE_CLOSE, MODE_SIGNAL, i);}

double imacd_M(int FastEMA, int SlowEMA, int SignalPeriod, int i)
{return iMACD(Symbol(), Period(), FastEMA, SlowEMA, SignalPeriod, PRICE_CLOSE, MODE_MAIN, i);}

//-----------------------------------------------------------------------[Cross]
bool MACD_Cross_Up ( int f, int s, int p, int i )
{
bool x;

if(    imacd_M(f, s, p, i+2)<imacd_S(f, s, p, i+2) 
    && imacd_M(f, s, p, i+1)>imacd_S(f, s, p, i+1) ) x=1;
else x=0;

return x;
}


bool MACD_Cross_Down ( int f, int s, int p, int i )
{
bool y;

if(    imacd_M(f, s, p, i+2)>imacd_S(f, s, p, i+2)
    && imacd_M(f, s, p, i+1)<imacd_S(f, s, p, i+1) ) y=1;
else y=0;

return y;
}

//-----------------------------------------------------------------------[Peaks-Valleys]
void MACD_PV ( int f,int s, int p,
               int limit,
               int &lastCrs, 
               double &inputBuffer[], 
               double &peaksBuffer[], 
               double &valleysBuffer[],
               int ratesT, int prevC )
{
   
   for( int i=limit-1; i>=0 && !IsStopped();i--)
   { inputBuffer[i]= imacd_M(f, s, p, i ); }


int Peak, Valley;
   
   
   for( int i=limit-1; i>=0 && !IsStopped();i--)
   {
      if (MACD_Cross_Down(f, s, p, i)) 
      { 
        Peak= ArrayMaximum(inputBuffer, ratesT-i-1-lastCrs , i+1);
        lastCrs=ratesT-i-1; 
        peaksBuffer[i]= Peak;
      }  

      if (MACD_Cross_Up(f, s, p, i)) 
      { 
        Valley= ArrayMinimum(inputBuffer, ratesT-i-1-lastCrs , i+1);
        lastCrs=ratesT-i-1;
        valleysBuffer[i]=Valley;
      } 
   }
}
Reason: