BarIndex() not incrementing during 'Bar Replay'?

Just when I think I'm starting to understand AFL, then reality kicks in. What have I goofed up... I can see from my printf statements that BarIndex is not changing during a chart 'Bar Replay'? When running 'Bar Replay' within a chart, I can see all of the array elements changing as the 'Bar Replay' progresses... except BarIndex never changes. When I run the same formula in an 'Exploration' I can see that the BarIndex increments by 1 for each bar. Also... when the formula is loaded into a chart, I can see the BarIndex change when I click on various bars in the chart.

My goal is to get the BarInex for each bar and write it to a file as the 'Bar Replay' progresses. As the 'Bar Replay' progresses, all my other array elements are written to my file as expected, including buy, sell, close, ma1, ma2, etc.; however, BarIndex() never changes.

ma1 = ema( C, MALength1 );
ma2 = ema( C, MALength2 );
Buy = Cross(ma1, ma2) AND timeOK;
Sell = Cross( ma2, ma1) OR TimeNum() > endTime;
Short = Cross( ma2, ma1) OR TimeNum() > endTime;
Cover = Cross(ma1, ma2) AND timeOK;

Buy = ExRem( Buy, Sell ); 
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

myBarIndex = BarIndex();
current_bi = SelectedValue(bi);

printf("myBarIndex = " + myBarIndex + "\n");
printf("current_bi = " + current_bi + "\n");
printf("ma1 = " + ma1 + "\n");
printf("ma2 = " + ma2 + "\n");
printf("close = " + c + "\n");
printf("\n");

As you can see... in the 'Exploration' the BarIndex() increments by one...
image

Has been explained in forum several times with referencing KB article. What you observe is because of QuickAFL. What you see on chart is a subset of entire array. So if QuickAFL is enabled then your printed bar index remains steady if not zooming.

Edited picture from KB article:

Taking upper KB picture's sample subset of 13 elements you will realize quickly that if scrolling chart then printf would show bar index 12 (without selecting a bar) but it would not show e.g incrementing index ..., 23, 24, 25, 26...


To get overall BarIndex you may use quickaflfirstdatabar of Status() function added to BarIndex().

bi = BarIndex();
current_bi = SelectedValue(bi);
qfdb = Status("quickaflfirstdatabar");
printf("qfdb+BarIndex = %g\n", qfdb+bi);
printf("selected bi = %g\n", current_bi );
printf("close = %g\n", C);

Now you will see incrementing BarIndex() if scrolling from left to right.


As aside the article was written 2008 so before AmiBroker 5.30 release.
Before that there was a difference of Barindex output.

You can see the difference between AB 5.20 final and AB 5.30 final.

As for BI[0] not being zero in zoomed in visible chart area (if QuickAFL is turned ON) of AB 5.20 picture refers to AB versions prior to AB 5.29.5 beta state. See Development log of cycle 5.21 to 5.30 http://www.amibroker.com/devlog

1 Like

Thank you fxshrat... your advice solves my issue.

I had already seen your edited picture from the KB article and it helped me with other AFL learning challenges. It's a great resource and I keep a copy in my AFL tutorials folder. Unfortunately, the light didn't come on for me regarding the use of Status("quickaflfirstdatabar") . Now I get it. Duh.

Thanks again...

Mike