Ranking stocks in portfolio backtest


In an exploration of a basket of stocks, one can rank columns with several AddRankColumns.


How would I go about doing a backtest where AB buys only if the stock is placed in the top 4 rankings for the day (generated using several AddRankColumns in an exploration)? I have read the KB regarding rankings but I don't think it addresses this particular situation.


Take a look at the StaticVarGenerateRanks() function and associated examples.

1 Like

The solution may not be StaticVarGenerateRanks, at least not always is is best solution. Usually it is total overkill. Whenever possible you should use built-in ranking in backtesting (PositionScore), and yes you can combine many scores into one (math 101).
You should start with reading the manual, as everything is already covered there and it is definitive source information http://www.amibroker.com/guide/h_ranking.html

Entire tutorial part of manual is a must-read:

As every musician should start from Bach, so every AmiBroker user should start from manual (especially tutorial).

1 Like

Thanks Tomasz and Matt,

Positionscore is something I have tried by giving weightings like so. Is this the recommended approach?

x = ROC(C,100);
xx = ROC(C,50);
xxx = ROC(C,10);

F1 = Optimize("f1",1,1,100,1);
F2 = Optimize("f2",1,1,100,1);
F3 = Optimize("f3",1,1,100,1);

PositionScore = F1*x + F2*xx + F3*xxx;


1 Like

Weighting is one of possibilities. It is quick and efficient. If that works for you - perfect, if not explore other options. For example normalize indicators before weighting. ROCs are unbounded, Stochastics, CCI and RSIs are momentum based but are bounded, work better with weighting.