Dynamic Watchlist Selection based on a formula

Hi,

Is it possible to have a watchlist selection done dynamically using a formula ?

for example if SPX/Ruseell 2k is > EMA(SPX/Ruseell 2k, 20) then choose watchlist SPX else chose watchlist Ruseell 2k

Could anyone guide me to the relevant page/example for this ?

thanks

1 Like

If you mean changing the base symbols as per the Watchlist in the Analysis Filter settings, then i think the answer is no.
But it doesn't end there, because that approach of dynamically changing watchlist (base symbols) will fail, because the AFL runs after those initial settings are parsed, those symbols being referenced etc and because of a multithreaded design among other things.
All this considering you are trying to achieve it in one AFL ( and one run)

The better approach is to add both or as many watchlists as you want to the Filter settings, then dynamically ( in your AFL ) check for Membership of each symbol pertaining to the condition that you are setting, in this case, SPX/Ruseell 2k is > EMA(SPX/Ruseell 2k, 20) to its corresponding Watchlist.
This condition itself can be checked in this first symbol code block,

if ( Status("stocknum") == 0 )	{
	// Compute your condition here,
    // You can use Foreign() to refer other symbols and compute on them
    // Decide which watchlist is the relevant one here

}

Since you already know the Watchlist Index, you can get the Symbols from that list explained here
https://www.amibroker.com/guide/afl/categorygetsymbols.html

You will filter and work only on the symbols that are present in the specific watchlist.

some topics from the forum

1 Like

You would have to run analysis "Apply to" on "All symbols" and do WL membership check via InWatchList function.

/// to be run on "Apply to" -> "All symbols" of analysis toolbar
/// https://forum.amibroker.com/t/dynamic-watchlist-selection-based-on-a-formula/12915/3
watchlist1 = 0;
watchlist2 = 1;
/// Note: instead of running on "All symbols" (slower) you may create a third watchlist 
/// where you move all symbols of watchlist1 and watchlist2 into it. 
/// Then you set "Filter" -> Watchlist3 of analysis toolbar.

condition = /*your SPX/Russell 2k is > EMA(SPX/Russell 2k, 20) check*/ ;

in_watchlist = IIf(condition, InWatchlist(watchlist1), InWatchlist(watchlist2));

Buy = Cross(Signal(), MACD()) AND in_watchlist;
Sell = Cross(MACD(), Signal());

Note: instead of running on "All symbols" (slower) you may create a third watchlist where you move all symbols of watchlist1 and watchlist2 into it. Then you set "Filter" -> Watchlist3 of analysis toolbar.


To get Watchlist number by name do forum search e.g.

8 Likes

mate this is super helpful. thank you so much for a detailed description. i will need some time to play around with your suggestion and read through the other msgs and try and work something out. really appreciate your time. cheers

wow, you make it look like ABC. thanks again for your help @fxshrat

Actually there is InWatchListName() function too.

So you could just do this (and with set watchlist names existing!)

/// to be run on "Apply to" -> "All symbols" of analysis toolbar
/// https://forum.amibroker.com/t/dynamic-watchlist-selection-based-on-a-formula/12915/3
watchlist1 = "SPX";// WL name has to exist!
watchlist2 = "Russell2000";// WL name has to exist!
/// Note: instead of running on "All symbols" (slower) you may create a third watchlist 
/// where you move all symbols of watchlist1 and watchlist2 into it. 
/// Then you set "Filter" -> Watchlist3 of analysis toolbar.

condition = /*your SPX/Russell 2k is > EMA(SPX/Russell 2k, 20) check*/ ;

in_watchlist = IIf(condition, InWatchlistName(watchlist1), InWatchlistName(watchlist2));

Buy = Cross(Signal(), MACD()) AND in_watchlist;
Sell = Cross(MACD(), Signal());
8 Likes

just brilliant. thank you so much. works like a charm

Is there any way to set default watchlist after AB starts?

There is no such thing as "default watch list"