在一个重要的案例中,thinkscript if function无法按预期进行分支。下面的测试用例可以用来重现这个严重的bug /缺陷。
简而言之,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;发布于 2019-10-12 21:00:40
发布于 2021-04-02 11:18:55
至少在2021年4月,documentation for the if reserved word说:
...if表达式总是计算和else分支,而if语句只计算由条件为真还是为假定义的分支。
(黑体字和斜体字)
绝对是令人困惑和意想不到的行为!
https://stackoverflow.com/questions/58354407
复制相似问题