Creating Watchlist from ASCII Disk File

This is related to previous entry “Scan and write text files”. With help from Tomasz I fixed the problems of writing the text file, which were related to multithreading Issues.

This is the next item in the sequence of things needed.

My program succesfully reads the ASCII files and parses the text into symbol and value fields and saves them in dynamic variables (varSet() ). The value field is later used to generate the appropriate Watchlist based on given value.

The problem arises when the line VarSet( Symbol, Value_Num ); is executed.

Using Debug View, I have checked the return value of VarSet() and it returns successful. I have also checked for Null values of Value_Num variable. No Null values found.

The program runs to completion and the new watchlists are generated correctly, but multiple AFL programs crash, most with error 17 (Missing argument). All of these program run well when this data processing program is not involved.

As a diagnostic, I commented out the VarSet() statement and nothing crashes.

Since I can not find any problems with the arguments for the VarSet() within the above stated tests, I suspect some kind of possible corruption … I have done a full installation of version 6.20 and then upgraded to ver 6.22 again.

Below is the full segment of the code that creates the problem. Attached the entire program but all non-essential parts to demonstrate problem are commented out.

Should I be looking at the VarSet() statement and arguments some other way? Corrumped Amibroker?

//File: Create Performance WatchList
//version 2.0
//June 28, 2017
//
//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 "TradableEquities - " + Date_string + "-Performance.txt";
//
//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 
//
//


//
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 ) + "  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 ) + "  sym = " + sym ); // + "  List " + Targetlist);
        CategoryRemoveSymbol( sym, categoryWatchlist, Target2Num );
    }

//
} // end if Status
*/
//
//  =============================================   LOOK  BELOW THIS LINE  =====================================
Num_symbols		= 0;
//Specify Source File
Date_string 	= StrReplace( NumToStr( DateNum(), 1.0 ), ",", "" );
Filepath  		= "G:/DataFiles/Daily Backtest Summary/";
Filename  		= "TradableEquities - " + Date_string + "-Performance.txt";
//
SourceName	= Filepath + Filename;
//
_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 );											// one based. Counts new line character

            _TRACE( "WL_Utilities-7B - line " + line + "  len " + Writeval( 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 - 0, count - 1 );					// Extract value from line

            Value_len   = StrLen( value ) - 1;

//
            charpos		= StrFind( line, " " );									// Find first blank after symbol - OK

            symbol		= StrMid( line, 0, charpos - 1 );						// get symbol                    - OK

            symlen		= StrLen( symbol );
            //
			_TRACE( "WL_Utilities-8A - Symbol " + Symbol + "  Val len " +  Value_len + "  Value " +  value );
//
            for( j = 0; j <= Value_len - 0; 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-9B - symlen " + WriteVal( symlen, 1.0 ) );
            _TRACE( "WL_Utilities-9C - symnum " + WriteVal( num_symbols, 1.0 ) );
            _TRACE( "WL_Utilities-9D - symbol " + symbol );
            _TRACE( "WL_Utilities-9E - value  " + value );
            _TRACE( "WL_Utilities-9F - value  " + value  + "  symbol " + symbol + "  numsym " + WriteVal( num_symbols, 1.0 ) );

			if (Symbol == "")												// Test for empty Symbols - they cause problems with VarSet()
			_TRACE( "WL_Utilities-9FX - line  " + line  + "  symbol " + symbol + Asc(Symbol,1));
			//
//Assign values to dynamic variables
            Num_symbols_str = numtostr( Num_symbols, 1.0 );
            Value_Num		= StrToNum( Value );
            
            VarSet_test1 = VarSet( Symbol, Value_Num );
            VarSet_test2 = VarSetText( Num_symbols_str, symbol );
            Value_Test	 = IsNull(Value_Num);
			_TRACE( "WL_Utilities-9G - VarSet test-1 " + WriteVal(VarSet_test1,1.0) + "   test-2 " + WriteVal(VarSet_test1,1.0) + "  Value Test " + WriteVal(Value_Test,1.0));
//
            line	= fgets( fh );												// Read file from disk. First read is done at start of loop. This allows check for empty line
            _TRACE( "WL_Utilities-9H - after fgets() at end of code - len " + StrLen(line) +  "  "  + Line);

        }
        while( ! feof( fh ) AND line != "" );  // not oef

        //
        fclose( fh );

		_TRACE( "WL_Utilities-10A - file closed - out of loop " );
        //
    } // end if (fh)
    else
        _TRACE( "WL_Utilities-10B - fh " + WriteVal( fh, 1.0 ) + "  File " + SourceName + " Not found" );

//
}// end if status
/*
//
//
//Calculate Average value of returns
Last_Value	= 0;
Avg_Value	= 0;

//
for( i = 1; i <= num_symbols; i++ )
{
    loop_num	= NumToStr( i, 1.0 );
    Symbol		= VarGet( loop_num );
    Value		= VarGet( Symbol );
    Cum_Value	= Last_Value + Value;
    Last_Value 	= Cum_Value;
    Avg_Value	= Cum_Value / i;					// Use as threshhold to eliminate poor ferforming issues.
//
    _TRACE( "WL_Utilities-11 - 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 Watchlists 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 )														// Select issues with positive returns
    {
        CategoryAddSymbol( symbol, CategoryWatchlist, Target1Num ) ;
        _TRACE( "WL_Utilities-12 - symbol " + symbol + "  Value " + WriteVal( Value, 1.2 ) + "  Avg " + WriteVal( Avg_Value, 1.2 ) );
    }

//
    if( Value > LastValue( Avg_Value ) )								// Select issues with above average returns
    {
        CategoryAddSymbol( symbol, CategoryWatchlist, Target2Num ) ;
        _TRACE( "WL_Utilities-13 - symbol " + symbol + "  Value " + WriteVal( Value, 1.2 ) + "  Avg " + WriteVal( Avg_Value, 1.2 ) );
    }

//
} // end for loop

*/

HELP