Clarification on understanding exrem() function

I require some assistance with exrem() function.
I have noticed that quite a few AFL trading systems using the exrem() function, particularly after the generation of the buy sell signals in AFL.
I assumed I understood exrem(), and to confirm it I wrote the following simple AFL code to generate some test signals to use as parameters for exrem(), the purpose of which was to test my understanding of exrem().

// This is just an example to create some data for testing exrem

Array1 = IIf( Random() > 0.5, True, False);
Array2 = IIf( Random() > 0.5, True, False);

Array1ExRem = ExRem(Array1,Array2);
Array2ExRem = ExRem(Array2,Array1);

Filter =True;



However, after seeing the following results ( from explore output) it seems that my understanding was not complete.
Screen Shot 2020-08-09 at 8.26.34 pm

I thought that exrem() removed excessive signals from array1 and array2 effectively acted as toggle/reset for the state read from array1.

The issue is that, my understanding of this does not match the output from the simple test as I am not able to clearly interpret the interaction of array2 with respect to array1.

Could someone please assist in describing in more detail the exrem() function in particular the logic of array2 (2nd parameter) with respect to array1. I require clarification in understanding the result (logic) of the highlighted rows shown in the explore output above.



Your Array1 and Array2 have very same rule. So Random may be larger 0.5 in both cases at same time (e.g Array1 -> 0.55 and Array2 -> 0.61 at same bar). So then both variables would return true at same time (and that may occur at consecutive bars as in your case).

So you could achieve similar results e.g. if you do this

Array1 = Rsi() > 50;
Array2 = Rsi() > 60;

And if RSI being larger 60 consecutively then Array1 and Array2 have true signal at same time consecutively.

So if you do ExRem like this

Array1ExRem = ExRem(Array1,Array2);
Array2ExRem = ExRem(Array2,Array1);

then in those upper cases (where it is true at same time) it would be like applying ExRem to same array (but returning true at same time for entire array).

Array1ExRem = ExRem(Array1,Array1);
Array2ExRem = ExRem(Array2,Array2);

So Exrem return true consecutively then.

The manual says

AFL Function Reference - EXREM

removes excessive signals:
returns 1 on the first occurence of "true" signal in Array1
then returns 0 until Array2 is true even if there are "true" signals in Array1

You would have same state at same time at some cases in array so of course Exrem would return true multiple times consecutively if array1 and array2 are true at same time consecutively. Why should it return zero then?? It would not be correct to return zero.

If two chickens lay eggs at the same time (e.g. both at 07:00 and 07:05) then you do not have zero eggs too. Exrem would return true at 07:00 and at 07:05 because both lay eggs at that time.
But if one chicken lays an egg at 07:00 and lays an egg at 07:05 and the other one lays an egg at 08:00 and 08:05 then Exrem would return true at 07:00 and at 08:00 else it would return false (zero).

This is ExRem as AFL function

function ExRemAFL( BuyTable, SellTable ) {
	// by Tomasz Janeczko (
	up = False;
	result = False;
	for( i = 0; i < BarCount; i++ ) {
		result[ i ] = False;
		if( NOT up && BuyTable[ i ] ) {
			up = True;
			result[ i ] = True; // generate True (1) impulse on buy
		if( up && SellTable[ i ] ) {
			up = False; // reset the flag on first matching sell
	return result;

You can clearly see why ExRem would return true (consecutively) if your Array1 and Array2 variables return true at same bar (consecutively).


Hi @fxshrat

Thanks for the clarification. The AFL code for exrem() you posted provided the understanding for the logic. I guess in an ideal scenario Array1 and Array2 would not be true at the same time: i.e. in typical applications one would not have buy and sell signal (true) on the same bar.

If I have a portfolio of 2 tickers and the buy = RSI(2) < 10 and the sell = RSI(2) > 90 and I use :
Buy = Exrem(BUY,SELL);
Sell = Exrem(SELL,BUY);

Ticker #1 has a BUY and gets stopped out with applystop. It NEVER has an RSI(2) > 90 (SELL) for the life of the back test.
Will it ever be eligible for a buy? Even if ticker #2 has an RSI(2) > 90. I guess I am asking if the BUY and SELLs in Exrem are ticker specific or portfolio wide.
thanks for any insight.

Buy and Sell arrays are "ticker specific", so also ExRem applied to them is.

Thank you Beppe. I did run some individual ticker tests using the RSI(2) > 90 as the ExRem sell to reset the ticker and the tickers that never again had a RSI(2) > 90 during the test were never bought again even though the buy parameters were met. Thanks again for the confirmation.