Creating a simple flag to build an indicator

Hi all,

This is my first message on the forum. I wish I had bought Amibroker a long time ago! Incredible the quality and versatility of this software!

I am a novice at AFL and I am trying to learn how to develop my custom indicators and strategies. I am trying to develop a very simple indicator, but I am having problems with it.

The rules of the indicator are very simple:

If the Close of this bar is equal the highest close of 20 bars ago the value of the indicator is 1.
If the Close of this bar is equal the lowest close of 20 bars ago the value of the indicator is -1.

As you can see I am trying to create a flag. That way the value of the indicator always has to be 1 or -1 and should keep and show the value of the indicator the last time that hit the upper band or the lower band.
I have tried with the next code, but unfortunately it does not work:

Valor = IIf(Close == HHV (Close,20), 1 , IIf(Close == LLV (Close,20), -1 , 0)); // Here the problem is that it shows the 0 value when it should show 1 or -1.
Plot( Valor, "Flag Indicator", colorWhite );

Perhaps is there a simple way of solving this problem? Any help would be very appreciate. Thank you!

Based on your upper quote... Is this one what you are looking for?

cond1 = C == HHV (C,20);
cond2 = C == LLV (C,20);

Plot( cond1, "cond1", colorGreen, styleStaircase );
Plot( -cond2, "cond2", colorRed, styleStaircase );

cond1 = Flip( cond1, cond2 );
cond2 = Flip( cond2, cond1 );

cond = cond1-cond2;

Plot( cond, "Flag Indicator", colorWhite, styleStaircase );



Thank you so much fxshrat, that’s just what I needed!

I can see that for this kind of cases I must use the “Flip” function.

Thank you again!

One word: an array. See

1 Like

Think of it the other way around. Each array (Open, High, Low, Close, Volume, Buy, Sell, BuyPrice, etc., etc.) has one element (value) per bar.

No. This code:

cond1 = Flip( cond1, cond2 );

simply calls Flip function with initial values of cond1, cond2 (arrays - so ALL bars are processed at once) and assigns result of Flip function BACK to cond1 variable OVERWRITING all previous values.

Use techniques described here: How do I debug my formula? to understand inner workings of any formula.


No, cond1 and cond2 get overwritten over the full size of the arrays (each bar gets overwritten with new element(s). So there are not arrays but there is/are element(s) of array(s) on each bar).

As you can see in the code after first assigments to cond1 and cond2 there are two immediate plot function calls to output those first two array variables (cond1 and cond2). AFL code execution is from top to bottom of what you see in editor. That's why first you see separate plot of cond1 and cond2 and then at the end there is a 3rd plot but this time plotting is based on new assignments of cond1 and cond2 after first two plots.


If you would place the first two plot lines after the two new assignments of cond1 and cond2 then those two plots would be differently.