Write exploration to file with header

I write to file the exploration results (actually the signals) using this (new exploration deletes the old file and create new):

path = "C:\\signal.csv";
	if( Status("stocknum") == 0 )
		{
	   fdelete( path );
		}
	fh = fopen(path, "a");
	if(fh AND aBuy!=0 OR aShort!=0)
		{
		fputs( Name()+","+WriteIf(aBuy,",Buy@",",Short@")+StrFormat("%g",IIf(aBuy,priceBuy,priceShort))+"\n", fh );
		}
	else{
		printf("Error opening file");
		}
	fclose(fh);

Everything is ok, I've got csv file. However, I would like to add the header (first row only) and if I do it like this below, the header is added not only in the first line, but in every line that corresponds to explored stocks (so if there is no signal, I've got my header in the file's line; e.g. during exploring 1000 stocks and having 20 signals, I've got a file with 1000 lines and if there is no signal, apears my header):

path = "C:\\signal.csv";
	if( Status("stocknum") == 0 )
		{
	   fdelete( path );// delete previous file before anything else
		}
	fh = fopen(path, "a");
fputs("Ticker,"+"Signal,"+"Entry"+"\n",fh); //Problematic line - where to put it to avoid repetition??
	if(fh AND aBuy!=0 OR aShort!=0)
		{
		fputs( Name()+","+WriteIf(aBuy,",Buy@",",Short@")+StrFormat("%g",IIf(aBuy,priceBuy,priceShort))+"\n", fh );
		}
	else{
		printf("Error opening file");
		}
	fclose(fh);

How to add the header (or any other line of text) without repetition?

There isn't AFL code required to export result list.
All that is needed is either AmiBroker Batch.

13

Or by user intervention going to File - Export HTML/CSV...

And yes, both ways will export header too.

2 Likes

Yes, I know I can do it by Batch and thank you, fxshrat, for your example.
However I still would like to do it in AFL, because I would like to add some other content. Any suggestions, please?

Oh, and one more thing - I would like to run exploration every 5 minutes. I'm able to set exploration to run every 5 minutes, but in batch I can set to run it hourly, so the file is saved every hour only, not after finishing every exploration.

I don't see the problem you have.
Simply add additional AddColumn/AddTextColumn lines.
So what's the problem about that?

AddTextColumn -> Name
AddColum/AddTextColumn -> Buy Or Short
AddColumn -> BuyPrice Or ShortPrice
... additional columns

Filter = (Buy OR Short /*OR...*/) AND Status("LastBarInRange");

@piotrd you have already been given excellent answers by @fxshrat. However, if you really need to write another text file, then just put the code to write the header inside the stocknum 0 case, like this:

if( Status("stocknum") == 0 )
{
     // don't need this when access is set to "w", because existing file will be overwritten
    //  fdelete( path ); 
    fh = fopen(path, "w");
    fputs("Ticker,"+"Signal,"+"Entry"+"\n",fh);
    fclose(fh);
}

// Open the file to write signals 
 fh = fopen(path, "a", true);

Also, note that you should set the third parameter of fopen() to True to allow shared access to the file from multiple threads.

:roll_eyes:

I was explicitly trying to discourage doing that.

Why doing that ... if

@fxshrat is correct. One should use AddColumn(s) to design desired output. Only this way you are leveraging multithreading to maximum extent. File functions are slower, require much longer code and resulting formula is much less readable and less reliable.

1 Like