Delta between two Watch Lists

Hi,

Every week i run exploration and save the results in a Watch List. A feature i was looking for is to find the following - Delta/Difference between 2 or more watch lists. This will help find symbols that are

  • appearing new for first time in current latest watchlist
  • appearing again and again in earlier and current watch list

What i mean is a feature to find the following

  • SYMBOLS present in both ( for 2 watch lists) or all ( more than 2) watch list(s)
  • SYMBOLS present only in the latest /current watch list.
  • SYMBOLS absent in the current watch list.

Example:
Watch List 1
A1
A2
A3

Watch List 2
A1
A4
A2

SYMBOLS present in both Watch List 1 and 2 - A1,A2
SYMBOLS added new in Watch List 2 - A4 [ The latest watch is the one specified the last when providing input ]
SYMBOLS absent in current Watch list - A3

A similar feature is provided by the UNIX comm utility. Has anyone attempted any thing of this sort, or can share pointers on how to do this?

Thank you for your help

Regards
Harsha R

We cannot call for such a feature, but you need to write a little bit of code on your own.

  1. Use this to get comma separated list of symbols in each list
    CATEGORYGETSYMBOLS( categoryWatchlist, watchNumber, 0 )
    (amibroker.com)]https://www.amibroker.com/guide/afl/categorygetsymbols.html

  2. Use string function to test if symbol present. You can iterate the string as shown in the example and check.
    STREXTRACT https://www.amibroker.com/guide/afl/strextract.html
    STRFIND https://www.amibroker.com/guide/afl/strfind.html

  3. Another function similar to above is CategoryFind( symbol , categoryWatchlist )
    https://www.amibroker.com/guide/afl/categoryfind.html

1 Like

That's incorrect. There is feature:

InWatchlistName() function (Well, or InWatchlist() function if you check by WL number).

Also there is not loop required.

/// Find memberships in defined watchlists.
/// @link https://forum.amibroker.com/t/delta-between-two-watch-lists/24576/3
WL1 = "SP 500";// insert your Watchlist1 name
WL2 = "NASDAQ 100";// insert your Watchlist2 name

mem1 = InWatchListName(WL1);
mem2 = InWatchListName(WL2);

// being present in either watchlist	
mem_cond = (mem1 + mem2) > 0;

// being present in both WLs
mem3 = (mem1+mem2) == 2;

// OUTPUT
SetOption("Nodefaultcolumns", True);
SetSortColumns(-4,1);// Sort by mem3
Filter = mem_cond AND Status("lastbarinrange");
AddTextColumn(Name(), "Ticker", 1);
AddTextColumn(StrExtract("FALSE,TRUE",mem1), "In "+WL1, 1);
AddTextColumn(StrExtract("FALSE,TRUE",mem2), "In "+WL2, 1);
AddTextColumn(StrExtract("FALSE,TRUE",mem3), "In "+WL1 + " && " + WL2, 1);

23

8 Likes

@fxshrat, another great bit of code.

Although I have to say I initially misread the SetOption field as "NodeFaultColumns" and it took me a few minutes to realize that it was actually "NoDefaultColumns". (Low on Coffee I guess :grinning:)

1 Like

nice one Mr. @fxshrat
By feature i meant not some readymade UI or Straight forward Function that we can call like Unix comm but what i meant is we need to write some code using already available functions.
Here you have nicely shown how we can add two arrays and deduce some output, especially with InWatchListName()

Thank you @fxshrat !

For what it is worth, it would be probably clearer, if instead of addition the code used logical (Boolean) operators:
Instead of this:

// being present in either watchlist	
mem_cond = (mem1 + mem2) > 0;
// being present in both WLs
mem3 = (mem1+mem2) == 2;

Logical operators provide clarity:

// being present in either watchlist	
mem_cond  = mem1 OR mem2; 
// being present in both WLs
mem3 = mem1 AND mem2;

It is also useful to use meaningful variable names:

// being present in either watchlist	
present_in_any  = mem1 OR mem2; 
// being present in both WLs
present_in_both = mem1 AND mem2;

FWIW, why I have choosen addition...

You would just have to add it single time.

// sum up memberships
memship_sum = mem1 + mem2;   
   
// being present in either watchlist   
inWL_either = memship_sum > 0;

// being present in both WLs
inWL_all = memship_sum == 2;

So if you have more Watchlist (or want to check for number of watchlists flexibly) you may write loop like this (for example):

/// Find memberships in defined watchlists.
/// @link https://forum.amibroker.com/t/delta-between-two-watch-lists/24576/8
WL1 = "WL Name 1";// insert your Watchlist1 name
WL2 = "WL Name 2";// insert your Watchlist2 name
WL3 = "WL Name 3";// insert your Watchlist3 name
WL4 = "WL Name 4";// insert your Watchlist4 name
WL5 = "WL Name 5";// insert your Watchlist5 name
WL6 = "WL Name 6";// insert your Watchlist6 name
// etc.

// set number of watchlists to check (see above)
wl_num = 6;

for ( i = 1, memship_sum = 0; i <= wl_num; i++ ) {
	inWL = InWatchListName(VarGetText("WL"+i));
	VarSet("inWL"+i, inWL);
	// sum up watchlist memberships
	memship_sum += inWL;
}

// being present in either watchlist	
inWL_either = memship_sum > 0;

// being present in ALL WLs
inWL_all = memship_sum == wl_num;

// OUTPUT
SetOption("NoDefaultColumns", True);
SetSortColumns(-(wl_num+2),1);// Sort by inWL_all
Filter = inWL_either AND Status("lastbarinrange");
AddTextColumn(Name(), "Ticker", 1);
False_True = "FALSE,TRUE";
for ( i = 1; i <= wl_num; i++ )
	AddTextColumn(StrExtract(False_True,VarGet("inWL"+i)), "In "+VarGetText("WL"+i), 1);
AddTextColumn(StrExtract(False_True,inWL_all), "In ALL watchlists", 1, -1, -1, 150);

NOTE: of course one may use logical operators with loop too... but I didn't.

Anyway... probably being a matter of taste what you do like/prefer more.

OK, I was just pointing out the fact that adding boolean values may be confusing for newbies not familiar with True == 1 equality in AFL.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.