Help with a logic problem

Back to topics list To post a new topic, please log in or register
scfx 2014.06.09 06: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 



GumRai 2014.06.09 08: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

scfx 2014.06.09 14:59 #

Thank you GumRai,

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

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



WHRoeder 2014.06.09 15: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...

Back to topics list  

To add comments, please log in or register