Checking if numbers of bars closing price below ema line code

 

Hi everyone I want to create a code to check if numbers of bars closing price is below ema line. And this is the code that I came up with.

bool PriceReversingDown(int shift=0)
{
   int result1, result2, result3; //result of closing price below ema line respectively to their bar numbers
   for(int i=shift; i<shift+3; i++)
   {
      if(ClosePrice(i+1)<EMA(i+1)) result1=1;
      return result1;
      if(ClosePrice(i+2)<EMA(i+2)) result2=1;
      return result2;
      if(ClosePrice(i+3)<EMA(i+3)) result3=1;
      return result3;
   }
   if(result1==1 && result2==1 && result3==1) return true;
   return false;
}

so in this code I create a variables with name result1, result2 and result3. This variable will store the value from if statement of respective bars. The last thing to do is to check whether the result return the value of 1, if all of it is 1 then it return true, if one of it false then return false.

when I compile this code a warning result showed up with description:

1. expression is not bollean.

2. possible use of uninitialized variable 'result1', 'result2', and 'result3'.

and of course  the result of this code is always true. Can anyone help me fix this code?

 

I am not sure what you are trying to do with your code.

Doesn't this do what you want?

bool PriceReversingDown(int i=0)
{
   if(ClosePrice(i+1)<EMA(i+1)
         &&
         ClosePrice(i+2)<EMA(i+2)
         &&
         ClosePrice(i+3)<EMA(i+3))
      return true;

   return false;
}

and it is much simpler.

 
Keith Watford:

I am not sure what you are trying to do with your code.

Doesn't this do what you want?

and it is much simpler.

I just made it complicated, yes that's what I want to do. thanks for make it simpler

 

This will make it even simpler by using a loop.

bool PriceReversingDown(int i=0,int numberOfBars=3)
{
   for(int x=1; x<=numberOfBars; x++)
      if(ClosePrice(i+x)>=EMA(i+x)) //look for condition NOT being satisfied
         return false;

   return true;
}
Reason: