How to access array element at current bar?

Hi, I’m new to AFL. I understand that most functions return arrays.
But how can I access the array element at the current bar?

BarIndex() itself returns an array. So mom[BarIndex()] won’t work (see attachment).
I know I could use cond = IIf(mom > minmom, True, False) instead.
But I would like to know anyway.

Thanx for your help!

Gerhard

mom = ROC(Close, 20);
minmom = 3;

x = mom > minmom;

See Understanding how AFL works.

AFL Function Reference - SELECTEDVALUE

This is in the context of charts, where you use the Selection Line. (Read the comments section)

@GA2024,
In AFL, to use array in the IF statement, you can only access one element at a time and usually in the loop.

mom = ROC(C,20);
minmom = 3;
for (i = BarCount - 50; i <= BarCount - 1; i++) {
	if (mom[i] > minmom) {
		Filter = 1;
	} else {
		Filter = 0;
	}
}

@Peter2047 @TrendSurfer @nsm51 Thanx for your help!

1 Like

@TrendSurfer In this case x itself seems to be an array.

So if(x) doesn't work either.

if you know the bar you want
just use array index ( x[ BAR_ I_WANT ),
although seems that you still do not understand how AFL works

@Peter2047 I understand that.
But isn't AB iterating through the code and arrays bar by bar anyway when processing a script?
So how do I identify the current bar then, i.e. the correct [i] at this point of date?
I don't need the values before and after, just the one at the current bar.

I'm used to a TradeStation/EasyLanguage kind of logic and I haven't yet got AB/AFLs logic.

What about reading knowledge base articles, manual and forum posts first.

https://www.amibroker.com/guide/a_mistakes.html

It's Basics.

For arrays you have to use Iif function not if statement!

To access last bar use LastValue function.

mom = ROC(Close, 20);
minmom = 3;

cond = mom > minmom;

if (LastValue(cond))
   printf("yes");

You do not need loop code for that!

mom = ROC(C,20);
minmom = 3;
bi = Barindex();
Filter = Iif( mom > minmom, 1, 0); // or just Filter = mom > minmom;

Plot (Iif(bi > Barcount - 50, Filter, Null), "Partial plot", colorRed, styleLine);

@GA2024 No, in fact AB is NOT running your AFL once for every bar in the array. For an analysis (scan, explore, backtest), AmiBroker will run your AFL once per symbol, with built-in variables like Open, High, Low, Close holding the entire price array for that symbol. Similarly to update a chart, AmiBroker will run the AFL one time, not once per bar.

Because of this, there is no concept of a "current" bar. You could consider the bar that's selected on the chart as the "current" bar, or you could consider the last bar of data available for the symbol in your database as the "current" (i.e. most recent) bar. It all depends on context.

For many operations, you will want to operate on the entire array at once. For example, if I want to calculate the range of all bars I might do something like this:

range = H - L;

The result of this statement is that the range variable contains an array, and each element of the array is the difference between the high and low from the corresponding elements in those arrays.

1 Like