在重要情况下函数无用的thinkscript

时间:2019-10-12 12:59:38

标签: thinkscript

在重要情况下,thinkscript if function无法分支。以下测试案例可用于重现此严重的错误/缺陷。

简而言之,如果if语句的函数参数之一无效,则通常可以使用if语句来阻止执行该函数调用。我们证明事实并非如此。实际上,两个分支都被执行,包括不满足if条件的分支。

这绝对违背了if条件测试的目的,即每种语言中每个if语句都具有的测试。

以下是一些示例代码,它们在图表上显示了问题。通过单击图表左上角闪烁的“ i”消息图标,可以看到结果:

折叠:“ from”不能大于“ to”:1> -1。

# Get the current offset from the right edge from BarNumber()
# BarNumber(): The current bar number. On a chart, we can see that the number increases
# from left 1 to number of bars e.g. 140 at the right edge.
def barNumber = BarNumber();
def barCount = HighestAll(barNumber);
# rightOffset: 0 at the right edge, i.e. at the rightmost bar,
# increasing from right to left.
def rightOffset = barCount - barNumber;

# This script gets the minimum value from data in the offset range between startIndex
# and endIndex. It serves as a functional but not direct replacement for the
# GetMinValueOffset function where a dynamic range is required. Expect it to be slow.
script getMinValueBetween {
    input data = low;
    input startIndex = 0;
    input endIndex = 0;
    plot minValue = fold index = startIndex to endIndex with minRunning = Double.POSITIVE_INFINITY do Min(GetValue(data, index), minRunning);
}

# Call this only once at the last bar.
script buildConditions {
    input startIndex = 1;
    input endIndex = -1;
# Since endIndex < startIndex, getMinValueBetween() should never
# be executed. However it is executed nevertheless.
    plot minValue = if (endIndex > startIndex) then getMinValueBetween(low, startIndex, endIndex) else close[startIndex];
}
plot scan;
if (rightOffset == 0) {
    scan = buildConditions();
} else {
    scan = 0;
}
declare lower;

2 个答案:

答案 0 :(得分:0)

问题的第一句有答案。

可以考虑使用if语句(vs if函数)。但是,如

所示,它已被破坏。

thinkscript if statement failure

答案 1 :(得分:0)

至少截至 2021 年 4 月,documentation for the if reserved word 表示:

<块引用>

...虽然if 表达式总是计算两个 then 和 else 分支,但 if 语句只计算由条件是真还是假定义的分支.

(我的粗体和斜体)

绝对令人困惑和意外的行为!