Filter
kaya-filterCoreVariants
One module per value type. They behave identically; pick the one matching the type you are wiring.
| Name | Type |
|---|---|
| Filter Boolean | True/false |
| Filter Double | Number |
| Filter Float | Number |
| Filter Integer | Whole number |
| Filter String | Text |
Cost
1 credits for n bars, rounded up. The marks are each plan's limit of bars.
A sweep gives you many candidates. Filter keeps the ones that pass a test, ranks them, and forwards the best few, in rank order, on its single output.
Where Collapse forwards exactly one winner and ends the sweep, Filter forwards up to Take survivors, and the rest of the strategy runs once per survivor. The sweep itself is explained on the For Each page.
Wiring
Filter has one output you can wire: output (labelled Output in the editor). Every connection out of a Filter must use it.
The module also lists the outputs rank_0 … rank_19 and selectionIndex. They hold what Filter picked: rank_j is the rank-j survivor's Items series, and selectionIndex lists the survivors' input positions in rank order. Read them to see the result; output itself holds no data. They cannot be wired. Saving a connection from any of them is rejected:
Module Simple Moving Average input price_data is wired to Filter Float output 'rank_0', which cannot be connected; connect from 'output' instead.
What output does depends on what it feeds:
output wired into | Result |
|---|---|
| An ordinary module input | That module, and everything downstream of it, runs once per rank: copy j receives the rank-j survivor. |
| Another Filter's or a Collapse's Items | Each rank is one candidate. |
The slots input of a slot-based For Each (e.g. For Each Float Array) | Each rank is one iteration. |
You cannot connect to "rank 2 only". To use only the best candidate, set Take = 1 or use Collapse.
The number of ranks is min(Take, number of candidates). A rank with no survivor is empty, and its downstream copy does not run.
flowchart LR
FE["For Each<br/>8 candidates"] --> IND["Indicator<br/>8 copies"]
IND -- Items / Sort / Condition --> F["Filter<br/>Take = 3"]
F -- output --> D0["Downstream copy: rank 0"]
F -- output --> D1["Downstream copy: rank 1"]
F -- output --> D2["Downstream copy: rank 2"]
Inputs
| Input | What you wire | Effect |
|---|---|---|
| Items | The candidate series, one row per combination. 1 to 20 slots. | What gets forwarded. |
| Condition (optional) | One boolean series per candidate. | A candidate survives only if its last non-empty value is true (true, 1, or any non-zero number). |
| Sort (optional) | One numeric series per candidate. | Survivors are ranked by their last non-empty value. |
| Order | desc or asc | desc puts the highest Sort value at rank 0, asc the lowest. |
| Take | Whole number, at least 1 | Maximum survivors forwarded. |
Set Order and Take explicitly. The editor pre-fills order = asc and take = 20; a module created without them ranks desc and forwards every survivor.
Condition and Sort rows line up with Items rows, so wire them from the same sweep. They may not span a loop the Items do not span; that is rejected with "rank inputs cannot introduce a loop the items do not collapse."
How Selection Works
- Drop empty candidates. A candidate whose branch produced no data upstream keeps its input position but is never selected.
- Gate. With Condition wired, a candidate is eliminated unless its last Condition value is true. A missing or empty Condition series eliminates the candidate. With no Condition wired, nothing is eliminated.
- Rank. With Sort wired, survivors are ordered by their last Sort value. A value that is missing or not a number ranks last under both
ascanddesc. The sort is stable: ties, including two unreadable values, keep input order. With no Sort wired, survivors keep input order. With neither Sort nor Condition wired, the strategy carries the warning "Filter … has no sort or condition; it takes candidates in input order." - Take. The first Take survivors become ranks 0, 1, 2 … Survivors beyond Take are dropped.
Selection is a decision about the end of the run, not bar by bar.
Input order is Items slot order, and within a slot the order of the sweep's combinations. When ties are likely (counts, ratings, rounded scores), that order is the tiebreaker.
Worked Example: Sector Rotation With a Trend Gate
Eight sector ETFs swept on monthly closes, decision date 31 October 2018.
- Sort: trailing 12-month return. Order
desc. - Condition: price above its 10-month simple moving average.
- Take: 4.
| Input order | Sector | Sort: 12m return | Condition: above average? |
|---|---|---|---|
| 0 | XLK | +11.6132% | false |
| 1 | XLE | +1.6399% | false |
| 2 | XLF | +0.4876% | false |
| 3 | XLV | +11.0128% | true |
| 4 | XLU | +0.8428% | true |
| 5 | XLY | +15.9317% | false |
| 6 | XLP | +6.5115% | true |
| 7 | XLI | −0.5151% | false |
Three survive the gate. Candidates = 8 and Take = 4, so a module wired to output runs four times:
| Downstream copy | Receives | Stored selectionIndex |
|---|---|---|
| rank 0 | XLV (+11.0128%) | 3 |
| rank 1 | XLP (+6.5115%) | 6 |
| rank 2 | XLU (+0.8428%) | 4 |
| rank 3 | nothing; this copy does not run |
Plan for empty ranks: if each rank sizes a fixed fraction of capital, an empty rank leaves that fraction in cash. Without the gate, Sort alone would have picked XLY and XLK, both already below their trend.
Edge Cases
| Case | Result |
|---|---|
| Every candidate eliminated | Every rank is empty; no downstream copy runs. |
| Take greater than the number of candidates | Warning "Filter take=… exceeds candidate count=…"; the number of ranks is the number of candidates. |
| One candidate | A gate: rank 0 holds it if Condition passes, otherwise rank 0 is empty. |
| A candidate whose history ends early or starts later | A series that ends early repeats its final value forward. One that starts later is empty before its first bar, and its rank keeps those empty bars. |
| Breadth | Items takes up to 20 slots. No module, including a copy downstream of output, may run more than 20 times. |
Ranking on Backtested Statistics
Ranking twenty parameter variants on a backtested Sharpe ratio and trading the winner selects the luckiest variant, not the best one (Bailey et al., Pseudo-Mathematics and Financial Charlatanism). Prefer Sort keys that are properties of the asset (momentum, volatility, yield), keep the sweep small and decided in advance, use Condition as a risk floor rather than a tuned threshold, and confirm the choice on data it was not selected from.