My simple MACD EA seems not works correctly. But why?

 

Hi,

I am new by EA. I try to build a very simple EA such as using the MACD to check if there is a golden cross (MACD crosses over the signal). If there is golden cross, go long. If there is a dead cross, close the long position. My EA is attached. By back testting one can see the long position is close though there is no dead cross.


Files:
 

And I find out, if I change the code from 

...
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
...

to
...
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
...

then it works.

But in this case, the signal has delay as shown below:


Any way to overcome this?

 
thomas2004: Any way to overcome this?
Overcome what?
  1. If you use the forming bar, you will get premature signals.
  2. If you use the completed bar, you open at the first mature signal.
 
whroeder1:
Overcome what?
  1. If you use the forming bar, you will get premature signals.
  2. If you use the completed bar, you open at the first mature signal.

1. Premature? You mean the so-called forward bias? 

2. I've seen quite a lot of EAs which use the current bar and the second bar, i.e [0] and [1] and seems quite OK. But in my EA when I use these, I got wrong signals.


From the second screenshot one can see the signal delay is to much. Do you think it is normal?

Reason: