Help debugging simple AFL code


Please help me debug following code.

SetBarsRequired( sbrAll, sbrAll );
printf("BarCount is %f\n", BarCount);
printf("Close[Barcount-1] is %f\n", Close[BarCount-1]);
printf("Close[0] is %f\n", Close[0]);
printf("Close[20] is %f\n", Close[20]);
printf("Close[200] is %f\n", Close[200]);
I am encountering error in 6th line when accessing the array Close beyond a certain index. The error encountered is following:
Error 10. Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array.

I have run this on two symbol databases (NIFTY, NiftyEOD). Both give the same error (but for different index range).
Database NIFTY ---- cannot access indices 21 and beyond
Database NiftyEOD --- cannot access indices 200 and beyond.

I have turned off quickAFL in first line. I printed value of BarCount in 2nd line to be sure that I am not trying to access non-existing index. What is bizzare is that I am able to access these same indices by putting suitable value of x in the notation Close[BarCount-x], but not by directly writing the index value. For example, for database NiftyEOD, BarCount is 373. So I cannot directly access Close[200] but can access it by writing Close[BarCount-173].

Same error is encountered when accessing other array like Open, High etc. Other lines in code give correct output when I remove the 6th line.

Following are the links to database files I am using so that you can reproduce the error.

P.S. This is my first attempt writing code for Amibroker, so pardon me if I have made some obvious mistakes. :)
you can not access array element with barcount-173 bcoz its always need no in terms of index.
array[100] is fine if barcount is grater than 101
array[barcount-5] is not fine bcoz array need indexing in terms of no

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Are you a day trader?