Iterate for loop in both directions from a certain point

 

Hello forum, good day.

Is it possible to iterate a for loop in both directions from a certain point in it until certain criteria is met?

For example, lets say that I have a condition inside a for loop where a Moving Average meets criteria at bar 16. At this moment, I assign the value of the Moving Average to a variable (i_val[i] = MA_Value[i]). After this, I need to go back and forth from this point inside the loop subtracting i_val[i] - the Moving Average value of every bar until criteria is met.


Here is what I have so far:

for ( i = 0; i < limit; i++ )
  {
    MA_Value[i] = iMA( NULL, 0, MA_1_Period, MA_1_Shift, MA_1_Method, MA_1_Price, i );
  }

for ( i = 2; i < limit - 1; i++ )
  {
    if ( MA_Condition )
      {
        i_val[i] = MA_Value[i];
        i_ma     = i;
        count++;
      }

    if ( count > loop_limit - 1 ) break;
  }

 

Your help will be much appreciated. 


Best regards and thank you in advance,

Gerardo 

Reason: