How to add a ranking to script for backtester

I have a model that I am testing that has 25 ETFs available in our investable universe.

The model allocates 20% to 5 positions.

I noticed that on different days, different holdings show up in the trade list when there are more than 5 ETFs that meet the buy criteria.

For example, on 12/5 SOXX showed up on the trade list. Today it has disappeared and another ETF has replaced it. Both still meet the buy criteria.

Is there a way to add a script to rank the holdings (or add something else) so that the ETFs don’t change?

Here is the code I am using for buy signals: (I am using the 1 week timeframe)

// Quad 0

c1 = r1.Heading > 0 and r1.Heading < 90; 
c2 = r1.Quadrant == 0; 
c3 = r1.Distance >2;
k1 = c1 and c2 and c3;
a1 = OFFSET(k1, OFFSET=1);

//Quad 1

c1a = r2.Heading > 45 and r2.Heading < 0; 
c2a = r2.Quadrant == 1; 
c3a = r2.Distance >2;

k2 = c1a and c2a and c3a;
b1 = OFFSET(k2, OFFSET=1);

//Quad 2

c1b = r3.Heading < 359 and r3.Heading > 315;
c2b = r3.Quadrant == 2;
c3b = r3.Distance >2;

k3 = c1b and c2b and c3b;
d1 = OFFSET(k3, OFFSET=1);

//Quad 3

c1c = (r4.Heading >315 and r4.Heading < 355) or (r4.Heading > 0 and r4.Heading < 90); 
c2c = r4.Quadrant == 3; 
c3c = r4.Distance >2;
//Buy Setup
k4 = c1c and c2c and c3c;
e1 = OFFSET(k4, OFFSET=1);

//Criteria #2
//Above WVS?

//Buy Signal

w1 and (a1 or b1 or d1 or e1);


Thanks John. Rebalancing or ranking strategies only work on discrete values. Because your ranking is based on True/False conditions if you have more than 5 that are true then the back tester randomly picks 5 as they all have the same ranking value of 1.

One way around this would be to add a discrete value that is unique to each ETF for the ranking criteria, eg using 10 day ROC, or r1.Distance, and add your script as the Filter. This means that only the ETFs that pass the filter are up for selection, which are then ranked on a discrete value.

Thanks Darren-

Maybe it is all the holiday food, but I can’t figure out what you mean by this:

One way around this would be to add a discrete value that is unique to each ETF for the ranking criteria, eg using 10 day ROC, or r1.Distance, and add your script as the Filter.

Can you point me in the direction on how to rank by r1.Distance then pick the top 5?

I scanned the forums and couldn’t find anything similar to what I am trying to do.

Thank you so much for your help!

Sorry I thought you were doing a rebalance test to pick the best 5 each period. See here for more info, including the Filter and Ranking options:

The ranking formula would be this (note that if you are running on a weekly timeframe you don’t have to specify the timeframe):


Is there a way to do this in the signal backtester instead of using rebalance?

My exit on the model is a stop based on volatility. I’d like to not rebalance the portfolio every week/signal change.

The signal backtest does everything I need, except ranking.


Hi John,

I can’t think of an easy way to combine true/false boolean results with a discrete ranking value in the backtester. However, if you would like for me to explore further we can arrange a scripting consultation.

Let me know how long you would like me to spend and I’ll see what I can do.

Thanks Darren - I didn’t even know I was able to hire you guys by the hour.

I am going to set this up after the holidays.

Thanks so much, Merry Christmas and have a wonderful New Year!

No worries - and same to you! See here for our consulting services: