Choosing the outperforming stock in an outperforming sector/fund

My current strategy is as such, i try to buy the top 3 performing sectors in the watchlist X (having xle, spy, xlk, gld etc etc inside) by way of seeing which has the highest gain. my code is as such

//Ranking criteria (Highest % gain in the week)
Gain=100*((Close-Open)/Open);

//Ranking
PositionScore = Gain;

Buy = PositionScore<4; // chose the top 3 highest gain tickers in the watchlist X
Sell = PositionScore>5; // sell those that fall out from the top 5

Now, i wish to take it a step further. instead of buying the tickers in the watchlist, which are all funds. i plan to buy the best performing stock in the fund instead of the fund itself.

For example, the best performing fund in watchlist X is xle, so i wish to buy the best perfoming stock in the xle fund (made up of slb, eog, cop, mpc, pxd etc), say PXD.

I have already created each fund's watchlist so XLE has a watchlist that contains all the stocks making up XLE.

However, i do not know how to code the portion of selecting the best performing stock in the XLE watchlist.

can anyone help?

This is a crude overview if it helps you start.

AFL Function Reference - CATEGORYGETSYMBOLS (amibroker.com)
You can get a comma-separated list of symbols.

// code that will map each fund to its watchlist number.
wlistIndex = // assign the watchlist index here
list_symbols = CATEGORYGETSYMBOLS(categoryWatchlist, wlistIndex, 0);

then scroll down in the same manual page, you can see FOR loop to iterate over all the symbols and perform simple ranking by comparing and storing the better symbol in a variable.

For complex ranking, you can see this
Ranking features (amibroker.com)
the 3rd way using StaticVarGenerateRanks function

1 Like

Thanks nsm51, but how will the program know which watchlist to look into to get the stocks? for example, xle, xlf, xlv came up the top 3 among the watchlist X, how will the system know to look into xle, xlf, xlv?

i tried to 'extract' the #1, #2, #3 in the positionscore ranking and then try to get the program to look into the watchlist but failed. code is below

Top1 = PositionScore=1;
Top2 = PositionScore=2;
Top3 = PositionScore=3;

list_symbolstop1 = CATEGORYGETSYMBOLS(categoryWatchlist, Top1, 0);
list_symbolstop2 = CATEGORYGETSYMBOLS(categoryWatchlist, Top2, 0);
list_symbolstop3 = CATEGORYGETSYMBOLS(categoryWatchlist, Top3, 0);

It is strongly recommended to actually read this tutorial slowly and carefully, before going anywhere further:

https://www.amibroker.com/guide/h_ranking.html

On a small note, comparison in AFL is ==, so your "pseudocode" above is incorrect.