Download MetaTrader 5

Simple moving average dead cross conditions (iMa crossover)

To add comments, please log in or register
Ziobrekka
17
Ziobrekka  

I want to build an EA that sell 1 lot when I have a moving average crossover, no take profit or stop loss

 First I check if there were no crossover in the previous bar 

iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,0)  >  iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,0)

then if there is a crossover 

 iMA(Symbol(),0,2,0,MODE_SMA,PRICE_CLOSE,0)  <  iMA(Symbol(),0,10,0,MODE_SMA,PRICE_CLOSE,0)

 

Are this conditions good?

 

// Check conditions

if ( ( iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,0)  >  iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,0) && ( iMA(Symbol(),0,2,0,MODE_SMA,PRICE_CLOSE,0)  <  iMA(Symbol(),0,10,0,MODE_SMA,PRICE_CLOSE,0) )))

   {  SELL(Symbol(),1,0,0,0,"sell 1") ;}

 

IMA CROSSOVER 

 

 

To work only with completed bars is this the way?

thank you 

int PreviousBarCount = 0;

extern bool      CompletedBars = true;    

-------------------------------------

// Check for completed bars

  if ((PreviousBarCount==Bars) && CompletedBars)

whroeder1
16058
whroeder1  

Your code
if( (iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,0)  >  iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,0)
&&  (iMA(Symbol(),0,2,0,MODE_SMA,PRICE_CLOSE,0)  <  iMA(Symbol(),0,10,0,MODE_SMA,PRICE_CLOSE,0) ))){
   SELL(Symbol(),1,0,0,0,"sell 1") ;
}
if a > b && b < a will never be true.
Write self-documenting code.
double fastCur = iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,0);
double fastPre = iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,1);
double slowCur = iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,0)
double slowPre = iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,1)
bool  isUp = fastCur > slowCur;
bool wasUp = fastPre > slowPre;
bool cross = isUp && !wasUp;
if(cross){
   SELL(Symbol(),1,0,0,0,"sell 1") ;
}
Ziobrekka
17
Ziobrekka  

 if a > b && b < a will never be true.

huge mistake, thank you 

  

I realized that this way I'm opening trades in the wrong bar, I don't want to work with open bars but closed bars only

should I add 1 to the shifts?

 

double fastCur = iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,1);
double fastPre = iMA(Symbol(),0,2,1,MODE_SMA,PRICE_CLOSE,2);
double slowCur = iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,1)
double slowPre = iMA(Symbol(),0,10,1,MODE_SMA,PRICE_CLOSE,2)
bool  isUp = fastCur > slowCur;
bool wasUp = fastPre > slowPre;
bool cross = isUp && !wasUp;
if(cross){
   SELL(Symbol(),1,0,0,0,"sell 1") ;
}
Ziobrekka
17
Ziobrekka  

 

this is what I get adding 1 to the shift

 where I have the yellow arrow  there is the dead cross and in the next bar the EA goes short at the open price (red arrow)

whroeder1
16058
whroeder1  
Exactly what you said you wanted.
Ziobrekka
17
Ziobrekka  

thanks to you, I did it also with a couple of custom averages 

icustom 

To add comments, please log in or register