Write to a textfile specified values only

My question lacks certainly knowledge, but unfortunately I didn't find
any documentation about it.
I have the following code which works as wanted except at the end,
as it writes all the values in the file and not only the values just
before the switch of the signal. This code is joint to a strategy
which generates alternatively long- and shortsignals.

filepath = "C:\\ScanExport.txt";
fdelete( filepath );
fh = fopen( filepath, "a", True );
qs = "";
if ( fh )
{
dt = DateTime();
fputs( Name() + "," , fh );
fputs( "DT,sig,entry,stop,prof,loss\n", fh );
for ( i = 0; i < BarCount; i++ )
    {
fputs( DateTimeToStr( dt[ i ] ) + ",", fh );
qs = StrFormat( "%g,%g,%g,%g,%g\n",  Ref(lastsignal[ i ],-1), Ref(entry[ i ],-1), Ref(realstopprice[ i ],-1) , PrecRound(Ref( bestprofit[ i ],-1), roundl ),PrecRound( Ref(worstloss[ i ],-1), roundl)); 
addtxtcond = LastSignal[ i ] != Ref(LastSignal[ i ],-1);
IIf(addtxtcond,fputs( qs, fh ),null);
}
fclose( fh );

So the addtxtcond doesn't seem to work, as the file is filled with
all the data and not only those before the change of the signal occurs.
Thank you for your attention

You are mixing AFL array code concept in the loop. Its simple like this.

filepath = "C:\\ScanExport.txt";
fdelete( filepath );
fh = fopen( filepath, "a", True );
qs = "";
if ( fh )
{
	dt = DateTime();
	fputs( Name() + "," , fh );
	fputs( "DT,sig,entry,stop,prof,loss\n", fh );
	for ( i = 1; i < BarCount; i++ )
    {
		if( LastSignal[ i ] != LastSignal[ i-1 ] ) {
			qs = StrFormat( "%g,%g,%g,%g,%g\n",  lastsignal[ i-1 ], entry[ i-1 ], realstopprice[ i-1 ], PrecRound( bestprofit[ i-1], roundl ), PrecRound( worstloss[ i-1 ], roundl)); 
			fputs( DateTimeToStr( dt[ i ] ) + "," + qs, fh );
		}
}
fclose( fh );

If you are scanning multiple symbols in multithread, its best to put each symbol in its own file or maybe put the Name() in along with each line. ( When i tested, if many threads are writing to same file, you may get unexpected result so if each single write has all the info then it will not be an issue. )

2 Likes

thank you very much, it's indeed nice and easy :slight_smile:
thank you also for your suggestion to specify the file name,
I already thought about it before :

filepath = "C:\\DataExport\\" + Name() + ".txt";

should be enough to differenciate different files...

If you use separate file for each symbol, you don't need "a" (append) mode. You can just use "w" (write) mode in fopen.

Thank you for your notice.
My idea was, as I apply this to intradaycharts which cover about a month of data,
to run it again after a month to get a continued kind of trackrecord by appending
the results to the previous ones.
But in this case indeed it makes no sens to start by deleting the file each time.
So I modified the code to have the choice.
For those who are interested it looks now basically like this

WriteResultsFile = ParamToggle( "WriteResultsFile", "No|Yes",1);
NewWriteResultsFile = ParamToggle( "NewWriteResultsFile", "No|Yes",0);
if (WriteResultsFile)
{    
filepath = "C:\\DataExport\\" + Name() + ".txt";
if (NewWriteResultsFile)
{
fdelete( filepath );
fh = fopen( filepath, "w", True );
}
else
fh = fopen( filepath, "a", True );
qs = "";
lastdate = "";
if ( fh )
{
	dt = DateTime();
	fputs( Name() + "," , fh );
	fputs( "DT,DT2,sig,entry,stop,prof,loss\n", fh );
	for ( i = 1; i < BarCount; i++ )
    {
		if( LastSignal[ i ] != LastSignal[ i-1 ] ) {
			qs = StrFormat( "%g,%g,%g,%g,%g\n",  lastsignal[ i-1 ], entry[ i-1 ], realstopprice[ i-1 ], PrecRound( bestprofit[ i-1], roundl ), PrecRound( worstloss[ i-1 ], roundl)); 
			fputs( lastdate + "," + DateTimeToStr( dt[ i ]) + "," + qs, fh );
			lastdate = DateTimeToStr( dt[ i ]);
		}
}
fclose( fh );
}

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