I would like to create a universe that -for example- keeps all the stocks in the top30% SubIndustries in terms of Pr13W%Chg
Think of it as “all the stocks belonging to SubIndustries that have best performed recently” (excluding dividends)
I tried the following 2 lines in the screener (starting with all fundamentals)
setvar(@SubIndusRank, Frank(“Aggregate(“Pr13W%Chg”,#SubIndustry, #Avg,16.5,#Exclude,false,true,1)”,#all,#desc))
@SubIndusRank>=70
The first line is an attempt to compute -for each stock in all fundamentals- the rank of its SubIndustry based on price change over the last 13 weeks (excluding dividends)
The second line keeps all the stocks belonging to any and all SubIndustries ranked in the top 30% by Pr13W%Chg (top30% of 160 SubIndustries = circa top 50)
I get → ERROR: Invalid criteria in Rule 1. Error near ‘Aggregate’: Aggregate - Missing operands
wwasilev , sorry didn’t see your solution. It’s very similar. I’m using our pre-built subindustry series accessing them via the Close() function, you are computing them on the fly . The other difference is that we do cap weighting.
Formula functions need to run before rules. The logic is not looking inside custom formulas to find them, probably by design. In any event using close(bar,#SubIndustry) to calculate return achieves the same.
That’s because Close accepts #SubIndustry. What’s the solution for factors that don’t? As an example, say instead of Pr13W%Chg (or close(0,#SubIndustry)/close(60,#SubIndustry)) , I wanted to use the ratio OpIncQ/OpIncPYQ. Is there a P123 solution for that?
So, if I’m not mistaken, depending where “Aggregate(“Pr13W%Chg”,#SubIndustry,#Avg,16.5,#Exclude,false,true,1)” is defined (custom formula or SetVar), the following is true for its use in FOrder;
screener: both custom formula and setvar work
universe: only setvar works
simulation: neither custom formula or setvar work
Is that right?
Walter
EDIT: FOrder(“$Pr13WPctChgSubInd”,#All,#Desc,True)<100 seems to work when $Pr13WPctChgSubInd is invoked in a prior rule. I seem to remember that from an earlier discussion.
Talked it over, and looks like there’s no way to rank an industry formula vs. other industries in a buy/sell rule. The easiest solution is probably to add specific functions that rank industries, like AggregateRank, AggregatePos, etc.
Thank you very much both Marco and Walter. It is amazing what we can do on this platform.
Now: replacing in Marco’s original example screen
showvar(@subindorder,FOrder(“close(0,#SubIndustry)/close(60,#SubIndustry)”,#all,#desc,true))
by
showvar(@SubIndorder,Aggregate(“close(0)/close(60)”,#SubIndustry,#Avg,0,#exclude,false,true,0))
should give the same result, no?
But with Marco’s screen, there are 1843 out of all-fundamentals 6131 stocks that pass
With my Aggregate substitute, I have only 1781 passing stocks.
I tried various parameters for aggregate (cutting the tails or not, #exclude or #winsor, etc)
The Aggregate function will give a single answer for every stock in the subindustry. The FOrder is assigning one value to each stock.
If I just grab every stock for which close(0,#subindustry)/close(60,#subindustry) is greater than the subindustry aggregate, I get 3431 companies.
That number is going to be affected by the number of companies in each subindustry and exactly where the aggregate ends up. I always think of Air Freight and Regional Banks when dealing with this stuff: Air Freight has 14 companies, while Regional Banks has 448. The number that exceed the Aggregate are going to vary, but obviously it’s not going to hit the 50% mark every time. Marco’s screen will.