Using for loop to write AFL

Hi All,

I have a buy condition:

Buy= abs(Ref(H,-1)-Ref(L,-1)) < Ref(ATR(5),-1) AND abs(Ref(H,-2)-Ref(L,-2)) < Ref(ATR(5),-1) AND abs(Ref(H,-3)-Ref(L,-3)) < Ref(ATR(5),-1);

Now I would like to optimize for number of previous H and L that smaller the ref(atr(5),-1), I assume I can using loop to achieve it, something like......

previousHL = Optimize("previousHL ", 1, 1, 20, 1);
for( i = 1; previousHL ; i++ )
{
.......
Buy= abs(Ref(H,-i)-Ref(L,-i)) < Ref(ATR(5),-1)......
.......
}

May I know if my idea is workable? Many thanks!

@wtchung, your loop code (when the "previousHL" variable will be set to 3) will NOT get the same results as your original Buy statement.

In the loop, you need to use AND to combine the previous "partial" results.

To see what I mean, try this exploration snippet (where I split your original Buy statement into its components); apply it to a single instrument.

Filter = 1;

cond1 = abs(Ref(H,-1)-Ref(L,-1)) < Ref(ATR(5),-1); 
cond2 = abs(Ref(H,-2)-Ref(L,-2)) < Ref(ATR(5),-1);
cond3 = abs(Ref(H,-3)-Ref(L,-3)) < Ref(ATR(5),-1);
AddColumn(cond1, "Cond1", 1);
AddColumn(cond2, "Cond2", 1);
AddColumn(cond3, "Cond3", 1);
Buy = cond1 AND cond2 AND cond3;

// loop
buyLoop = True;
for (i = 1; i <= 3; i++)
{
	cond = (abs(Ref(H,-i)-Ref(L,-i)) < Ref(ATR(5),-1)); 
	AddColumn(cond, "Loop - Cond" + i, 1);
	buyLoop = buyLoop AND cond;
}

AddColumn(Buy, "Buy", 1);
AddColumn(BuyLoop, "Buy Loop", 1);

P.S. When you post some AFL code you should ALWAYS use the required code tags:

2 Likes
  1. The optimize function works ONLY in Optimize mode, in other modes like Scan/Explore etc, it will return or use only the default value.

  2. If you are using optimize mode, then where does the need for using a loop arise, the optimizer runs the code once for each value.
    If there are more than 1 optimize variables, then you will have a permutation of each one.

All the results are displayed in the result list.

Why would you run the loop from I=1 for Buy, is it not redundant to a large extent ?

1 Like

All you have to do is this

This one:

/// @link https://forum.amibroker.com/t/using-for-loop-to-write-afl/8404/5
// loop invariant code outside of loop
range = H-L;
refATR = Ref(ATR(5),-1);

Buy = 1;
for (n = 1; n <= 3; n++ )
	Buy &&= abs(Ref(range,-n)) < refATR;

Sell = Cross(MA(C,50), C);

is doing the same thing as this one (of first post)

Buy= abs(Ref(H,-1)-Ref(L,-1)) < Ref(ATR(5),-1) AND abs(Ref(H,-2)-Ref(L,-2)) < Ref(ATR(5),-1) AND abs(Ref(H,-3)-Ref(L,-3)) < Ref(ATR(5),-1);

Sell = Cross(MA(C,50), C);

Now, as for optimization of number of range max. look-back and in order to get backtest report there you need to set Setoption -> GenerateReport to 1 to get full report for each iteration.

/// @link https://forum.amibroker.com/t/using-for-loop-to-write-afl/8404/5
SetOption("GenerateReport", 1 );// force full report creation in optimization
num_conditions = Optimize("Max. H-L look back", 1, 1, 20, 1);

// loop invariant code outside of loop
range = H-L;
refATR = Ref(ATR(5),-1);

Buy = 1;
for (n = 1; n <= num_conditions; n++ )
	Buy &&= abs(Ref(range,-n)) < refATR;

Sell = Cross(MA(C,50), C);
3 Likes