# Creating a simple flag to build an indicator

#1

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!

#2

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 );
``````

#3

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!