Ranking and Relative Strength
Every question this course has asked so far could be answered by one symbol on its own. Is the close above the 200-day average? Has volume expanded? Did price cross the upper band? Point AmiBroker at a thousand symbols and it will answer the same self-contained question a thousand times, in parallel, and hand you the list of the ones that said yes.
This part asks a question of a completely different shape. Is this symbol among the twenty strongest in its market today? No amount of staring at that symbol’s chart will answer it, because the answer does not live in that symbol’s data. It lives in the comparison between that symbol and every other one, on the same bar. That is a cross-sectional question, and it needs machinery the previous parts have not used.
Why this is harder than it looks
Section titled “Why this is harder than it looks”An AFL formula executes against exactly one symbol at a time. In the Analysis window,
AmiBroker runs the formula once per symbol, and — since version 5.50 — it runs many of
those executions on separate threads at the same time. That design is what makes scanning
a large database fast. It is also precisely what stands between you and a ranking: your
formula, running on ABC, has no way to see the number your formula produced for XYZ,
because that run may not have happened yet, may be happening right now on another core,
or may have finished and thrown its results away.
Foreign() and SetForeign() do not solve this. They let you read another symbol’s
price data; they do not let you read another execution’s computed results. You could
rebuild every peer’s score inside every symbol’s execution, but for a universe of five
hundred names that is two hundred and fifty thousand foreign reads to produce five hundred
numbers, and it will be slower than the answer is worth.
The solution AmiBroker documents is a two-pass workflow built on static variables: a process-wide store that every formula in the running application can read and write. Pass one computes and stores one score per symbol. A ranking function then sorts those stored scores across the universe. Pass two runs per symbol and simply reads its own rank back. Three of the five pages in this part are about getting that right, because getting it subtly wrong is easy and produces numbers that look entirely reasonable.
What this part covers
Section titled “What this part covers”The first lesson is about meaning rather than mechanics: what relative strength is as a measurement, what changes when you switch the benchmark, why “the strongest stock” is a statement about a time window rather than about a stock, and what a relative strength reading genuinely does not tell you.
The second draws the line between filtering and ranking. They answer different questions, they fail in different ways, and in a falling market they can disagree completely about what you should be looking at.
The third is the engine room: static variables, their lifetime, the persistent
argument, naming and collisions, cleaning up between runs, and what multi-threading does
and does not require of you. The fourth adds StaticVarGenerateRanks(), the documented
ranking function, along with the verification procedure that catches it being wrong.
The last page is a project: a working relative strength ranking tool with an Exploration front end, a self-check built into its own output, and an honest account of everything it does not do.
What changes for you
Section titled “What changes for you”By the end of this part you should be able to:
- state precisely what a relative strength number measures, and against what;
- explain why a rank cannot be computed the way a filter is computed, in terms of how AmiBroker executes formulas;
- write, run and clean up after a two-pass cross-sectional formula;
- call
StaticVarGenerateRanks()with the right arguments and read its output back under the right variable name; - verify a ranking by hand rather than trusting that it looks sensible;
- say, without hedging, why a ranking is not a trading system.
What you need first
Section titled “What you need first”Parts 8 to 12. You need to be comfortable with arrays, with Param(), with the Analysis
window’s Exploration mode and the Filter variable, and with reading AddColumn() output.
Part 11’s material on user-defined functions and the local keyword appears in the
project formula. If SetForeign() and RestorePriceArrays() are new to you, they are
introduced here in enough detail to follow along; Part 15 takes them apart properly.
For data, end-of-day bars for twenty to five hundred symbols in one market are enough, and a free end-of-day source is fine. Nothing here needs a live feed, and nothing here needs the Professional edition — the only difference the edition makes is how many threads an Analysis window may use, which changes how long a run takes and not what it returns.
One thing to carry through the whole part
Section titled “One thing to carry through the whole part”A rank is a description of a cross-section at a moment. It says where a symbol stood relative to its peers, on the measurement you chose, over the window you chose. It contains no claim about what happens next, no allowance for the cost of acting on it, and no notion of risk. Every page in this part is written on that assumption, and the project ends by spelling out exactly what would have to be added before a ranking could become something you trade.
0 / 5 lessons in this part completed
Progress tracking needs browser storage, which is unavailable here. The course works exactly the same without it.
- LessonRelative Strength: What It Does and Does Not Mean26 min
- LessonRanking versus Filtering24 min
- LessonStatic Variables for Cross-Sectional Work30 min
- LessonStaticVarGenerateRanks() and Top-N Selection30 min
- ProjectProject: Market Relative Strength Ranking Tool60 min
Sources for this lesson
2 verified · checked 2026-08-31
- 01AmiBroker User's Guide - Ranking functionalityamibroker.com/guide/h_ranking.html2026-08-31
- 02AmiBroker User's Guide - Efficient use of multithreadingamibroker.com/guide/h_multithreading.html2026-08-31
Every technical claim on this page was checked against the official AmiBroker documentation on the date shown. Where the course disagrees with folklore, the source is how you can tell which one to trust.