when 2 moving average line collided

 

hi every one

I define 2 moving average in EA:

iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,0)

iMA(Null,0,50,0,MODE_EMA,PRICE_CLOSE,0)

number one with ma_period=1

number two with ma_period=50

I want when these two line collided it shows alert

how I can do it?

Best regards

Neda

 
Neda shahbazi:

hi every one

I define 2 moving average in EA:

number one with ma_period=1

number two with ma_period=50

I want when these two line collided it shows alert

how I can do it?

Best regards

Neda

If you're aware that a MA(1) is likely a price average then : 

if (iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,1) > iMA(Null,0,50,0,MODE_EMA,PRICE_CLOSE,1) && 
iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,0) < iMA(Null,0,50,0,MODE_EMA,PRICE_CLOSE,0)) { printf("*** MA(1) cross MA(50) from above"); }

if(iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,1) < iMA(Null,0,50,0,MODE_EMA,PRICE_CLOSE,1) && 
iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,0) > iMA(Null,0,50,0,MODE_EMA,PRICE_CLOSE,0)) { printf("*** MA(1) cross MA(50) from below"); }

It says : 

  • If at previous bar (1) : MA(1)>MA(50)
  • and at current bar (0) : MA(1)<MA(50 
  • then it has crossed from above (sell)

... and vice versa for the buy.

 

Why use

iMA(Null,0,1,0,MODE_EMA,PRICE_CLOSE,0)

why not just use Close[0] ?

 
Icham Aidibe:

If you're aware that a MA(1) is likely a price average then : 

It says : 

  • If at previous bar (1) : MA(1)>MA(50)
  • and at current bar (0) : MA(1)<MA(50 
  • then it has crossed from above (sell)

... and vice versa for the buy.

thanks for response I wrote your code it didn't work for PRICE_CLOSE for MA(1) but it worked correctly for  PRICE_MEDIAN  for MA(1) like below code:

  if (iMA(Symbol(),0,1,0,MODE_EMA,PRICE_MEDIAN,1) > iMA(Symbol(),0,315,0,MODE_EMA,PRICE_CLOSE,1) && 
iMA(Symbol(),0,1,0,MODE_EMA,PRICE_MEDIAN,0) < iMA(Symbol(),0,315,0,MODE_EMA,PRICE_CLOSE,0))
 { Comment("*** MA(1) cross MA(50) from above"); }

if(iMA(Symbol(),0,1,0,MODE_EMA,PRICE_MEDIAN,1) < iMA(Symbol(),0,315,0,MODE_EMA,PRICE_CLOSE,1) && 
iMA(Symbol(),0,1,0,MODE_EMA,PRICE_MEDIAN,0) > iMA(Symbol(),0,315,0,MODE_EMA,PRICE_CLOSE,0))
 { Comment("*** MA(1) cross MA(50) from below"); }


but I want PRICE_CLOSE for MA(1)...


I have tested for PRICE_OPEN it worked correcctly:

   if (iMA(Symbol(),0,1,0,MODE_EMA,PRICE_OPEN,1) > iMA(Symbol(),0,315,0,MODE_EMA,PRICE_OPEN,1) && 
iMA(Symbol(),0,1,0,MODE_EMA,PRICE_OPEN,0) < iMA(Symbol(),0,315,0,MODE_EMA,PRICE_OPEN,0))
 { Comment("*** MA(1) cross MA(50) from above"); }

if(iMA(Symbol(),0,1,0,MODE_EMA,PRICE_OPEN,1) < iMA(Symbol(),0,315,0,MODE_EMA,PRICE_OPEN,1) && 
iMA(Symbol(),0,1,0,MODE_EMA,PRICE_OPEN,0) > iMA(Symbol(),0,315,0,MODE_EMA,PRICE_OPEN,0))
 { Comment("*** MA(1) cross MA(50) from below"); }

it just didn't worked for PRICE_CLOSE

why this happen ?

what should I do?

thanks in advanced

 
int Crossed(double Line1,double Line2)
  {
   static int last_dir=0;
   static int cur_dir=0;

   if(Line1>Line2)
     {
      cur_dir=1;
     }
   if(Line1<Line2)
     {
      cur_dir=2;
     }
   if(cur_dir!=last_dir  && last_dir!=0)
     {
      last_dir=cur_dir;
      return(last_dir);
     }
   else
     {
       last_dir=cur_dir;
      return(0);
     }
  }

return 0 means no cross

return 1 means cross is happened and line1 is above

return 2 means cross is happened and line2 is above

Reason: