0

thinkscript 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;

4

2 回答 2

0

这个问题的第一句话就有答案。

人们可能会考虑使用 if 语句(与 if 函数相比)。然而,这被打破了,如

thinkscript if 语句失败

于 2019-10-12T13:00:39.507 回答
0

至少截至 2021 年 4 月,保留字的文档if说:

...虽然if 表达式总是计算thenelse 分支,但 if 语句只计算由条件是真还是假定义的分支。

(粗体和斜体我的)

绝对令人困惑和意想不到的行为!

于 2021-04-02T03:18:55.203 回答