Predicting EMA Crossover formula help

 

I found a fun read here (article) on predicting EMA crossovers. If I understood it correctly, for EMAs, we can see a recurrence relation between yesterday's EMA and today's EMA, and by shifting this relation forward, we can solve to figure out the approximate price needed for tomorrow to close in order for the EMAs to cross (derivation here btw). I tried to implement this with an EA (so I could see results on the strategy tester). I expected it to give some false signals (obviously), but it also misses a good number of signals, which I don't think it should do if implemented correctly. Could someone take a look at the formula and my code and try to find the mistake? Also, in order to get it to work somewhat well, I had to use Close[0], which I don't want to do. Will that cause problems?

The code that matters:

int start(){
   if (!NewBar()) return(0);
   double f = FastMAPeriod;
   double s = SlowMAPeriod; //simplify for typing
   int t = 0; //what time shift is "today"?
   int y = 1; //time shift for "yesterday"
   //calculate the price needed for today (the most recent bar close)
   //and yesterday (the bar close before that)
   double fast_MA_today = iMA(Symbol(),0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,t);
   double slow_MA_today = iMA(Symbol(),0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,t);
   double fast_MA_yester = iMA(Symbol(),0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,y);
   double slow_MA_yester = iMA(Symbol(),0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,y);
   double price_needed_today = ((f-1)*(s+1)*fast_MA_today-
                        (f+1)*(s-1)*slow_MA_today)/(2*(f-s));
   double price_needed_yester = ((f-1)*(s+1)*fast_MA_yester-
                        (f+1)*(s-1)*slow_MA_yester)/(2*(f-s));
   //check for signals
   if (fast_MA_today < slow_MA_today && price_needed_today <= Close[t] && price_needed_yester > Close[y]) {
      ObjectCreate(DoubleToStr(ArrID,1),OBJ_ARROW,0,iTime(NULL,0,0),price_needed_today);
      ObjectSet(DoubleToStr(ArrID,1),OBJPROP_COLOR,Green); //green for long. duh.
      ArrID++;
      Alert("");
   } else if (fast_MA_today > slow_MA_today && price_needed_today >= Close[t] && price_needed_yester < Close[y]) {
      ObjectCreate(DoubleToStr(ArrID,1),OBJ_ARROW,0,iTime(NULL,0,0),price_needed_today);
      ArrID++;
      Alert("");
   }
   return(0);
}

Should be simple enough to understand. Tell me if I need to add more comments. FYI, t=1 y=2 provided the signal one bar late. t=2 y=3 provided the signal TWO bars late.

 
int t = 0; //what time shift is "today"?
double fast_MA_today = iMA(Symbol(),0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,t);
double slow_MA_today = iMA(Symbol(),0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,t);
double price_needed_today = ((f-1)*(s+1)*fast_MA_today-
                        (f+1)*(s-1)*slow_MA_today)/(2*(f-s));

The article is based on the stock market, which has a close. Forex doesn't. So Today=bar 1, and Tomorrow=bar 0 and that's what is being calculated, what the price must be tomorrow to have a cross over. T MUST be one.

Reason: