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;