Creating Matching Watchlist from IndustryID() consituents

Hi all,

Been trying to work out kinks in the following AFL. The intention was to convert IndustryID constituents (or any other amibroker categorisation) to a matching watchlist and then be-able to update it as the database evolves. I find this easier to handle and manipulate for the purpose of using screening functions like InWatchlist(), since the only other similar function to mimic InWatchlist() is InGICS() and InICB, which becomes a problem for Industry or Sector checks.

Regardless, the current code keeps booting me out of Amibroker and I would like to understand the flaw in my logic. I don't believe the issue is related to the parts that create the watchlist.

Sometimes when I delete the watchlist categories and run the script it works, then on re-run it fails and boots me out. I was initially thinking it was linked to how strExtract formed the lists that would then be entered into the watchlist, but then when I manually created a list in the format "CBA.au, FMG.au, WBC.au" it was still failing on a fresh re-run (after deleting the existing watchlists). This leads me to believe the issue is with the section deleting the tickers from the existing watchlists prior to adding then finding the current industry constituents and adding in the most recent symbols.

The code is an adaptation to @fxshrat as found here:

/*
Script created by John Hanna.
The purpose of this script is to:
1. Creating a matching watchlist for each non-blank IndustryID()
2. Delete, then update the watchlist with current constituents.

*/

// Set the category type:
CategoryType = categoryIndustry;

SymbolsProcessed = "";
// Get the filled industry categories
for (i = 0; i < 256; i++) 
{   
	
    CategorySymbols = CategoryGetSymbols(CategoryType, i);
    
    if (CategorySymbols != "") 
    {
        CategoryID_Number = i;
        CategoryName = CategoryGetName(CategoryType, CategoryID_Number); 

        // Debugging print to identify the category being processed
        printf("\nProcessing Category Name: %s (ID: %g)", CategoryName, CategoryID_Number);

        // Check if the watchlist exists
        WatchlistID_Number = CategoryFind(CategoryName, categoryWatchlist);
        if (WatchlistID_Number == -1) 
        {
            // Watchlist doesn't exist, create it
            CategoryCreate(CategoryName, categoryWatchlist);
            WatchlistID_Number = CategoryFind(CategoryName, categoryWatchlist);
            printf("\nWatchlist '%s' created successfully.", CategoryName);
        } 
        else 
        {
            printf("\nWatchlist '%s' already exists.", CategoryName);
        }
		
        // Erase the watchlist before adding new symbols
        oldlist = CategoryGetSymbols(categoryWatchlist, WatchlistID_Number);
        for (j = 0; (sym = StrExtract(oldlist, j)) != ""; j++) 
        {
            CategoryRemoveSymbol(sym, categoryWatchlist, WatchlistID_Number);
        }
        printf("\nWatchlist '%s' cleared.", CategoryName);
		
        // Add the symbols from the industry category to the watchlist
        for (k = 0; (sym = StrExtract(CategorySymbols, k)) != ""; k++) 
        {
            CategoryAddSymbol(sym, categoryWatchlist, WatchlistID_Number);
            SymbolsProcessed += sym;
        }
        printf("\nSymbols added to Watchlist '%s'.", CategoryName);
        printf("\ncategory Symbols are %s\n\n", CategorySymbols);
    }
}

printf("\n\nFull Symbol list processed: %s", SymbolsProcessed);


printf("\nEnd of Script");