Question: Loops and Shifting

 
Hi: I am trying to write a strategy that uses a precondition and a current condition to enter a trade. I use a simple trend-following indicator to enter the trade but do not want it to trigger unless the precondition was true.

I thought that I could run a loop for the precondition using bar shift but am having problems with the code. I use a stochastic for the precondition and I wrote it as follows:
for (q = 1; q < 7; q++) 
      
         {

            double STOCH =    iStochastic(NULL, PERIOD_H1, 11, 5, 8, MODE_SMA,0, MODE_MAIN, q);
           
               
             }  
So if the stochastic level was below 50 between 0 and 7 bars ago, and the entry condition is being fulfilled, the trade should trigger

Here is how I wrote the buy condition:

if ( SLOW.TREND > 0 && STOCH < 50)

but it does not work, as it only reads shift 7 bars and not 0, 1, 2... 7. Is there another way of expressing this without multiple OR statements? Does a While or Do While work for this?

Thanks in advance!


 
int iStoch,iShift
double dStoch[8];

for (iStoch=0,iShift=0;iStoch<8; iStoch++,iShift++) 
      
         {
          dStoch[iStoch] =  iStochastic(NULL, PERIOD_H1, 11, 5, 8, MODE_SMA,0, MODE_MAIN, iShift);
          if(dStoch[iStoch] < 50 && SLOW.TREND > 0)   

 
for (q = 1; q < 7; q++) 
   double STOCH =    iStochastic(NULL, PERIOD_H1, 11, 5, 8, MODE_SMA,0, MODE_MAIN, q);

This reads stoch[1]. then overwrites it with stoch[2]. ... all the way to stoch[6]. Never does seven.

Don't know why you are reading previous values. You aren't doing anything with them.

Either use an array to save all values and then process the array. Or just process each value INSIDE the loop.

Reason: