Clearing out a watchlist using AFL

I’ve created a scan/explore that find stocks that meet different criteria and based on that writes them to one of 5 watchlists. I saw some code in the knowledgebase from a few years that let you clear out a watchlist by getting all symbols and then running a loop to delete them individually but was wondering if there is anything simpler; like a one line command that just clears or resets a watchlist?

Thanks

Hi mike,

Check out the following in the help file.
CategoryRemoveSymbol();

A quote from: https://www.amibroker.com/guide/afl/categoryremovesymbol.html

… removes the symbol from given category. When symbol string is empty ("") then current symbol is used.

// the code removes the symbols with last day volume < 1000 // from the watch list number 1 if( LastValue( V ) < 1000 ) { CategoryRemoveSymbol( "", categoryWatchlist, 1 ); }

… so you can easily (conditionally or unconditionally) delete all or selected symbols from a watchlist.

If you add CategoryRemoveSymbol() to Scan or Exploration code, you won’t need any loop.

Hope this helps.

Thanks! I’ll give that a try.

I’ve been using a similar formula for several years but it doesn’t always remove all of the existing symbols in the watchlist before adding the new ones. Could someone please tell me what is wrong with this formula. Erase watchlist is always set to Yes.

_SECTION_BEGIN("Watchlist"); 

procedure CategoryNameRemoveSymbol(Symbol, ListName, Category)

{
    ListNum = CategoryFind(ListName, category);
    CategoryRemoveSymbol(Symbol, category, ListNum);
}

procedure CategoryNameAddSymbol(Symbol, ListName, Category)

{
    ListNum = CategoryFind(ListName, category);
    CategoryAddSymbol(Symbol, category, ListNum);    
}

EraseWatchlist = ParamToggle("Erase Watchlist", "Yes|No", 0);

CatName = "Leaders";

if (EraseWatchlist == 0)
{
    CategoryNameRemoveSymbol("", CatName, categoryWatchlist); 
}


if (LastValue(Filter))
{
    CategoryNameAddSymbol("", CatName, categoryWatchlist);    
} 

_SECTION_END();
2 Likes

Does anybody have this code in afl code or at least can anybody guide me to get a start on this or redirect me to some links somewhere else....

Please help.

_SECTION_BEGIN("Watchlist"); 

procedure CategoryNameRemoveSymbol(Symbol, ListName, Category)

{
    ListNum = CategoryFind(ListName, category);
    CategoryRemoveSymbol(Symbol, category, ListNum);
}

procedure CategoryNameAddSymbol(Symbol, ListName, Category)

{
    ListNum = CategoryFind(ListName, category);
    CategoryAddSymbol(Symbol, category, ListNum);    
}

EraseWatchlist = ParamToggle("Erase Watchlist", "Yes|No", 0);

CatName = "Leaders";

if (EraseWatchlist == 0)
{
    CategoryNameRemoveSymbol("", CatName, categoryWatchlist); 
}


if (LastValue(Filter))
{
    CategoryNameAddSymbol("", CatName, categoryWatchlist);    
} 

_SECTION_END();

The above code , I think could be a good starting point.

However , as the poster himself tells it does not work perfectly----So please any expert mentor on this.

Moderator comment: The above code is not yours. It belongs to this thread (posted earlier by @Ozzietrader above). When posting somebody's else code you need to reference original (quote where you did get from) or simply respond in where original code appears. Remember: code is subject to copyright. Entire forum is subject to Creative Commons NC Attribution SA which requires proper attribution

Procedure to clear watchlist


procedure ClearWatchlist( WatchlistName)
{

	WatchListIdx = CategoryFind(Watchlistname,categoryWatchlist);
	
	if (WatchListIdx > -1) {
		
		str_data = CategoryGetSymbols(categoryWatchlist, WatchListIdx);
		
		for (i = 0; (sym = StrExtract(str_data,i)) != ""; i++)
		{
		
			CategoryRemoveSymbol(sym,categoryWatchlist,WatchListIdx);
		
		}
	
	
	}

}
	

Hi dhermanus

How do I incorporate that formula into the one that I have? Do I just add it to it ?

Thanks,

Oz

Just copy and paste the procedure on top of your coding.. then call it.. eg. if your watchlist is call fav1

then just call the procedure :

ClearWatchlist("fav1");

PS. Remember to paste the procedure on top first.

1 Like

I must have something wrong. Your procedure will clear the watchlist ok but when the two formulas are combined the watchlist doesn't populate with the new list of stocks. I presume that I edit my formula and remove the CategoryNameRemoveSymbol part from my original formula since your procedure has already accomplished that. What do I have wrong?

_SECTION_BEGIN("Watchlist"); 

procedure ClearWatchlist( WatchlistName)
{

	WatchListIdx = CategoryFind(Watchlistname,categoryWatchlist);
	
	if (WatchListIdx > -1) {
		
		str_data = CategoryGetSymbols(categoryWatchlist, WatchListIdx);
		
		for (i = 0; (sym = StrExtract(str_data,i)) != ""; i++)
		{
		
			CategoryRemoveSymbol(sym,categoryWatchlist,WatchListIdx);
		
		}
	
	
	}

}

CatName = "Leaders";

ClearWatchlist(CatName);

procedure CategoryNameAddSymbol(Symbol, ListName, Category)

{
    ListNum = CategoryFind(ListName, category);
    CategoryAddSymbol(Symbol, category, ListNum);    
}


if (LastValue(Filter))
{
    CategoryNameAddSymbol("", CatName, categoryWatchlist);    
} 

_SECTION_END();

Your symbol code is an empty string.

If you are using the procedure in explorer/AA use this so that the ClearWatchlist call run once for each run :

if ( Status("stocknum") == 0 )
{
   ClearWatchList("Leader");
}

1 Like