How to Exit a 'For...' loop that contains global .afl instructions?

Hello,

I would like to know if it possible ,and how , to use a 'For ( ) { .. }'loop ,
, with golabl afl instructions .
Hencemore I wish to exit fromthe inside of this 'for...' loop before its 'natural' end ,
i.e when a condition set by the instructions within the loop is fulfilled .

For example , to Exit of a Loop that calculate , for each bar , the Highest HIGH over different period of time , as soon as one of the Highest High computed within the loop , is Higher than 100 $ .

I tried this code :

  For ( period = 0 ; period < 20 ; period++) { // Loop over the last 20 bars  

       HighestHigh = HHV(High,period) ; // Highest HIGH over the prevous 'Period' bars - instruction Ok 
                                        // ==> return an historical array 

       // To Exit the 'for' loop , I have tried those two kind of instructions :
       // --------------------------------------------------------------------
        
         if ( HighestHigh[period]  > 100  )  break ; // Exit loop 'for...' , if  HighestHigh calculated over 'period' bars, is over 100 ($)
                                                     // The logic of this instruction doesn't works properly for all bars of the history .
                                                     //  It apply perhaps only to the first bar of the history ?  
       // Or :  

         period = iif ( HighestHigh > 100 , 99999,period) ; // invalid instruction ; it returns an history array not suitable with the type of parameter of the 'for...' loop.
   
      }

Is there a smart guy that know to handle this kind of logic with global .afl instructions ?

Thanks ,

AFAIU, you look for the period at each bar of array that is the first one ensuring HHV being larger than value n (e.g. 100). Correct?

/// @link https://forum.amibroker.com/t/how-to-exit-a-for-loop-that-contains-global-afl-instructions/6513/1
dollar_value = 100;

per_array = Null;
for ( period = 20; period >= 1; period-- ) { // Loop over the last 20 periods
	HighestHigh = HHV(High, period);
	
	for ( i = 0; i < BarCount; i++ ) {
		if ( HighestHigh[i] > dollar_value  )  {
			per_array[i] = period;
		}
	}		
}

Plot( per_array, "1st period ensuring HHV being larger than $" + dollar_value, colorRed, styleHistogram );

5557

3 Likes

@pyhrus you should read this: http://www.amibroker.com/guide/a_mistakes.html

and this: IIf function versus if statement

then you need to understand that your HighestHigh is an ARRAY and there is no single highesthigh value but different HH for each and every bar and to use if() statement you need to specify WHICH BAR you want to use.

2 Likes

AFAIU, you look for the period at each bar of array that is the first one ensuring HHV being larger than value n (e.g. 100). Correct?


Yes , it's absolutely what I am looking to do :blush:

Apparently when using global afl instructions , algorithm have to be set upside-down :

the historical loop that is usually the main driving top loop when using historical instructions , need to be included in the secondary 'global afl loop' when using global afl instructions .

Does anyone know if it exist a quick guide , with short afl examples , to help coding with global .afl instructions ?

Hello, fxshrat

Would you be interested to code for me a trendline based strategie using global afl instructions ?