Running a scan that clears 2 watchlists of their cotents, reads an ASCII file, does some calculations, then adds symbols to the 2 watchlists it emptied. The program is running OK and the results are OK, but when it terminates 3 other, independent programs that are contained in the window crash.
Program #1 crashes with exception at given address
Program #2 crashes with error 17 (Missing argument) pointing to line "Midprice = MA( Close, 20 );"
Program #3 crashes with error 17 (Missing argument) pointing to line
"VolSpike = Volume > 1.35*Ref(MA(Volume,2),-1);
The AFL code is below:
//File: Create Performance Watch List
//version 1.0
//November 2016
//
//This program reads specified ASCII file with performance data from prior backtest,
// - Prior backtest data created from "UTIL - Scan - Create Performance ASCII file"
// - ASCII filename is
//
//Performance evaluation:
// 1. Remove all issues with negative returns
// 2. Remove all issue with below average returns
//
//Program Flow
// - Read ASCII file
// - Calculate average P/L
// - Process data and create 2 watchlists:
// - With profit greater than zero // AK-HighPerf-zero
// - With profit greater than average profit // AK-HighPerf-Avg
// - Create statistics.
//
//Use Dummy watchlist as source in order to process data only once
//
//
//#pragma maxthreads 1
//
//Specify Source File
Date_string = StrReplace( NumToStr( DateNum(), 1.0 ), ",", "" );
Filepath = "G:/DataFiles/Daily Backtest Summary/";
Filename = "TradableEquities - " + Date_string + "-Single.txt";
//
SourceName = Filepath + Filename;
//
if( Status( "action" ) == 3 AND Status( "stocknum" ) == 0 )
{
//TargetList = "AK-Performing Issues";
//
//
Target1Name = "AK-Perform-Zero"; // Issues with profit > 0
Target2Name = "AK-Perform-Avg"; // Issues with profit > Average
Target1Num = CategoryFind( Target1Name, CategoryWatchlist );
Target2Num = CategoryFind( Target2Name, CategoryWatchlist );
//
StaticVarSet( "Target1Num", Target1Num );
StaticVarSet( "Target2Num", Target2Num );
//
Target1List = CategoryGetSymbols( CategoryWatchlist, Target1Num, 0 ) ; // Get ticker symbols in CSV format
Target2List = CategoryGetSymbols( CategoryWatchlist, Target2Num, 0 ) ; // Get ticker symbols in CSV format
_TRACE( "WL_Utilities-1 - Target1Name " + Target1Name + " Target1Num = " + Target1Num + " List1 " + Target1List );
_TRACE( "WL_Utilities-2 - Target2Name " + Target2Name + " Target2Num = " + Target2Num + " List2 " + Target2List );
//
//
//Remove Symbols from Target1 Watchlist
for( i = 0; ( sym = StrExtract( Target1List, i ) ) != ""; i++ )
{
_TRACE( "WL_Utilities-3 - i " + Writeval( i, 1.0 ) + " remove sym = " + sym ); // + " List " + Targetlist);
CategoryRemoveSymbol( sym, categoryWatchlist, Target1Num );
}
//
//Remove Symbols from Target2 Watchlist
for( i = 0; ( sym = StrExtract( Target2List, i ) ) != ""; i++ )
{
_TRACE( "WL_Utilities-4 - i " + Writeval( i, 1.0 ) + " remove sym = " + sym ); // + " List " + Targetlist);
CategoryRemoveSymbol( sym, categoryWatchlist, Target2Num );
}
//
} // end if Status
//
// ============================================= LOOK BELOW THIS LINE =====================================
Num_symbols = 0;
//
_TRACE( "WL_Utilities-x0 - SourceName " + SourceName );
//
if( status( "action" ) == actionScan AND Status( "stocknum" ) == 0 )
{
//
files = fdir( filepath + "*.*", 1 ); // ZZZZZZ???
_TRACE( "WL_Utilities-xdir - " + files );
line = "startline";
len = 0;
//
fh = fopen( Sourcename, "r", True ); // Open file from disk
_TRACE( "WL_Utilities-x1 - fhread " + fh );
//
if( fh )
{
do
{
if( Num_symbols == 0 ) // Forces to read first line of file
line = fgets( fh ); // Read first line from from disk file. Subsequent lines are read later in program
num_symbols++; // Count symbols read
_TRACE( "WL_Utilities-7A - start of do loop - Num_sumbols " + WriteVal( num_symbols, 1.0 ) );
len = StrLen( line ) - 1; // one based. Count new line character
_TRACE( "WL_Utilities-7B - line " + line + " len " + numtostr( len, 1.0 ));
char = "X"; // Dummy character filler
count = 0;
while( char != " " AND len > 0) // Find space before value
{
char = StrMid( line, len - 1, 1 ); // zero based - get last character
len--;
count++;
_TRACE( "WL_Utilities-7C - len " + WriteVal( len, 1.0 ) + " count " + WriteVal( count, 1.0 ) + " ASCchar " + Asc( line, len ) + " char " + char );
} // end while char
//_TRACE( "WL_Utilities-8A - count " + WriteVal(count,1.0));
//_TRACE( "WL_Utilities-8B - len " + numtostr(len,1.0));
//
Value = StrMid( line, len + 1, count - 1 ); // Extract value from line
Value_len = StrLen( value );
//
charpos = StrFind( line, " " ); // Find first blank after symbol
symbol = StrMid( line, 0, charpos - 1 ); // get symbol
symlen = StrLen( symbol );
//
_TRACE( "WL_Utilities-8A - Symbol " + Symbol + " Val len " + Value_len + " Value " + value );
//
for( j = 0; j <= Value_len - 1; j++ ) // Verify ASCII codes for Value string
{
_TRACE( "WL_Utilities-8B - Symbol " + Symbol + " Val len " + WriteVal( Value_len, 1.0 ) + " j " + WriteVal( j, 1.0 ) + " char " + StrMid( value, j, 1 ) + " ASC " + Asc( StrMid( value, j, 1 ) ) );
}
//
_TRACE( "WL_Utilities-9A - Vallen " + WriteVal( Value_len, 1.0 ) );
_TRACE( "WL_Utilities-9A - symlen " + WriteVal( symlen, 1.0 ) );
_TRACE( "WL_Utilities-9A - symnum " + WriteVal( num_symbols, 1.0 ) );
_TRACE( "WL_Utilities-9B - symbol " + symbol );
_TRACE( "WL_Utilities-9C - value " + value );
_TRACE( "WL_Utilities-9D - value " + value + " symbol " + symbol + " numsym " + WriteVal( num_symbols, 1.0 ) );
//Assign values to dynamic variables
Num_symbols_str = numtostr( Num_symbols, 1.0 );
Value_Num = StrToNum( Value );
_TRACE( "WL_Utilities-9F - after 2 var assignments " );
VarSet( symbol, Value_Num );
VarSetText( Num_symbols_str, symbol );
//
line = fgets( fh ); // Read file from disk. First read is done at start of loop. This allows check for empty line
_TRACE( "WL_Utilities-9G - after fgets() at end of code - len " + StrLen(line) + " " + Line);
}
while( ! feof( fh ) AND line != "" ); // not oef
//
fclose( fh );
_TRACE( "WL_Utilities-9H - file closed - out of loop " );
//
} // end if (fh)
else
_TRACE( "WL_Utilities-9I - fh " + WriteVal( fh, 1.0 ) + " File " + SourceName + " Not found" );
//
}// end if status
//
//
//Calculate Average value of returns
Temp_Value = 0;
Avg_Value = 0;
//
for( i = 1; i <= num_symbols; i++ )
{
loop_num = NumToStr( i, 1.0 );
Symbol = VarGetText( loop_num );
Value = VarGet( Symbol );
Cum_Value = Temp_Value + Value;
Temp_Value = Cum_Value;
Avg_Value = Cum_Value / i;
//
_TRACE( "WL_Utilities-10 - Numsymbol " + WriteVal( i, 1.0 ) + " symbol " + symbol + " Value " + WriteVal( Value, 1.2 ) + " Cum " + WriteVal( Cum_Value, 1.2 ) + " Avg " + WriteVal( Avg_Value, 1.2 ) );
//
}
//
Target1Num = StaticVarGet( "Target1Num" );
Target2Num = StaticVarGet( "Target2Num" );
//Create new Watchlist with low performing issues removed.
for( i = 1; i <= num_symbols; i++ )
{
Symbol = VarGet( NumToStr( i, 1.0 ) );
Value = LastValue( VarGet( Symbol ) );
//
if( Value > 0 )
{
CategoryAddSymbol( symbol, CategoryWatchlist, Target1Num ) ;
_TRACE( "WL_Utilities-11 - Add symbol " + symbol + " Value " + WriteVal( Value, 1.2 ) + " Avg " + WriteVal( Avg_Value, 1.2 ) );
}
//
if( Value > LastValue( Avg_Value ) )
{
CategoryAddSymbol( symbol, CategoryWatchlist, Target2Num ) ;
_TRACE( "WL_Utilities-12 - Add symbol " + symbol + " Value " + WriteVal( Value, 1.2 ) + " Avg " + WriteVal( Avg_Value, 1.2 ) );
}
//
} // end for loop
//Restorepricearrays();
//