November 14, 2014
How to export quotes to separate text files per symbol
The following KB article: http://www.amibroker.com/kb/2006/03/04/how-to-export-quotations-from-amibroker-to-csv-file/ 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 ( i = 0; 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
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
Filed by Tomasz Janeczko at 2:23 pm under Exploration
Comments Off on How to export quotes to separate text files per symbol