checking Ma 20 and 50 intersection

 

hey, i am trying to check for intersection between ma 20 and ma 50,

the function i wrote seem to work, but its seems to be non consistentic.

sometimes (depends of current value) the intersection accures when the substraction of the two MA's is ~0.2 and sometimes its ~1.4.

how can i determine for sure when the intersection accured?


bool checkMAInterSection(int firstMA, int secondMA) // 20,50
{

double currPosFirstMA = iMA(NULL, 0, firstMA, 0, MODE_EMA, PRICE_MEDIAN, 0);

double currPosSecondMA = iMA(NULL, 0, secondMA, 0, MODE_EMA, PRICE_MEDIAN, 0);

double subVal = MathAbs(currPosFirstMA - currPosSecondMA);
bool returnVal;
if ( (subVal*100 >= 0) && (subVal*100 <= INTERSECTION_MIN_VALUE ) // dont have to multiply 100
{
returnVal = true;
}
else
{
returnVal = false;
}
if (returnVal==false)
{
Alert("MA\'s didnt intersected");
}
else
{
Alert("MA\'s Intersected!!");
}

return (returnVal);
}
 

Please use this to post code . . . it makes it easier to read.

 
VladiRozen:

hey, i am trying to check for intersection between ma 20 and ma 50,

the function i wrote seem to work, but its seems to be non consistentic.

sometimes (depends of current value) the intersection accures when the substraction of the two MA's is ~0.2 and sometimes its ~1.4.

how can i determine for sure when the intersection accured?


You can't . . . the intersection isn't always going to be aligned with a bar . . . but you can only get the values on the bars unless you want to interpolate . . . you are getting the values for bar 0 . . . don't the MA values for bar 0 repaint ?
 

bar 0 is the current bar....

i'm calculating the current values of the MA's substracting between them,

i want to get a consistentic substraction value, for example, if the substraction is below 0.2 the ma's have intersected or just going to....

 
bool checkMAInterSection(int firstMA, int secondMA)
{
   double currPosFirstMA = iMA(NULL , 0 , firstMA , 0 , MODE_EMA , PRICE_MEDIAN , 0);
   double currPosSecondMA = iMA(NULL , 0 , secondMA , 0 , MODE_EMA , PRICE_MEDIAN , 0);
   double subVal = MathAbs(currPosFirstMA - currPosSecondMA);
   bool returnVal;
   
   
   if ( (subVal*100 >= 0) && (subVal*100 <= INTERSECTION_MIN_VALUE )/* && (nextInterSectionIndicator == true)*/)
   {
      returnVal = true;
   }
   else
   {
      returnVal = false;
   }
      
      if (returnVal==false)
      {
             Alert("MA\'s didnt intersected");
      }
      else
      {
            Alert("MA\'s Intersected!!");
      }

return (returnVal);
}
 
VladiRozen:

bar 0 is the current bar....

Yes, and it's current value, Close[0] = Bid, is changing every tick . . . so your MAs are changing every tick.

Just because the difference between the MAs is < x doesn't mean that they have crossed or about to cross, they may converge and then diverge. They may cross and uncross many times during bar 0

 

ok, if i will change it to iMA(NULL, 0, firstMA, 0, MODE_EMA, PRICE_MEDIAN, 1);

its going to be consistentic?

 
bool checkMAInterSection(int firstMA, int secondMA)
{
   double currFirstMA  = iMA(NULL , 0 , firstMA ,  0 , MODE_EMA , PRICE_MEDIAN , 1),
          prevFirstMA  = iMA(NULL , 0 , firstMA ,  0 , MODE_EMA , PRICE_MEDIAN , 2),
          currSecondMA = iMA(NULL , 0 , secondMA , 0 , MODE_EMA , PRICE_MEDIAN , 1),
          prevSecondMA = iMA(NULL , 0 , secondMA , 0 , MODE_EMA , PRICE_MEDIAN , 2);
   bool   currFirstAboveSecond = currFirstMA > currSecondMA,
          prevFirstAboveSecond = prevFirstMA > prevSecondMA,
          crossed              = currFirstAboveSecond != prevFirstAboveSecond;
   return(crossed);
}
 
thanks WHRoeder and everybody... nice func
Reason: