Error in EA with a moving average crossover

 

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)
 {
   PlaySound("alert.wav");
   AlertedLong = true;
   AlertedShort = false;

 }
    
   if (EMA2 > EMA1 && EMA4 < EMA3 && AlertedShort == false) 
 {
   PlaySound("alert.wav");
   AlertedShort = true;
   AlertedLong = false;

 }
 
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)   //?????
 
is AlertedXXXX static or globally declared?
 
WHRoeder:
is AlertedXXXX static or globally declared?

Globally
 
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?
 
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 !!!!!

 
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)
 {
   PlaySound("alert.wav");
   AlertedLong = true;
   AlertedShort = false;

 }
    
   if (EMA2 > EMA1 && AlertedShort == false) 
 {
   PlaySound("alert.wav");
   AlertedShort = true;
   AlertedLong = false;

 }
Comments?