Incorrect Signals for MA crossings

 

Hi All,

Apologies if this is a silly query, I'm still a bit of a newbie.

I have coded-up a simple strategy that uses the crossing of MAs to signal a buy/sell signal. I have utilised a lot of the concepts seen in the MACD Sample file that comes with metatrader.

My MA variables are defined as follows:

   EMA12_current = iMA(NULL,0,12,0,1,PRICE_CLOSE,0);

   EMA12_previous = iMA(NULL,0,12,0,1,PRICE_CLOSE,1);

   SA26_current = iMA(NULL,0,26,0,0,PRICE_CLOSE,0);

   SA26_previous = iMA(NULL,0,26,0,0,PRICE_CLOSE,1);

If EMA12 crosses below SA26 then a buy order should be closed and a sell order opened. The piece of code below does the closing portion:

         if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())     // check for an opened position and symbol.
           {
               if(OrderType() == OP_BUY)
                 {
                     if(EMA12_current < SA26_current && EMA12_previous > SA26_previous)     // check to see if it should be closed.
                       {
                           if(!OrderClose(OrderTicket(),OrderLots(),Bid,1000,Violet))
                             {
                                 Print("OrderClose error ", GetLastError());

                             }

Most of the time it works however, there are occasions where the lines don't cross and the order is closed or opened, as seen in the screenshot.

How do I get this working properly?

Thanks.

 
bateMan: 

Most of the time it works however, there are occasions where the lines don't cross and the order is closed or opened, as seen in the screenshot.

How do I get this working properly?


  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.
  2. No screenshot
  3. They can cross many times as price moves up and down during the current candle. Use bars 1 and 2.
Reason: