Count Num Symbols in Watch List Referencing Name

I’m trying to find a way to count the number of symbols in a watch list (the number of symbols will be dynamic), and if possible, I prefer to reference the watch list by name, rather than by the ordinal number of where the watch list falls in a list of watch lists.

I have found the example function below, but I think it references watch lists by the order in which they appear in a list, and I can’t be sure a given watch list will always be in the same order. Is there a way to evolve the code below so that I can count the number of symbols in a watch list by referencing the watch list by name?

function CategoryCountSymbols( category, number )
{
   count = StrCount( list = CategoryGetSymbols( category, number ), ",");  
   return IIf( list == "", 0, count + 1 );
}    

Title = "Symbols in watchlist 0: " + CategoryCountSymbols( categoryWatchlist, 0 );

Or is there an even more simple way to do without creating a function. Here’s a pseudo code scenario of what I’m trying to do:

NumberofSymbolsinWatchList = Count(InWatchListname(”TargetedWatchList”));
Condition = Close > MA(Close > 5);
PercentofSymbolsAbove5SMA = NumberofSymbolsinWatchList / Condition;

I really appreciate any ideas. Thanks for your consideration!

This code should work:

EnableTextOutput( False ); // not necessary
WatchlistName = "Alarm 3";
WatchlistNumber = CategoryFind( WatchlistName, categoryWatchlist );
WatchlistStr = CategoryGetSymbols( categoryWatchlist, WatchlistNumber );

if( WatchlistStr != "" ) WatchlistNumOfSymbols = StrCount( WatchlistStr, "," ) + 1;
else WatchlistNumOfSymbols = 0;

printf( "\nWatchlist  < %s >   contains  %g symbols.", WatchlistName, WatchlistNumOfSymbols );

Przechwytywanie

Hope it helps ...

5 Likes

… I should have mentioned, that the above code requires AB 6.17 to work properly (just because of the %s string specifier used in printf() line). If someone has an older version, he/she can replace the printf() line with (for example) this one:

printf( "\nWatchlist  < " + WatchlistName +  " >  contains  " + WatchlistNumOfSymbols + " symbols");
1 Like

@Milosz has given you one of many possible ways of counting the symbols in a WL. Like most things, AmiBroker can arrive at a solution in different ways. Regardless, you did mention your ultimate goal was

Have you reviewed this recent thread on the forum ?

3 Likes

Yes, of course I agree with you Larry ( @portfoliobuilder ). I have just shown an example code which might be used as a stand-alone solution to count any watchlist members providing its name.

But if @seaworthy wants to accomplish his goal, he rather needs to read about AddToComposite() function and Composites based on Static Variables - for example in this thread (with Tomasz’s remarks and suggestions to use StaticVarAdd() whenever possible):

2 Likes

How to get watchlist number+members by watchlist name has been posted already. Please use forum search.

EDIT: same for counting number of symbols of categories

2 Likes

@seaworthy I think @fxshrat and @Milosz posts should help to remind you and anyone else that there is already a wealth of information on this forum. You need to spend a little time searching and you will find either exactly what you were looking for, or a good beginning to your desired solution.

Considering the forum is only a few months old, I think that is a real testament to how great things have worked out since moving to Discourse.

4 Likes

Thank you, Milosz and others. I was able to make this work for my code. Really appreciate your contributions to the forum.

3 Likes