For Each
kaya-for-eachCoreVariants
One module per value type. They behave identically; pick the one matching the type you are wiring.
| Name | Type |
|---|---|
| For Each Boolean | True/false |
| For Each Boolean Array | True/false series |
| For Each Boolean Datapoint | True/false at one bar |
| For Each Boolean Matrix | Several true/false series |
| For Each Boolean Series | True/false series |
| For Each Crypto Ticker | Choice from a list |
| For Each Double | Number |
| For Each Double Array | Number series |
| For Each Double Datapoint | Number at one bar |
| For Each Double Matrix | Several number series |
| For Each Double Series | Number series |
| For Each Float | Number |
| For Each Float Array | Number series |
| For Each Float Datapoint | Number at one bar |
| For Each Float Matrix | Several number series |
| For Each Float Series | Number series |
| For Each Integer | Whole number |
| For Each Integer Array | Whole number series |
| For Each Integer Datapoint | Whole number at one bar |
| For Each Integer Matrix | Several whole number series |
| For Each Integer Series | Whole number series |
| For Each Stock Ticker | Choice from a list |
| For Each String | Text |
| For Each String Array | Text series |
| For Each String Datapoint | Text at one bar |
| For Each String Matrix | Several text series |
| For Each String Series | Text series |
Cost
Every strategy contains a number you guessed. A moving-average window, an RSI threshold, a lookback for a z-score. You picked 20 because 20 felt reasonable, and never went back.
For Each is the module that goes back for you. Instead of one window you declare a whole set, and Systemic runs your strategy for every one, together, on the same data. A single answer becomes a landscape of answers — and the shape of that landscape tells you more than any point on it.
The family has many typed variants: integers, floats, symbols, and every kind of series or array. They behave the same way and differ only in the kind of value they hand you.
What a For Each Does
A For Each defines an iteration set and hands out its members one at a time. Everything downstream is duplicated once per member — the branch fans out.
flowchart LR
FE["For Each Integer<br/>From 10, To 50, Step 10"]
subgraph FAN["One branch, executed 5 times"]
direction TB
E1["element = 10"] --> S1["Strategy branch"]
E2["element = 20"] --> S2["Strategy branch"]
E3["element = 30"] --> S3["Strategy branch"]
E4["element = 40"] --> S4["Strategy branch"]
E5["element = 50"] --> S5["Strategy branch"]
end
COL["Filter or Collapse<br/>consumes the dimension"]
OUT["A single result"]
FE --> E1 & E2 & E3 & E4 & E5
S1 & S2 & S3 & S4 & S5 --> COL --> OUT
style FE fill:#3498db,color:#fff
style COL fill:#27ae60,color:#fff
style OUT fill:#d5f5e3,stroke:#27ae60
style FAN fill:#ecf0f1,stroke:#bdc3c7
It Is Not a Loop That Runs Five Times
You draw the branch once. Before anything starts, it is copied once per value. The copies are identical except for the value put into each, and they all run together as one strategy over the same data.
- There is no "first" or "last" iteration. No copy sees another's result or passes a value to the next. If your idea needs one run's answer to feed the next, a sweep is the wrong tool.
- The cost is width, not wait. Five copies of a twenty-module branch is a hundred modules of work at once, which is what the limits below count.
You Cannot Put a For Each Inside a For Each
A For Each may not be placed anywhere downstream of another For Each. Such a strategy is rejected before it runs, with a validation error stating that nested For Each loops are not supported.
To sweep two parameters at once, place two independent For Each modules on separate branches and let those branches meet downstream. This is the only way to combine loops.
Defining the Iteration Set
Four ways, depending on the type you are iterating over.
1. Numeric ranges — Integer, Float, Double
You give From, To, Step, and a Scale — linear (the default) or logarithmic.
Through the API these are the static inputs foreach.range.from, foreach.range.to, foreach.range.step and foreach.range.scale (linear or log).
Linear. Values climb by Step until they would pass To:
Requires Step greater than zero and From no larger than To.
Logarithmic. Values are multiplied by Step each time:
Requires From greater than zero and Step greater than one. A smaller multiplier would never advance.
Worked Example: From 10, To 50, Step 5, Linear
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|---|
| element | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 |
To is a ceiling, not a destination. Change To to 52 and you get the same nine values — 55 would overshoot, so the range stops at 50.
Linear or Logarithmic?
| Setting | Scale | Values | Count |
|---|---|---|---|
| From 10, To 100, Step 10 | Linear | 10, 20, 30, … , 100 | 10 |
| From 2, To 64, Step 2 | Logarithmic | 2, 4, 8, 16, 32, 64 | 6 |
| From 0.001, To 1, Step 10 | Logarithmic | 0.001, 0.01, 0.1, 1 | 4 |
Use linear when equal spacing matters: windows in days, holding periods in bars. Use logarithmic when the quantity spans orders of magnitude. Sweeping a stop-loss from 0.001 to 1 would need a thousand linear steps but only four logarithmic ones.
2. Boolean
A checklist: tick true, false, or both. At most two elements — the cleanest possible A/B test.
Through the API this is the static input foreach.bool.selection: true, false or true,false.
3. Enum — Crypto Ticker, Stock Ticker
Choose a subset of the available symbols, or take all of them. The elements come out sorted alphabetically by canonical symbol, whatever order you clicked them in. This is the variant that turns a strategy into a screener.
Through the API this is the static input foreach.enum.subset: ALL, or a JSON array of canonical symbols such as ["kc:BTC-USDT","kc:ETH-USDT"]. Without this input the loop runs over every symbol. A symbol the market does not have is a validation error.
One loop can drive every data source of its market at once. The loop iterates the market's symbols, such as BTC-USDT or NVDA, and every data source wired to it reads that symbol from its own provider:
- A crypto loop feeds Cryptocurrency and the Perpetuals sources: open interest, funding rate, long/short ratio and liquidations.
- A stock loop feeds Stock: Price, the SEC filing sources (Fundamentals, Balance Sheet, Income and Cash Flow) and Insider Transactions.
Not every provider serves every symbol, so the loop iterates only the symbols every linked source can serve. A symbol that a linked source lacks is struck out and locked in the loop, and the counter shows what will actually run. Your selection is never edited for you: unlink the source and the symbol comes back. If no selected symbol is left for a linked source, you get a validation error naming that source. A source that publishes each symbol on a fixed schedule takes the schedule all its symbols share; the editor fills it in when you connect the loop.
4. Variadic — Array, Matrix, Series, Datapoint, String
Instead of settings, these give you input slots. Each wired slot becomes one iteration, in slot order. At least one slot must be wired, and there are at most 20. This is how you sweep over things that are not numbers: three price series, four candidate baskets, a handful of labels.
One Loop, One Dimension
Each For Each contributes exactly one dimension to everything downstream of it.
Two Independent Loops Multiply
Two independent For Each modules — each on its own branch, neither downstream of the other — whose branches meet at a common module produce the Cartesian product of their sets. A window loop over and a symbol loop over AAPL, MSFT meet as six combinations: 10·AAPL, 20·AAPL, 30·AAPL, 10·MSFT, 20·MSFT, 30·MSFT.
For independent loops of sizes meeting at one module:
The Same Loop Twice Does Not
If a single For Each reaches a module by two paths — its element feeding both a moving average and a threshold further along — that is still one dimension. The loop is paired with itself, not squared: element 20 on the left meets element 20 on the right, never element 30.
The Cap: 20 Combinations
Twenty is the number to remember, and it binds in two places:
- No single loop may have more than 20 elements. A range, enum subset, or slot count resolving to 21 or more is rejected on its own.
- No module may see more than 20 combinations. Loops of 5 and 4 are fine, and so is their meeting point at 20. Make them 5 and 5 and the meeting point is rejected at 25, though neither loop is too large.
Both errors are raised before the strategy runs.
| Independent loops | Sizes | Combinations | Status |
|---|---|---|---|
| 1 | 20 | 20 | At the ceiling |
| 1 | 21 | 21 | Rejected — the loop itself is too large |
| 2 | 5 × 4 | 20 | At the ceiling |
| 2 | 5 × 5 | 25 | Rejected — each loop is legal, the meeting point is not |
| 3 | 3 × 3 × 2 | 18 | Comfortable |
| 3 | 3 × 3 × 3 | 27 | Rejected |
Three parameters at five values each is 125 combinations. Four is 625. Five is 3,125. This is combinatorial explosion, and it is why the ceiling exists.
A second, wider limit caps the total copied work. It counts every module in the whole strategy that is not itself a For Each, Filter or Collapse, not only the ones downstream of the loop. Combinations here multiply the sizes of every For Each in the strategy, even loops that never meet:
Twenty combinations in a hundred-module strategy sits exactly at that limit. A strategy also cannot hold more than 2000 modules in total.
A Real Sweep: Moving-Average Windows on Bitcoin
A For Each Integer with From 20, To 120, Step 20 gives six values — — namely 20, 40, 60, 80, 100 and 120.
The test. Daily BTC/USD closes, 2021-01-01 through 2025-12-31: 1,826 closes, 1,825 decision days. The rule is deliberately crude — at each close, if price sits above the simple moving average of the last closes, hold Bitcoin through the next day, else hold cash earning nothing. No fees, no slippage, no interest. Each window is primed on closes preceding the start date, so all six see the same 1,825 decisions. Buy-and-hold returned +198.63%.
xychart-beta
title "BTC/USD 2021-01-01 to 2025-12-31: total return by window"
x-axis "Moving-average window (days)" [20, 40, 60, 80, 100, 120]
y-axis "Total return (%)" 0 --> 650
bar [64.8, 573.9, 339.2, 260.0, 373.6, 611.6]
line [198.63, 198.63, 198.63, 198.63, 198.63, 198.63]
Bars: total return per window. Flat line: buy-and-hold.
| Window | Days in market | Total return |
|---|---|---|
| 20 | 927 / 1825 (50.8%) | +64.8% |
| 40 | 940 / 1825 (51.5%) | +573.9% |
| 60 | 979 / 1825 (53.6%) | +339.2% |
| 80 | 1009 / 1825 (55.3%) | +260.0% |
| 100 | 1028 / 1825 (56.3%) | +373.6% |
| 120 | 1054 / 1825 (57.8%) | +611.6% |
The spread runs from +64.8% to +611.6%, nearly a factor of ten, from changing one setting on one module. Worse, it is not orderly: 40 is spectacular, 60 and 80 ordinary, 100 recovers, 120 best of all. Time in the market barely moves, 50.8% to 57.8% — the differences are not about exposure but about which particular days each window sat out. Build this with a window of 40, stop there, and you would believe you had found something.
The Overfitting Trap
The obvious move now is to keep the best number. Window 120. +611.6%. Done.
Do not do that. Split the same test in two: choose on 2021–2023, then check that choice on 2024–2025, which it never saw.
| Window | 2021–2023 (chosen on this) | 2024–2025 (judged on this) |
|---|---|---|
| 20 | +18.5% | +39.1% |
| 40 | +215.4% | +104.4% |
| 60 | +153.9% | +65.5% |
| 80 | +113.1% | +61.6% |
| 100 | +160.4% | +74.1% |
| 120 | +249.2% | +95.0% |
| Buy-and-hold | +44.1% | +98.3% |
The in-sample winner is window 120. Out of sample it came second of six and lost to simply holding Bitcoin — +95.0% against +98.3%. The number that looked like a discovery was, in the period that mattered, worth less than doing nothing.
This is arithmetic, not bad luck. Evaluate variants and keep the maximum, and that maximum is inflated by the noise in your sample — more so the larger gets. As Bailey, Borwein, López de Prado and Zhu put it in Pseudo-Mathematics and Financial Charlatanism: with enough trials, a backtest with a superb in-sample record and zero genuine edge is not merely possible but expected.
Which brings the cap of 20 into focus. It also limits how far you can fool yourself in one pass.
How to Read a Sweep Honestly
- Read the shape, not the peak. A smooth ridge, where neighbouring values behave alike, suggests something real. A peak next to a trough is the signature of noise — and the six windows above are jagged.
- Compare against doing nothing. Over the full span five of six windows beat buy-and-hold; over the out-of-sample half, only one did.
- Judge on data you did not choose on — reserve a period, or roll the fit forward, as in walk-forward analysis.
- Count everything you tried, including the sweeps you abandoned. That total is the honest , and it governs how impressed you should be. This is data dredging, and it is easy to do by accident.
A sweep is not a search for the winning number. It is a robustness test — asking whether your idea survives being nudged.
Closing the Loop
To turn a fanned-out branch back into one result, close the dimension the For Each opened. A Filter ranks the combinations and forwards the top few, one downstream copy per rank. A Collapse reduces every combination to a single winner, the one ranked best, and returns you to a single branch.
Two loops open two dimensions, and each has to be closed. See the Filter and Collapse pages for how to choose between them.
Missing Data
A For Each never invents values and never skips them. The iteration set is fixed from your settings alone, before any market data is read. A range of six values is six copies, whether the data behind them is complete or full of gaps.
Missing data inside a copy stays inside that copy. If one combination has gaps — a moving average not yet warmed up, an asset that did not trade that day — the others are untouched. A gap in one combination is never filled from another.
Watch the variadic variants. A slot wired to an empty series is still an iteration; it just produces nothing useful downstream. The count is the number of slots you wired, not the amount of data in them.
Edge Cases
| Situation | What happens |
|---|---|
| From equals To (linear) | One element. A valid loop of size 1 |
| Boolean with nothing ticked, or enum with no symbols chosen | Validation error |
| Any range, slot or limit rule on this page is broken | Validation error |
Every validation error is raised while you are building, before a run starts.
Further Reading
- Pseudo-Mathematics and Financial Charlatanism — Bailey, Borwein, López de Prado and Zhu, Notices of the AMS, May 2014. Why the best backtest in a large sweep is usually the luckiest one.
- The Probability of Backtest Overfitting — the same authors' framework for putting a number on that risk.
- "…and the Cross-Section of Expected Returns" — Harvey, Liu and Zhu, NBER Working Paper 20592. On raising the bar for significance once you count how many ideas were tested.
- Overfitting and data dredging — why a model that fits the past perfectly usually fits the future badly.