HomeKnowledge Base

How to export quotes to separate text files per symbol

The following KB article: already explained how to use exploration to export quotes into a single text / CSV file.

If, for some reason, we need individual files for each symbol, AmiBroker offers another way of writing data to text files. This can be achieved by using fputs function that would write directly to external files. Using fputs allows us also to fully control formatting of the output data and file naming can be dynamically set based on Name() function output.

To perform the export procedure, we need to run a Scan over the list of symbols we want to export data for.

In the Analysis->Formula Editor please enter the following code:

// 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

for ( 0BarCounti++ )
// write ticker name
fputsName() + "," fh );

// write date/time information
fputsDateTimeToStrdt] ) + ","fh );

//write quotations and go to the next line
qs StrFormat"%g,%g,%g,%g,%g\n"O], H], L], C], V] );
fputsqsfh );

// close file handle
fclosefh );
// line required by SCAN option
Buy 0;

Now please select Tools->Send to Analysis, select the list of symbols (e.g. Apply To: Filter, pick the watchlist in the Filter dialog), set Range to All Quotations, and press Scan

Comments are closed.