Strange crashing

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();

//

If you are getting exception from your program #1 you should go to Tools->Preferences, AFL and uncheck “catch exceptions”, and then send proper report from this window
http://www.amibroker.com/guide/x_bugrecovery.html

Tomasz, I never received a response to the exception report per your request above.

In addition after fixing several issues with ASCII file per your recommendations I posted issue again with more checks in my AFL code. I don’t see that posting ???

Are you able to help me with this issues?

Thanks
Ara

I replied already (above). You need to send output of bug recovery window. Without that no one can help.

Tomasz, I did send the crash report previously directly from Amibroker. Here it is again. Should I have copied and sent it through this method?

Please note that multiple other programs also crash but with error 17

The complete code segment shown in crash report is below

    do
    {
        n++;
        myDateNum   = Nz(ValueWhen( lastTradedYear, dn, n ));												// n=1 ==> last complete year. Date as read from arrays  
        R_Year		= LastValue( int( ( myDateNum + 19000000 ) / 10000 ) );								// Year (yyyy)
        _TRACE("Seasonal-Initial-0 - dn " + WriteVal(dn,1.0) + "  n " + WriteVal(n,1.0) + "  Firstyear " + WriteVal(Firstyear,1.0)+ "  Last myDateNum " + WriteVal(myDateNum,1.0) + "  R_Year " +  WriteVal(R_Year,1.0));
        //
        if( R_Year != 1900 ) FirstYear	= R_Year;  															// First year - not complete year

         //printf( "find Year - n " + n + "  year " + R_year +  "  Fyear " + Firstyear + "\n" );
    }
    //while( R_Year != 0 );
     while( R_Year != 1900 );
AmiBroker version 6.22.0.6220
( 64-bit, cooltool.dll 6.22.0,  mfc42.dll 6.22.0,  msvcrt.dll 7.0.7601 )

Microsoft Windows 7 version 6.1 (Build 7601) 
Service Pack 1.0, Common Controls: 6.16

Unhandled exception
Type:	CSysException
Code:	c0000005
Description:	ACCESS VIOLATION
Address:	000000013F906B2E

    { 
        n++; 
        myDateNum   = Nz(ValueWhen( lastTradedYear, dn, n ));            // n=1 ==> last complete year. Date as read from arrays   
        R_Year  = LastValue( int( ( myDateNum + 19000000 ) / 1
-------------------------------------------------------------^
File: 'G:\DataFiles\Amibroker-EOD\Formulas\Custom AFL\Seasonal Trading-096.afl', Ln: 1080, Col: 62
Error 47.
Exception occurred during AFL formula execution at address: 000000013F906B2E, code: C0000005
Call Stack:
000000013F906B2E Broker, (function-name not available)() +0 byte(s)
000000013F909E59 Broker, (function-name not available)() +0 byte(s)
000000013F90AEBA Broker, (function-name not available)() +0 byte(s)
000000013F90AC3B Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909D2F Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F90A0E1 Broker, (function-name not available)() +0 byte(s)
000000013F909D2F Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F907E29 Broker, (function-name not available)() +0 byte(s)
000000013F90A96F Broker, (function-name not available)() +0 byte(s)
000000013F909C4A Broker, (function-name not available)() +0 byte(s)
000000013F8FCE10 Broker, (function-name not available)() +0 byte(s)
000000013F936939 Broker, (function-name not available)() +0 byte(s)
000000013F8FF12A Broker, (function-name not available)() +0 byte(s)
000000013F914511 Broker, (function-name not available)() +0 byte(s)
000007FEE3FA29BC mfc140, (function-name not available)() +0 byte(s)
000007FEE4EBBE1D ucrtbase, crt_at_quick_exit() +125 byte(s)
0000000077A559CD kernel32, BaseThreadInitThunk() +13 byte(s)
0000000077B8A561 ntdll, RtlUserThreadStart() +33 byte(s)


Additional information:

Multi-threaded charts - ENABLED

Number of stock loaded: 31832
Currently selected stock: ADBE
Number of quotes (current stock): 7786

Workspace:
Data source = MSTK, Data local mode = 1, NumBars = 29000

Preferences:
Data source = (local), Data local mode = 1, NumBars = 2000

Command history:
2971 - Opens currently selected window layout
2828 - Opens new Analysis window - a tool to test systems & explore market--New Analysis window
2783 - Preferences settings--Preferences
2783 - Preferences settings--Preferences
2971 - Opens currently selected window layout
2971 - Opens currently selected window layout

Cache manager stats:
Number of list elements: 40
Number of map elements: 40
Hash table size: 5987

Memory status:
  MemoryLoad: 28 %
  TotalPhys:     12573816K  AvailPhys:      9005336K
  TotalPageFile: 25145772K  AvailPageFile: 21404412K
  TotalVirtual:  4294967168K  AvailVirtual:  4294540632K


Log:
Logging started 2017-07-01 07:25:00
    19.00 ms : Enabling low frag heap (1.22 ms)
    20.22 ms : Launching splash screen (4.01 ms)
    24.23 ms : Waiting for splash screen (13.58 ms)
    37.82 ms : Alloc other stuff (37.85 ms)
    75.67 ms : Loading amisci.dll (15.47 ms)
    91.14 ms : Setting up SEH translator (194.43 ms)
   285.56 ms : Initializing OLE (0.02 ms)
   285.59 ms : Initializing RichEdit (5.07 ms)
   290.66 ms : Checking current working directory (0.96 ms)
   291.62 ms : Loading persistent variables (0.52 ms)
   292.14 ms : Loading MRU lists (371.89 ms)
   664.02 ms : Loading commisison table (0.88 ms)
   664.91 ms : Loading preferences (3.75 ms)
   668.66 ms : Initializing display settings (0.22 ms)
   668.88 ms : Loading old groups and markets (0.84 ms)
   669.72 ms : Loading GICS and ICB (5.76 ms)
   675.48 ms : Loading plugins (111.04 ms)
   786.51 ms : Loading AT interfaces (0.05 ms)
   786.57 ms : Loading AFL function table (0.56 ms)
   787.13 ms : Init chart infos (1.34 ms)
   788.47 ms : Loading parameters (11.53 ms)
   800.01 ms : Loading chart infos (8.63 ms)
   808.63 ms : Loading custom tools (0.59 ms)
   809.22 ms : Allocating lists (0.17 ms)
   809.39 ms : Loading layers and alerts (2.75 ms)
   812.14 ms : Loading miscellaneous data (0.02 ms)
   812.16 ms : Adding MDI templates (0.67 ms)
   812.83 ms : Register OLE server (6.90 ms)
   819.73 ms : Creating main frame object (26.75 ms)
   846.48 ms : Loading main frame (482.53 ms)
  1329.01 ms : Parsing command line (0.02 ms)
  1329.03 ms : Checking Broker.Document object registration (0.06 ms)
  1329.09 ms : Dispatch commands via ProcessShellCommand (0.02 ms)
  1329.11 ms : Showing main frame window (58.44 ms)
  1387.55 ms : Setting up accelerators (5.19 ms)
  1392.74 ms : Loading database (LoadMarketData) (5467.39 ms)
  6860.13 ms : Setting active symbol (122.51 ms)
  6982.65 ms : Opening default chart (18.22 ms)
  7000.86 ms : Loading default workspace/layout (241.46 ms)
  7242.32 ms : Closing startup splash screen (async in 1 second) (0.55 ms)
  7242.87 ms : Starting up schedule (0.62 ms)
  7243.49 ms : *InitInstance finished

I meant sending to support as no-one on this forum is able to help with access violation.

Errors like that can only be fixed when a formula that is able to generate reproducible crash in development environment is provided. Only reproducible errors can be fixed. Reproducible on our end.

Tomasz, I understand that you need to be able to reproduce errors in order to find a solution.

In this case, as I have stated previously, the crash happens when I run a different unrelated program (the one that reads an ASCII file and saves data in Dynamic Variables for further processing). So basically you will not be able to reproduce the crash because the program runs normally until this unrelated program is run.

As I have further stated many programs crash when this unrelated program is run, most of the with error 17 and no crash report. One program crashes with access violation (which I sent).

So the question for me is how can you guide me to trouble shoot the problem since you will likely not be able to reproduce the crash, unless I send you multiple programs to reproduce the relevant parts of my system?

Thanks
Ara

In that case, the crash may be due to running out of file handles. You need to make sure you are not opening too many files at the same time. You should make sure to close (fclose() call) any files that you have open and do this as soon as possible because there is limited number of open file handles than any Windows program can open.

The random behavior you are getting may also be sign of some race condition due to simultaneous unprotected access to shared resource (such as file).

The program the causes the crash has only 1 fopen() and 1 fclose() statement both outside a loop… so it seems pretty straight forward … Also fopen() is wriiten as fh = fopen( Sourcename, “r”, True ); … but

A couple of questions:

  • Can I monitor within AFL the number of handles that are open - how?
    if not within AFL, Is there another way to monitor handles?

ara1,

Do you still get access violations if you use #pragma maxthreads 1?

I have a simple scan that causes access violations with more than one thread. Unfortunately I am using a third-party plugin whiich makes it harder to tell what is happening. My AFL code does not use fopen.

-Alan

Yes, for example “R” 3rd party plugin is not multi-threading safe. Setting maxthreads helps only if you have race condition in 3rd party plugin, but it won’t help if plugin does not work with non-UI threads.

Alan

Yes I still get a crash … but thanks for the suggestion

Ara

Tomasz

I did some more work ... here are the results

  1. I got processexplorer. As far as I can make out there is only 1 file handle open. Below are a couple of images I captures from a video

  2. I limited the ASCII file read loop to process only a fixed number of lines. Under 205 read operations it did not cause any problems. Sometimes it ran successfully up to 500 to 600 lines read without problems. My file has 1212 lines, so it always fails if I process entire file.

  3. The failure occurs when this statement is executed: VarSet_test1 = VarSet( Symbol, Value_Num );
    The test for successfull operation always shows success. I also cheched Value_Num for Null values. No problem.

  4. AFL code attached. Problem on line 155.


//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 
//
//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;

//
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 remove symbol - 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 remove symbol - i " + Writeval( i, 1.0 ) + "  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 );											// 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 );
         //   if (Symbol != "CCI")
         //   {
            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_test2,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 Num_symbols < 700); //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



You may have symbol that contains invalid characters or spaces or whatever. Valid variable name
MUST begin with letter (a-z only) and subsequent characters must be letters (a-z only, no other chars, digits (0…9) and underscore _). Attempt to create variable (via VarSet) with ANY other character than mentioned above leads to errors.

If that does not help you may run this in Analysis->Old Automatic Analysis and see if that helps. Or you can try 32-bit version.

Tomasz, I tried all of your suggestions. All symbols started with an alpha character. Some symbols contained a period (such as BF.B) … Got rid of all of those … no difference.

Also Old Analysis and 32 bit system did not resolve the problem.

I finally verified the VarSet() crashing problem …

Code was VarSet(symbol, value);

Some symbols contain a “.” character that was causing the problem. such as VarSet(AB.X, value)

Why customers have this peculiar talent of shooting themselves in the foot?

What told you to use “.” in the variable names???

It is written quite clearly in the manual, right in the very beginning:
http://www.amibroker.com/guide/a_language.html

Identifiers are arbitrary names of any length given to functions and variables.

Identifiers can contain the letters (a-z, A-Z), the underscore character ("_"), and the digits (0-9). The first character must be a letter.

AFL identifiers are NOT case sensitive.

Tomasz,
The names of variables with “.” are not my choice. These are stock symbols that contain "."
This forces me to pre-screen stock lists before I do any processing.

Ara

You don’t need to prescan. You just need to replace or remove forbidden characters.

str = StrReplace( str, ".", "_" );