Limit to 200 barCount in loop

Hello, I know little about programming and looking for someone to help with the following code. I search in this forum and basically copy and paste the code. I am trying to export daily 1 min data from my symbol list. I can't seem to pass the 200 limit bar count. Anything outside I get the error "Array subscript out of range..." If you notice on my loop I have BarCounter -200 which is the max (anything over that I get the error) This translates to a start a 1 min bar at 12:40 pm and I need to start collecting from 9:30 am for the day specified. Any suggestions? And if you are feeling generous I would love if you can show me how to set the parameter in AFL using any start date and finish date. Below is my code. Any help is appreciated....

// create folder for exporting purposes
fmkdir( "C:\\DataExport\\" );

// open file for writing
// file name depends on currently processed ticker
fh = fopen( "c:\\DataExport\\" + Name() + ".txt", "w" );

// proceed if file handle is correct
if ( fh )
    dt = DateTime();

    // write header line
    fputs( "Ticker,Date/Time,Open,High,Low,Close,Volume\n", fh );

    // iterate through all the bars
	//TimeFrameGetPrice( "C", in1Minute);
    for ( i = BarCount -200; i < BarCount; i++ )
        // write ticker name
        fputs( Name() + "," , fh );

        // write date/time information
        fputs( DateTimeToStr( dt[ i ] ) + ",", fh );

        //write quotations and go to the next line
        qs = StrFormat( "%g,%g,%g,%g,%g\n", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
        fputs( qs, fh );

    // close file handle
    fclose( fh );
// line required by SCAN option
Buy = 0;

Moderator comment: Your formula did not use proper formatting and I had to fix it. You really NEED TO read "How to use this site" and use CODE BUTTON. It is really too time consuming for me to fix every message posted by people who fail to follow easy instructions.


There is a far simpler way to create an export. Just write an Exploration, run it, then File / Export HTML/CSV to export it. Or use Batch to automate its export. You can set the start and end dates and periodicity in the Analyser settings.

The coding becomes far simpler. Eg

Filter = 1;
AddColumn(Open, "Open", 1.5);
AddColumn(High, "High", 1.5);
AddColumn(Low, "Low", 1.5);
AddColumn(Close, "Close", 1.5);
AddColumn(Volume, "Volume", 1.0);

This example uses 5 minute periodicity, so it's one row per 5 minute bar. 1 minute periodcity will give you one row per 1 minute bar.

Also, please read and follow the rules in How to use this site when posting code snippets.

1 Like

Thanks HelixTrader for taking the time to respond to my post. I was aware of the code you posted because such code is in the knowledge base; unfortunately the code is not intuitive to me. Probably has to do with my limited experience to Amibroker and believe me I have spent hours trying to solve this to no avail. For example as you posted, I can’t seem to change the periodicity to 1-min interval ( the lowest setting is daily). I click on the settings icon and in the window under periodicity it does not allow me to change to intraday. I then look at the user guide and it only states " To backtest intraday data you should switch to proper interval there and then run the backtest"; again, (I am assuming the ref guide is referring to time frame settings in my charts and they are in 1 min data. And that does not seem to work either. So I don’t know what to do other than continuing posting here. Hoping for someone in the forum is kind enough to show me the steps to get to my objective or write the code so a monkey can run it (sorry to the ape species reference). Thanks all for your help…

Yeah, right…“your” code.
Someone has looked into the future together with Marty McFly & Doc Brown and has posted “your” code here already.

Not intuitive? Perhaps it’s because of the “fact” that it’s “your” code. :laughing:


You can’t even use code tags properly not to mention you ignore forum editor preview and code reference because you just don’t care. In the same way as above you ignore existing information.

Database Settings


BTW, as far as your code edit

 for ( i = BarCount -200; i < BarCount; i++ )

is concerned you should better read here.
Do not make assumptions on number of bars

1 Like

HI @fxshrat,
My understand from your comment above is that AFL will have access to only 200 bars, even if we use indexing ( array[i] ) to address the array element. Is there any AFL options/command to increase the bar available to all available bars?
Thanks for answering,

The issue is about exceeding existing index range going from 0 to Barcount-1. If for example your symbol's array is just 100 elements large but on the other hand you do this

 for ( i = BarCount-200; i < BarCount; i++ )


 for ( i = 200; i < BarCount; i++ )

or this

 for ( i = BarCount-123; i < BarCount; i++ )


 for ( i = BarCount-123; i < 12345; i++ )

or totally worse

 for ( i = BarCount-123; i < BarCount+123; i++ )


without generally taking care of available bars then start index will become negative on one end and in last example in addition you try to become larger than available barcount. It simply exceeds barcount range (you try to access non existing elements) in such cases. That's what it is about. That is why AFL engine will return error message.

As the KB article from upper link says. Don't make assumptions on number of available bars but simply take precautions in your code so that it works without any errors on any number of available bars. Examples are in there already.

Another example precaution

 for ( i = Max(0, BarCount-200); i < BarCount; i++ )