Export Custom Forumula data into separate csv files

Hello Forum members, hope all are doing well.

I have been using the following formula (AmiBroker Knowledge Base » How to export quotes to separate text files per symbol) to save each symbol's data into separate CSV files, this works like a charm.

My question is, Is it at all possible to also add a custom formula like (C/Ref(C, -1))-1 to the above AFL and export that too in the CSV file? If so, can someone please guide me on how to or to an example of a similar nature?

thanks in advance for your help.

cheers!

fputs( "Ticker,Date/Time,Open,High,Low,Close,Volume\n", fh );
// becomes
fputs( "Ticker,Date/Time,Open,High,Low,Close,Volume,chg\n", fh );

and

qs = StrFormat( "%g,%g,%g,%g,%g\n", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
// variable to understand
pc = (C[i]/C[i-1])-1;
qs = StrFormat( "%g,%g,%g,%g,%g,%g\n", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ], pc );

and for ( i = 0;... should start from 1... or you write some if statement to output that "first" bar with pc = 0 etc
because when i=0, i-1 will be error.

2 Likes

thank you v much @nsm51. was stuck at the for i = 0 bit. cheers.

just to add to this query, your solution works but now when i export the data, the first bar for OHLC is missing in the csv. The code i am using is as follows

fh = fopen( "C:\\Data\\" + 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,aux1_Percentile, aux2_Circuit, chg\n", fh );

    // iterate through all the bars

    for ( i = BarCount - 1; i >= 1; i-- ) // here is where i started i from 1
    {
        // write ticker name
        fputs( Name() + "," , fh );

        // write date/time information
        fputs( DateTimeToStr( dt[ i ],1  ) + ",", fh );
        
	pc = (C[i]/C[i-1])-1; 
	
        //write quotations and go to the next line
              qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f, %.4f, %.4f, %.%%f\n", 
                     O[ i ], H[ i ], L[ i ], C[ i ], V[ i ], Aux1[ i ], Aux2[ i ], pc*100 ); 
        fputs( qs, fh );

    }
    // close file handle
    fclose( fh );
}
 
// line required by SCAN option
Buy = 0;
for() {
...
}
qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f, %.4f, %.4f, 0\n", 
                     O[ 0 ], H[ 0 ], L[ 0 ], C[ 0 ], V[ 0 ], Aux1[ 0 ], Aux2[ 0 ]); 
        fputs( qs, fh );
    // close file handle
    fclose( fh );

just add that single last line with index=0 after the for loop

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.