Sort Quotes before exporting

Hi members, quick query. i am using the below formula to export quotation data to csv format.

// create folder for exporting purposes
fmkdir( "D:\\StocksUpData_CSV\\" );

// open file for writing
// file name depends on currently processed ticker
fh = fopen( "D:\\StocksUpData_CSV\\" + Name() + ".csv", "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;

I would like to know if there is any way to sort the quotations chronologically (latest first) before exporting into the csv format?

thanks,
Gautam

You are writing file directly, so it comes in the order you create it. To reverse the order you have to reverse the loop, instead of this:

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

use this:

 for ( i = BarCount - 1; i >= 0; i-- )

Alternatively, you can use exploration and SetSortColumns and then export using File->Export menu.

1 Like

thanks Tomasz, really amazing software you have built here if i havent said before. cheers

It is indeed.

Just a note that you should check, make sure QuickAFL is disabled either from that Analysis settings window or using SetBarsRequired() or make sure enough bars are there else you may not get the full range for some symbols or undesired results etc

1 Like