Detect when moving averages are about to cross

I don’t think he wants to look into future but is looking for near cross occurrences from below or near cross from above. “About to cross” translates to me as possibly “near to cross”. So where is the future look?

// scan for near cross from below or near cross from above of two MAs
/// @link http://forum.amibroker.com/t/detect-when-moving-averages-are-about-to-cross/1997/8

percent = 0.5;// within percent range
 
MA1 = MA( Close, 20 ); // shorter SMA
MA2 = MA( Close, 200 ); // longer SMA

slope = LinRegSlope(MA1, 2);// slope of MA1 of previous few bars

MA1_from_above = slope < 0 AND MA1>MA2;// MA1 coming from above (negative slope) and being higher than MA2
MA1_from_below = slope > 0 AND MA1<MA2;// MA1 coming from below (positive slope) and being lower than MA2

almost_equal = AlmostEqual( MA1, MA2, percent * 100000 );

MA_NearCross_up = MA1_from_below AND almost_equal;
MA_NearCross_dn = MA1_from_above AND almost_equal;

Buy = MA_NearCross_up;
Short = MA_NearCross_dn;
7 Likes