How to export quotes to separate text files per symbol

Hi..I am using the below script to export 1-minute data. I would like the date to be in YYYYMMDD format and time to be 24 hour with no AMPM designation.

// 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 );
    fputs( "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 );
        //  fputs( DateTimeToStr( str[ 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;type or paste code here

The format should look like the below:

20070504 10:26,100.03,100.03,100.03,100.03,4700
20070504 10:27,100.04,100.04,100.04,100.04,300
20070504 10:29,100.00,100.00,100.00,100.00,400
20070504 10:31,100.01,100.01,100.01,100.01,200
20070504 10:34,100.02,100.02,100.02,100.02,100

Any help will be much appreciated.

Thanks,

Mike

@reds, if you are using a recent version of AmiBroker this is simple.

The DateTimeToStr() with a second parameter set to 3 will convert your dates as per the ISO format.

Try this snippet; run it simply pressing the "Verify syntax" button in the code editor and check the results in the Trace tab of the Log window (or using an external tool like DebugView).

function formatMyDate(aDate)
{
	local dateStr;
	// this works also for datetime with no time - EOD data 
	dateStr = DateTimeToStr(aDate, 3);
	if (StrLen(dateStr) == 10) 
		dateStr += " 00:00:00";
	return (StrLeft(StrReplace(dateStr, "-", ""), 14));
}

Version (6.2);

// verify the main steps used in the above custom function
aDate = _DT("2017-11-25 19:18:17");
dateStr = DateTimeToStr(aDate, 3);
_TRACE("Date ISO format: " + dateStr); // expected: 2017-11-25 19:18:17"
dateStr = StrReplace(dateStr, "-", "");
_TRACE("Date ISO format, no dashes: " + dateStr); // expected: 20171125 19:18:17"
dateStr = StrLeft(dateStr, 14);
_TRACE("Date ISO format, no dashes, no seconds: " + dateStr); // expected: 20171125 19:18

// then try some intraday bars
dt = DateTime();
for (i = Max(0, BarCount-10); i < BarCount; i++) 
{
	_TRACE(FormatMyDate(dt[i]));
}

If it works, then apply it to your code:

Version( 6.2 );
function formatMyDate(aDate)
{
	local dateStr;
	// this works also for datetime with no time - EOD records
	dateStr = DateTimeToStr(aDate, 3);
	if (StrLen(dateStr) == 10) 
		dateStr += " 00:00:00";
	return (StrLeft(StrReplace(dateStr, "-", ""), 14));
}

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

/// --->  no code changes  until.......

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

/// <--- the rest of your code

Hope it helps.

Oh boy, all that is needed is one line of DateTimeFormat. Nothing more and nothing less.
https://www.amibroker.com/guide/afl/datetimeformat.html

dtstr = DateTimeFormat( "%Y%m%d %H:%M,", dt[i] );

Full 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 );
    fputs( "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
        dtstr = DateTimeFormat( "%Y%m%d %H:%M,", dt[i] );
        //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( dtstr + qs, fh );
    }
    // close file handle
    fclose( fh );
}
 
// line required by SCAN option
Buy = 0;
7 Likes

Excellent as usual @fxshrat!

I completely missed that function.
Fortunately, you are always ready to correct my silly mistakes. Thanks.

1 Like

Thank you beppe & @fxsrat!

@fxsrat your code worked perfectly!