I've tried numerous ways and iterations to output only a set / specific number of signals to a text file, which would seem fairly straight forward, but can't seem to get it done. All of the different methods I've tried are too numerous to include so I've included my last attempt to simplify the process. I'm just trying to output the first 20 buy signals to the output file. Thank you.
Buy = Cross( MACD(), Signal()) ;
// Sell = Cross( Signal(), MACD() );
// IIf(Cum(Buy) == 0, counter = 0, null);
// IIf(Buy == true, counter = counter + 1, counter);
if( Nz( StaticVarGet( "InitializationDone" ) ) == 0 )
{
// StaticVarRemove("InitializationDone*");
StaticVarSet( "InitializationDone", 1 );
// code for first execution
}
// IIf(StaticVarGet("InitializationDone") == 1, counter = 0, 0);
if( StaticVarGet( "InitializationDone" ) == 1 )
{
counter = 0;
}
IIf( Buy == true, counter = counter + 1, counter = counter );
if( counter < 20 )
{
if( LastValue( Buy ) )
{
// filepath = "C:\\ScanExport.sig";
filepath = "C:\\Users\\Dave\\Alera\\TestAccount1\\1001\\ScanExportToday.sig";
if( Status( "stocknum" ) == 0 )
{
// delete previous file before anything else
fdelete( filepath );
}
// open file in "share-aware" append mode
fh = fopen( filepath, "a", True );
// proceed if file handle is correct
if( fh )
{
lastbuyDT = LastValue( ValueWhen( Buy, DateTime() ) ) ;
// write to file
// fputs( Name() + ", Last Buy: " + DateTimeToStr( lastBuyDT ) + "\n", fh );
// write to sig file
fputs( "BTO " + Name() + " " + "DAY" + " " + "LMT" + " " + Close + "\n", fh );
// close file handle
fclose( fh );
}
else
{
_TRACE( "Failed to open the file" );
}
}
}
/*
AddColumn( BuyPrice, "Buy" );
AddColumn( StaticVarGet( "InitializationDone" ), "Static Variable Value" );
AddColumn( counter, "Counter Value" );
*/