Control Flow
Because Kafal operates on a high-performance vectorized runtime, it makes a strict distinction between scalar routing and time-series evaluation.
If / Else Blocks
Block-level if and else statements are designed for scalar evaluation. The condition must resolve to a single boolean value, not a time-series array.
// Standard block routing based on scalar variables or inputs
if strategy_mode == "conservative" {
trade.long(crossover(fast, slow), size=0.5)
} elseif strategy_mode == "aggressive" {
trade.long(crossover(fast, slow), size=2.0)
} else {
trade.long(crossover(fast, slow), size=1.0)
}Vectorized Conditionals
To perform conditional assignments across historical bars, you must use vectorized conditionals like where() or the ternary syntax ? :.
// This will throw KAFAL-BLOCK-IF-SERIES:
// if close > open { candle_color = "green" }
// Correct: Vectorized Conditional (Ternary)
candle_color = close > open ? "green" : "red"
// Correct: Vectorized Conditional (Function)
candle_color = where(close > open, "green", "red")For Loops
Loops can iterate over a specific inclusive range (start..end) or an existing array structure. To prevent infinite loops, iterations are strictly capped at 100,000 items.
// Range-based loop
sum_val = 0
for i in 0..10 {
sum_val = sum_val + close[i]
}
// Iterable-based loop over an array
my_arr = array()
array_push(my_arr, 10)
for x in my_arr {
plot(x) - title="Array Value"
}