How to programmatically erase watchlist with delisted symbols

I'm looking for a way to erase all symbols from a watchlist using AFL. Note that the watchlist may contain delisted symbols that do not exist in the database anymore. Here is the code I am using. This is a snippet from a larger program that rebuilds the watchlist by adding symbols during a SCAN operation (not shown).

if (Status("ActionEx") == actionScan)
{
  destWatchListNumber = 0;

  Buy = Status("firstbarinrange");
  Sell = Status("lastbarinrange");

  if (Status("stocknum") == 0)
  {
    tickerList = GetCategorySymbols(CategoryWatchlist, destWatchListNumber);
    for (i=0; (sym = StrExtract(tickerList, i)) != ""; i++)
      ;
    _trace("Before removal, there are " + i + " symbols in WL");

    tickerList = GetCategorySymbols(CategoryWatchlist, destWatchListNumber);
    for (i=0; (sym = StrExtract(tickerList, i)) != ""; i++)
      CategoryRemoveSymbol(sym, categoryWatchlist, destWatchListNumber);
    _trace("Removed " + i + " symbols");

    tickerList = GetCategorySymbols(CategoryWatchlist, destWatchListNumber);
    for (i=0; (sym = StrExtract(tickerList, i)) != ""; i++)
      ;
    _trace("After removal, there are " + i + " symbols in WL");
  }
}

Here is the log output in the trace window:

Before removal, there are 4297 symbols in WL
Removed 4297 symbols
After removal, there are 784 symbols in WL

I inspected some of the 784 symbols after this attempt to erase the watchlist and they do not exist in the database. Does anyone know how to programmatically erase a watchlist containing symbols that are not in the database?

1 Like

When you say, Symbols are not in the DB, Do you mean that the Flat Files don't exist in their respective directory?

A watchlist on the other hand is a simple text file present in the AB\Database\Watchlist Directory of the file system.

Maybe, because the symbol doesnt exist, it wont be able to reference it.

Have you tried using fopen, read the file directly, parse it and write to it.

Its a watchlist file, and although this method isn't officially documented, I don't think there is scope to do any serious damage :smiley:
Its just one symbol per line.

Of course one would expect the commands to be written properly :slight_smile:

Will give the code a test, to see the file changes are not overwritten when AB saves everything during exit.
so far just a thought without code testing.

1 Like

See also this reply to the thread (somehow related).

As for changing watchlists as text files, it is better to do it outside of AmiBroker (using scripts to check database files).

Watchlists files (and their associated index.txt file, that holds the watchlists names) are overwritten when you close the application.

2 Likes

yes, was thinking the same thing but was pre-occupied at that time.

Its best to do it from outside.

Ofcourse, this isn't a daily affair either, cleanup is required, whether OS or otherwise, maybe 1 or 2 days in a year for it dedicated :smiley:

1 Like

@beppe thanks for sharing the link. It looks like a pure AFL solution is not currently possible. I also did not see a way to erase watchlists with OLE automation.

As @travick mentioned, it is not a frequent operation. For now, I will just right-click and erase the watchlist manually.