Hi,
I'm trying to code a portfolio level system wich relies on a simple moving average cross on the top N ranking assets of a watchlist.
I've read the docs and a lot of forum posts and also asked ChatGPT (got what seems a "junk response" with a code using a non existent AFL function named "Rank(PositionScore)") but nothing seems to match exactly what I'm looking for or perhaps I'm not using the correct words in my search.
My current understanding is that the way to go is using StaticVarGenerateRanks, but I'm asking for help to make sure there is not an easier way or a better solution as I'm ranking based on a unique indicator and not a more complex formula.
An example code with errors of what I'm trying to acomplish:
PosQty = 5; // Define how many open positions you want at most
SetOption("MaxOpenPositions", PosQty );
PositionSize = -100/PosQty; // Size every position as 100% of portfolio equity divided by max. positions number. Equal weight.
// Research how to size as % of available cash instead of portfolio equity
// Research how to size in function of PositionScore rank,
// example if 5 positions... (5/15)33% for top ranked, 27%, (4/15) for the 2nd .... (1/15) 7%
PositionScore = ROC(C,180);
Buy = Cross(MA(C,5),MA(C,20)) AND PositionScore>20; //This doesn't work as PositionScore is not the rank position of the ticker
// but the value of ROC asigned to PositionScore in the line before
// I want to take signals only in the top 20 ranking assets based on an indicator. My understanding is that PositionScore doesn't act as a filter but as a preference function and will take a signal on the 100th ranked asset if it is the only signal generated.
Sell = Cross(MA(C,20),MA(C,5)) ;
Thanks for your appreciations.