# Error in EA with a moving average crossover

28

I am trying to make an EA with a moving average crossover. The code fails after the first cross. If you get a opposite cross, you dont get the signal. Do someone has some tips where the error is?

```EMA1=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);
EMA2=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
EMA3=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);
EMA4=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);

if (EMA1 > EMA2 && EMA3 < EMA4 && AlertedLong == false)
{

}

if (EMA2 > EMA1 && EMA4 < EMA3 && AlertedShort == false)
{

}```
6340

herbertioz:

I am trying to make an EA with a moving average crossover. The code fails after the first cross. If you get a opposite cross, you dont get the signal. Do someone has some tips where the error is?

at bar 0 you can have a signal change again to old situation

in that case you don't change back signal to the old situation

```if (EMA1 < EMA2 && EMA3 < EMA4 && AlertedLong == true)  //?????

if (EMA2 < EMA1 && EMA4 < EMA3 && AlertedShort == true)   //?????```
15107

is AlertedXXXX static or globally declared?
28

WHRoeder:
is AlertedXXXX static or globally declared?

Globally
28

deVries:

at bar 0 you can have a signal change again to old situation

in that case you don't change back signal to the old situation

I dont understand. Can you explain in another way?
6340

herbertioz:

I dont understand. Can you explain in another way?

what happens your global variable becomes true

and before the bar 0 ends the old situation of no cross comes back

but your global variable stays true

if one or few bars later the cross is happen you havce not condition

" if (EMA1 > EMA2 && EMA3 < EMA4 && AlertedLong == false) " because AlertedLong is true !!!!!

28

deVries:

what happens your global variable becomes true

and before the bar 0 ends the old situation of no cross comes back

but your global variable stays true

if one or few bars later the cross is happen you havce not condition

" if (EMA1 > EMA2 && EMA3 < EMA4 && AlertedLong == false) " because AlertedLong is true !!!!!

This code below will also work I think, not a cross, but a moving average higher than or lower than.

```bool AlertedLong, AlertedShort = false;

EMA1=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);
EMA2=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);

if (EMA1 > EMA2 && AlertedLong == false)
{