How to isolate these events? MA Cross over uncross same bar?

 

I'm working on a MA cross over EA and adding features here and there. One thing I have been trying to do is detect events where the moving averages cross while a bar is open, then uncross before bar close -- a fakeout. I am having a lot of trouble trying to figure out how to detect these events reliably and generate a trading signal.


Can anyone give me some insight? I understand what I want in words... I want the 1st cross over to trigger signal A, and if uncross before bar close trigger signal B. If A and B are true, open buy/sell stop order. I'm not sure the best way to do this and my previous attempts fail for various reasons, including not figuring out how to reset the signals at the right time. Is there a way to only look at bar close or only look at bar open?


Here is snip of my order execution code

         if (OrderType() == OP_BUY && CrossUncross && WS_Type == 3)
             {
              closeBuyTrade();
              openOrder = false;
              placeSellUncross();
 
             } 
         else if (OrderType() == OP_SELL && CrossUncross && WS_Type == 3)
             {
              closeSellTrade();
              openOrder = false;
              placeBuyUncross();
             } 
         else if (OrderType() == OP_SELLLIMIT && CrossUncross && WS_Type == 3)
             {
              DeletePendingOrder();
              openOrder = false;
              placeBuyUncross();  
             }  
         else if (OrderType() == OP_BUYLIMIT && CrossUncross && WS_Type == 3)
             {
              DeletePendingOrder();
              openOrder = false;
              placeSellUncross();
             }

Here is snip of my detection code

  if (LastActiontime == Time[0])
   {
    hasCrossed = false;
    isCrossed = false;
    }

   if ((MAFastPrevious < MASlowPrevious) && (MAFastCurrent > MASlowCurrent))        //fast MA crosses up over slow MA
      {
      buySignal1 = true;
      hasCrossed = true;
      }

   else if ((MAFastPrevious > MASlowPrevious) && (MAFastCurrent < MASlowCurrent))   //fast MA crosses down under slow MA
      {
      sellSignal1 = true;
      hasCrossed = true;
      }   

   if(LastActiontime != iTime (NULL,WS_Time,0))
      {
      if(((MAFastPrevious < MASlowPrevious) && (MAFastCurrent > MASlowCurrent)) || 
          ((MAFastPrevious > MASlowPrevious) && (MAFastCurrent < MASlowCurrent)))
          {
           isCrossed = true;
           }
          else isCrossed = false; 
      
      if (hasCrossed && !isCrossed)
         {
          CrossUncross = true;                  
          }
      else CrossUncross = false;
       
      LastActiontime = iTime(NULL,WS_Time,0);
      }          
                  


I know it is very inefficient and poorly written, I will be revising the entire EA as I learn MQL to make it better.


I would appreciate any help thank you!!

 
  1. Bear in mind that at bar[0] the ma[0], close[0], .. might change only open[0] and ma[1] are fix.
  2. What if you use open[0], high[0] or low[0] and close[0] and ma[1].
  3. In this case a fake-break would be: open[0]<ma[1] && high[0]>ma[1] && close[0]<ma[1]
  4. But I would wait until the bar is almost complete: (TimeCurrent()-time[0])/(PeriodSeconds(_Period) > 0.8
Reason: