Help fixing code for detecting tops and bottom in stochastic

 

Hi, I want to create a code to detect a top or bottom in the stochastic. And here's the code I try to make.

int counter = 0;
double bar1,bar2,bar3;
double StochasticPatternHT(int shift=0)
  {
   int result = 0;
   for(int i =0; i>Bars; i++)
     {
      bar1 = StochasticHT();
      counter++;

      if(counter == 
2 )
         bar2 = bar1;
      if(counter == 
3 )        
        {
         bar3 = bar2;
         if(bar1>bar2<bar3)
            result=1; //uptrend
         break;
         if(bar1<bar2>bar3)
            result=2; //downtrend
         break;
        }
     }
   return result;
  }

First the code will do the loop for every bar. When code detect the first bar, then the bar1 variable will be set to stochastic value for that respective bar and counter will increase. Then when the counter is 2, it will set the bar2 variable value to bar1. When the counter is 3, it will set the bar3 value to bar2 and additionally will do the pattern and will output the result back to the system.

Apparently that code doesn't work properly, anyone can help me where's the problem are?

 

I have no idea what you are trying to do so can't help with that.

Do you see your error?

for(int i =0; i>Bars; i++)
 
         if(bar1>bar2<bar3)
            result=1; //uptrend

This code won't work as expected. Probably you meant

         if(bar1>bar2 && bar2<bar3)
            result=1; //uptrend
 
Keith Watford:

I have no idea what you are trying to do so can't help with that.

I'm trying to detect tops and bottoms of a stochastic. To make it works I need at least 3 points which is 1 bar before the top 1 bar at the top and 1 bar after the top, this 3 point will create a curve. It will run through all the bars from the current bar until it detect those pattern. In the meantime while the the code running through bars it will save 3 value of stochastic, in mathematic it would be like this i,i-1,i-2

Do you see your error?

Yes I should use < rather than >.

if(bar1>bar2 && bar2<bar3)
   result=1; //uptrend

it doesn't work using this code. My code works but only return result 1.

Update:

int counter = 0;
double stochasticValue;
double bar1,bar2,bar3;
double StochasticPatternHT(int shift=0)
  {
   int result = 0;
   for(int i=0; i<Bars; i++)
     {
      stochasticValue = StochasticHT(i);
      counter++;
      if(counter == 1)
         bar1 = stochasticValue;
      if(counter == 2)
         bar2 = stochasticValue;
      if(counter == 3)
        {
         bar3 = stochasticValue;
         if(bar3>bar2<bar1)
            result=1; //uptrend
         if(bar3<bar2>bar1)
            result=2; //downtrend
         break;
        }

     }
   return result;
  }


changing the code to set the bar1, bar2, bar3 to stochastic value but still not working

 
Luandre Ezra:

I'm trying to detect tops and bottoms of a stochastic. To make it works I need at least 3 points........

Think carefully about what you want the function to do and code accordingly.

At the moment your code is just nonsense and you have put no thought into it at all.

 
         if(bar1>bar2<bar3)
True = non-zero and false = zero so you get:
if( 3 < 2 < 1 )
if( false < 1 )
if(     0 < 1 )
if(     true  )
if( 3 > 2 > 1 )
iftrue > 1 )
if(     1 > 1 )
if(     false )
 
Keith Watford:

Think carefully about what you want the function to do and code accordingly.

At the moment your code is just nonsense and you have put no thought into it at all.

My code for detecting stochastic pattern is based on another code for detecting the zigzag pattern. In the zigzag pattern it works fine. The difference in here is that I need 3 point of value to generate a pattern rather than 2 in zigzag.

William Roeder:
True = non-zero and false = zero so you get:

I never knew that comparing 3 value will be interpret like that. Thank you this is an eye opener for me.

going trying to fix the code then.

Reason: