How to locate specific point in AFL

How can i locate the specific point in amibroker.

I would like to look back to the period where RSI is in oversold zone.
Any function that can locate and extract the number of periods.

I tried to use this but it only plots the location where they are under oversold area.

SelectedRSI = RSI(14) - 30;
Barssince(selectedRSI > 0) < 1;
Abd then plot1527070786829

Would appreciate any help here. I'm quite new to this

hello @kanokung

You need to read ValueWhen()

You can try this code.
Play with the cursor click on different bars, please look the chart title what is says.

// Mouse Click on different bars, please look the chart title what is says
myRSI = RSI(14);

OSrsi = myRSI<30;  // oversold zone
OBrsi = myRSI>70;

OSBars = BarsSince(OSrsi);
OBBars =  BarsSince(OBrsi);
LastSinceOverSold =  LastValue(OSBars);
LastSinceOverBought =  LastValue(OBBars);

Plot( myRSI, "My_RSI ", colorDefault, styleLine );
PlotShapes(IIf(OSrsi, shapeSmallCircle, shapeNone),colorGreen,0, myRSI,  0);
PlotShapes(IIf(OBrsi, shapeSmallCircle, shapeNone),colorOrange,0, myRSI,  0);

Title = " My_RSI = " + myRSI + "\n OverSold = "+ OSBars + ", OverBought = "+ OBBars +
  "\n LastValue BarsSinceOverSold "+ LastSinceOverSold + "\n LastValue BarsSinceOverBought "+ LastSinceOverBought ;


thanks a lot. this is helpful :slight_smile:

The code only call the Last recent value but it could not call the one previous occurrence.

My objective is to compare those points and get the period to get

A = Period of low after breaching the support line
B = Period after recovering from support line
C = Period retrace to support line but higher than A

Is there anyway that i could mark those point and store value of A,B,C,D,a, and b like the picture shown to get their respective period so i could compare with the price pattern?


hello @kanokung
this forum works better if you show us your attempts of coding, and you will find that you will get better assistance if you show your own work, and explain where in the code you are having difficulty.

1 Like

Did you read the reference that was given by @PanoS? That explains how to reference "prior" occurrences.

With AmiBroker AFL, (almost) all things are possible. The key is knowing how to code it. That is not a trivial thing in your example. Start simple and work up.

And as PanoS said, show your work.


i have tried your suggestion about valuewhen() but i still can't figure out how to locate the prior occurrence.
i was trying to get guidance on any command that would do the trick (not the whole coding to spoonfeeding me)

{------ RSI Looping FSW_UP ------}
	If IsValid(RSI) Then
			a = 0;
			a_check = 0;
			While a <= 499 And RSI[a] >= 30 Do
						a_check = 1;
  			 			a = a+1;
				For b = a To 100 Do Begin a_check[-b] = 1; End;
						DrawLine ("rsia" ,RSI[a]);

	If IsValid(RSI) Then
			b = 0;
			b_check = 0;
			While b < a And Highest(RSI,a) > RSI[b] And a_check[b] = 1 Do
						b_check = 1;
  			 			b = b+1;
				For c = b To 100 Do Begin b_check[-c] = 1; End;
						DrawLine ("rsib" ,RSI[b]);

	If IsValid(RSI) Then
			c = 0;
			While c < a And Lowest(RSI,b) < RSI[c] And b_check[c] = 1 Do
						c = c+1;
						DrawLine ("rsic" ,RSI[c]);

This is what i have tried in another program. To be frank, i have zero knowledge of computer science and can only do simple if then function. So when converting to amibroker, i have go through reference but still cannot find any command that can find "x-bar" at the location that i wanted.

i even tried to level RSI each level for value 0-10 / 10-20 and so on. still valuewhen() gives me the latest values not the prior condition.

so the issue i am having is the look back period.

Guidance would really be appreciated and really sorry that make both of you @PanoS @snoopy.pa30 that i come for a whole complete solution here.

i read through and spot this . "CCI(20) Divergence Indicator" that might be answer my finding except there are tons of programming language that i am not familiar at all.

and when i try to apply and trace each line. there are tons of errors

From the manual:

ValueWhen(EXPRESSION, ARRAY, n = 1)

Returns the value of the ARRAY when the EXPRESSION was true on the n -th most recent occurrence. Note: this function allows also 0 and negative values for n - this enables referencing future

So change the "n".


@snoopy.pa30 @PanoS sorry to disturb and seek for advice again

here are the improvement but i would like to ask to find this specific point

{//Identify Variables

	//RSI compenents
	myRSI = Prec(RSI(14), 2 );
	//RSI condition zones
	OSrsi = myRSI < 30;
	OBrsi = myRSI > 70;
	Midrsi = myRSI > 50;
	//Count Candles since it meets condition
	OSBars = BarsSince(OSrsi);
	OBBars = BarsSince(OBrsi);
	MidBars = BarsSince(Midrsi);
	//Collect Value at the conditioned candle
	LastSinceOverSold = LastValue(OSBars);
	LastSinceOverBought = LastValue(OBBars);
	LastSinceMid = LastValue(MidBars);

n=Param("% Reverse ",25,0,100,1);
Var = Zig(myRSI, n);
t= Trough(myRSI, n, 1);
p= Peak(myRSI, n, 1);
x[0] =Var[0];
price[0] = C[0];
bear = 0;
bear_di = 0;
bear_con = 0;
bear_trough = 0;
bull_di = 0;
bull_con = 0;
bull_peak = 0;

{// bearish divergence
	for (i=0; i < BarCount; i++){
		if (var[i] == p[i])
			x[j] = var[i];
			price[j] = C[i];
			if ( x[j] < x[j-1] && price[j-1] < price[j] && x[j-1] >= 70)
			bear_di[i] = 1;
			if ( x[j] < x[j-1] && price[j-1] > price[j] && x[j-1] >= 70)
			bear_con[i] = 1;

{// Bullish divergence
	for (i=0; i < BarCount; i++){
		if (var[i] == t[i])
			x[j] = var[i];
			price[j] = C[i];
			if ( x[j] > x[j-1] && price[j-1] > price[j] && x[j-1] <= 30)
			bull_di[i] = 1;
			if ( x[j] > x[j-1] && price[j-1] < price[j] && x[j-1] <= 30)
			bull_con[i] = 1;
PlotShapes ( IIf(bear_di, shapeDigit1, shapeNone), colorRed);
PlotShapes( IIf(bear_con, shapeDigit2, shapeNone), colorRed);
PlotShapes( IIf(bull_di, shapeDigit1, shapeNone), colorBrightGreen);
PlotShapes( IIf(bull_con, shapeDigit2, shapeNone), colorGreen);
{//Main Function for Exploration

	Filter = Buy OR (Sell);

	__signal = WriteIf(Buy, "Buy", "Sell");
	Plot (myRSI, "My_RSI", colorDefault,styleLine);
	AddColumn(i, "i");
	AddColumn(j, "j");
	AddColumn(x[j], "x[j]");
	Title = "My_RSI" + myRSI;

this is the result from the code


the problem is i don't know how to find the peak point where i am pointing


Usually in every program if you press F1 the User's Guide of this app open, In there are many examples.

If you are in AFL Amibroker formula Editor, and you write ValueWhen then with right mouse click by click “Function Reference” User's Guide opens.

There is a very good example and takes care the zigzag but I don’t remember the link. I hope you will find it

Can you check also ("last leg" problem) of classic zigzag

1 Like

thank you that you are still helping me out despite the long lost from the forum for trying