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?