Value returned by "barcount" in an optimization context

Again i have that feeling that I am posting an obvious question. But after a few hours searching for a response...

My AFL code works perfectly in chart and backtest contexts. But in optimize context it fails with an out range error because "barcount" invariably returns 100. I have a total of 8160 bars.

I use 64 bit Amibroker 6.93.0 under Windows 10.

In fact it appears that my arrays are limited to 100 elements in optimize context, hence the barcount value of 100. I do not find where this limit comes from.

Please do read the Knowledge Base

This is not optimisation, but parameter read special execution.

Thank you for fast response Tomasz.
However, after I read the knowledge base you refered me to, I still fail to understand why my initial 8160 bars falls to only 100 when optimizing.
I DID use BarCount in my FOR loop as recommended, not a fixed number of bars.
And again, exactly the same code works perfectly in other contexts.

Please review the following points carefully. Your code should never assume a fixed number of bars, as it can be executed in various contexts.

I want to clarify that the issue we are facing is not related to optimization; rather, it pertains to a special execution that occurs before optimization. During this phase, AmiBroker must execute the formula to identify the OPTIMIZE() statements and their parameter ranges, and this special run utilizes fewer bars to enhance speed.

Therefore, your code must not make any assumptions about the number of bars. Always verify the BarCount and ensure that you never access elements beyond BarCount - 1.

1 Like

Problem solved:
In my case not making any assumptions about the number of bars did not solve the problem (I DID use barcount).
What caused a problem was the fact that the 100 bars were lower than the number of 1mn intraday bars for a day. At some point I had a negative index because the end of the 1st day of the database was never reached.