Download MetaTrader 5
To add comments, please log in or register
Install apps from the library right in MetaTrader. It is easy!
Harry 2014.06.09 04:25 

Hi Everyone,

I am writing an indicator based on 2 variables.

The rule is that: Whenever variable1 (take value 0 and 1) turns to 1 from 0. find the very first bar (including this turning bar) that has variable1 =1 and varibale2=100. It is like looking back, and I choose to look back 5 bars.

My approach is below:

I think my approach is correct and the code is quite straight forward. But still it doesn't work correctly. It assign found[k] to the first condition met even though it is 20 bar away from the turning bar. So the condiiton 5 bar look back is ignore, somehow.

Any help is very much appreciated.


for(int i=1;i<limit;i++)
  if( variable1[i]=1 &&  variable2[i]=100 )  //identify potential correct point
   {  for(int j=i+5;j>=i;j++)                //look back 5 bar
      {  if(variable1[j]=1 && variable1[i+1]=0
            && variable2!=100)               //find turning bar with variable2 not equal to 100. If found such point, start look back below
         { for(int k=j+1;k>=i;k++)  //look back to find the first bar satisfy 2 conditions
            {  if(variable1[k]=1 &&  variable2[k]=100)
               {  found[k]=Close[k];
                  break; //DONE
      if(brk==1) break;  //DONE and find new point 
Keith Watford
Keith Watford 2014.06.09 06:45  
  for(int j=i+5;j>=i;j++)                //look back 5 bar

I'm not sure what you are trying to achieve, but the above is a bad loop.

You don't look back a maximum of 5 bars, you START your look back at i + 5.

j is initialised at a value higher than i and then is increased at each pass. Therefore the condition j>=i will always be true

Harry 2014.06.09 12:59  

Thank you GumRai,

It should be j--. and it is the error.

Somehow I type ++ and could no way catch it.


whroeder1 2014.06.09 13:10  
scfx: I think my approach is correct and the code is quite straight forward.

Just the opposite. You have two or three nested loops. but break only exits the inner most one.

Find, your first condition, break out of that loop. Start the next loop and find the next...

To add comments, please log in or register