is there a better way to get the two last low stochastic crosses ?

 

hey guys :)

i discovered mql4 a week ago, i know i am late :p

but here is one part of my program :

for (n=0;n<2;i++)
    {
    if ( iStochastic(Symbol(),0,5,3,3,0,1,MODE_MAIN,i) < 20 && iStochastic(Symbol(),0,5,3,3,0,1,MODE_SIGNAL,i) < iStochastic(Symbol(),0,5,3,3,0,1,MODE_MAIN,i) && iStochastic(Symbol(),0,5,3,3,0,1,MODE_SIGNAL,i+1) > iStochastic(Symbol(),0,5,3,3,0,1,MODE_MAIN,i+1))
      { 
      time_Low[n]=iTime(Symbol(),0,i);
      n++;
      }
    }


we are saturday so obviously i didn't test it yet, but this is the easiest way i figured out to get the two last times the indicator crossed under 20.

do you have a better idea to propose ?

 

You are not showing enough of the code.

You are doing n++ within an n for loop which looks wrong.

You can split the tests up onto more than one line so you can actually read them!

 

sorry about the lengh of the test :)


here is all the code:


datetime time_Low[2];
datetime time_High[2];
int i=0;
int n=0;
int shift=0;
int shift1=0;


for (n=0;n<2;i++)// under 20
    {
    if ( 
    iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) < 20 
    && iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) < iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) 
    && iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i+1) > iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i+1)
    )
      { 
      time_Low[n]=iTime(Symbol(),0,i);
      n++;
      }
    }

i=0;
n=0;

for (n=0;n<2;i++)// over 80
    {
    if ( 
    iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) > 80 
    && iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) > iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) 
    && iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i+1) < iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i+1)
    )
      { 
      time_High[n]=iTime(Symbol(),0,i);
      n++;
      }
    }
     
     

shift = iBarShift(Symbol(),0,time_Low[0]);
shift1 = iBarShift(Symbol(),0,time_Low[1]);
Comment(
  "last time : "  + time_Low[0] + " (" + shift  + " candles back)\n",
  "time before :" + time_Low[1] + " (" + shift1 + " candles back)\n"
        );
 

Ahh, I see it now. Very ingenious but non-standard and therefore more difficult to read.

You could do it this way ...

   for( n=0; n<2; i++ ){ // under 20      // pick the first two low stoch crosses which occur below 20
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) > 20 )
         continue;
         
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) > iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) )
         continue;
          
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i+1) < iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i+1) )
         continue;
       
       time_Low[n]= Time[i];
       n++;    
   }

which is more efficient since MQL4 evaluates all terms in a logical expression (unlike C)

However, I think it needs more work because you probably don't want to allow two crosses near each other in the same below 20 area.

Something like this ?

   bool armed = true;

   for( n=0; n<2; i++ ){ // under 20      // pick the first two low stoch crosses which occur below 20
       if( !armed ){
          if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) > 40 )
              armed = true;
       }
       
       if( !armed )  // don't allow wiggles in the 20 zone to be counted
         continue;
           
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) > 20 )
         continue;
         
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i) > iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i) )
         continue;
          
       if( iStochastic(Symbol(),0,5,3,3,0,0,MODE_SIGNAL,i+1) < iStochastic(Symbol(),0,5,3,3,0,0,MODE_MAIN,i+1) )
         continue;
       
       time_Low[n]= Time[i];
       n++;
       armed =false;    
   }

Compiles, but not tested.

 
Thanks i gonna try that as soon as i can :)