Help me to code this.

 
I want to know how many bars ago do two moving average cross each other. Like MA(8) and MA(14).
Can anyone please code this for me?
 
s_ad:
I want to know how many bars ago do two moving average cross each other. Like MA(8) and MA(14).
Can anyone please code this for me?

It sounds like you need something like the following:

// Does a simple brute-force search to find the offset of the bar in which two moving averages most
// recently crossed. Returns 0 if the cross is in the current bar, e.g. if MA #1 is higher than 
// MA #2 at the moment, but was below it at the end of the last bar. Returns -1 if no cross has 
// occurred during the period defined by MaxBars. The time etc of the cross can obviously be
// determined by doing Time[n] etc on the bar-offset returned by this function. Moving averages
// can obviously cross and then re-cross (or "un-cross") during a bar. Therefore, if this function
// returns 0, indicating a cross in the current bar, then that may change again before the 
// current bar finishes. To ignore the current bar, and only look at completed bars and "confirmed"
// crosses, use the optional third parameter for the function to tell it to start at bar 1, not bar 0

int FindBarOffsetOfMACross(int MAPeriod1, int MAPeriod2, int StartAtBar = 0)
{
   // Parameters to use for the moving average. Can be turned into extern parameters etc.
   int MovingAverageType = MODE_SMA;
   int MovingAveragePrice = PRICE_CLOSE;
   
   // Maximum number of bars to look backwards. Could also be an extern parameter.
   int MaxBars = 1000;
   
   // Get the current values of the two moving averages
   double CurrentMA1 = iMA(Symbol(), Period(), MAPeriod1, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 
   double CurrentMA2 = iMA(Symbol(), Period(), MAPeriod2, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 

   // Loops backwards through the bar history looking at the MA values    
   for (int i = StartAtBar + 1; i <= MaxBars; i++) {
      // Get the historic values of the two moving averages, i bars ago
      double PreviousMA1 = iMA(Symbol(), Period(), MAPeriod1, 0, MovingAverageType, MovingAveragePrice, i); 
      double PreviousMA2 = iMA(Symbol(), Period(), MAPeriod2, 0, MovingAverageType, MovingAveragePrice, i); 
   
      // A cross has occurred if the current value #1 is now higher than #2, but was lower i bars ago. Or if
      // #1 is currently lower than #2, but was higher i bars ago. 
      if ( (CurrentMA1 > CurrentMA2 && PreviousMA1 < PreviousMA2) || (CurrentMA1 < CurrentMA2 && PreviousMA1 > PreviousMA2) ) return (i - 1);
   }

   // No cross during the maximum period defined by MaxBars
   return (-1);   
}
 
Slightly more efficient code
double CurrentMA1 = iMA(Symbol(), Period(), MAPeriod1, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 
double CurrentMA2 = iMA(Symbol(), Period(), MAPeriod2, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 
double CurrentDir = CurrentMA1 - CurrentMA2;
   // Loops backwards through the bar history looking at the MA values    
   for (int i = StartAtBar + 1; i <= MaxBars; i++) {
      // Get the historic values of the two moving averages, i bars ago
      double PreviousMA1 = iMA(Symbol(), Period(), MAPeriod1, 0, MovingAverageType, MovingAveragePrice, i); 
      double PreviousMA2 = iMA(Symbol(), Period(), MAPeriod2, 0, MovingAverageType, MovingAveragePrice, i); 
      double PreviousDir = PreviousMA1 - PreviousMA2;   
      // A cross has occurred if the current value #1 is now higher than #2, but was lower i bars ago. Or if
      // #1 is currently lower than #2, but was higher i bars ago. 
      if ( CurentDir*PreviousDir < 0) return (i - 1);
//    if ( (CurrentMA1 > CurrentMA2 && PreviousMA1 < PreviousMA2) || (CurrentMA1 < CurrentMA2 && PreviousMA1 > PreviousMA2) ) return (i - 1);
   }
 
string lsSymbol = Symbol();
int liPeriod = Period();

double CurrentMA1 = iMA(lsSymbol, liPeriod, MAPeriod1, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 
double CurrentMA2 = iMA(lsSymbol, liPeriod, MAPeriod2, 0, MovingAverageType, MovingAveragePrice, StartAtBar); 
double CurrentDir = CurrentMA1 - CurrentMA2;

double PreviousMA1 = 0;
double PreviousMA2 = 0;
double PreviousDir = 0;

   // Loops backwards through the bar history looking at the MA values    
   for (int i = StartAtBar + 1; i <= MaxBars; i++) {
      // Get the historic values of the two moving averages, i bars ago
      PreviousMA1 = iMA(lsSymbol, liPeriod, MAPeriod1, 0, MovingAverageType, MovingAveragePrice, i); 
      PreviousMA2 = iMA(lsSymbol, liPeriod, MAPeriod2, 0, MovingAverageType, MovingAveragePrice, i); 
      PreviousDir = PreviousMA1 - PreviousMA2;   
      // A cross has occurred if the current value #1 is now higher than #2, but was lower i bars ago. Or if
      // #1 is currently lower than #2, but was higher i bars ago. 
      if ( CurentDir*PreviousDir < 0) return (i - 1);
//    if ( (CurrentMA1 > CurrentMA2 && PreviousMA1 < PreviousMA2) || (CurrentMA1 < CurrentMA2 && PreviousMA1 > PreviousMA2) ) return (i - 1);
   }
 
WHRoeder:
Slightly more efficient code [...]

If we're looking to sacrifice clarity in favour of trivial speed gains - regardless of how confusing this may be for someone asking for pretty basic help - then it's slightly faster to do something like this than to multiply double values:

bool bNowUp = (CurrentMA1 > CurrentMA2);

[...]

     bool bWasUp = (PreviousMA1 > PreviousMA2);
     if (bNowUp != bWasUp) return (i-1);
 
Thanks for the code.
Reason: