fold 手册给出了一个例子:
input price = close;
input length = 9;
plot SMA = (fold n = 0 to length with s do s + getValue(price, n, length - 1)) / lenth;
这有效地迭代地调用函数,就像在 for 循环体中一样。
当我使用此语句调用我自己的函数时,它会中断,因为循环索引变量未被识别为可以传递给我的函数的变量:
script getItem{
input index = 0;
plot output = index * index;
}
script test{
def total = fold index = 0 to 10 with accumulator = 0 do
accumulator + getItem(index);########## Error: No such variable: index
}