首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >thinkscript if函数在重要情况下无用

thinkscript if函数在重要情况下无用
EN

Stack Overflow用户
提问于 2019-10-12 20:59:38
回答 2查看 458关注 0票数 0

在一个重要的案例中,thinkscript if function无法按预期进行分支。下面的测试用例可以用来重现这个严重的bug /缺陷。

简而言之,if语句通常可用于在函数参数之一无效的情况下阻止执行函数调用。我们证明情况并非如此。实际上,两个分支都会被执行,包括不满足if条件的分支。

这完全违背了if条件测试的目的,即每种语言中每条if语句都有的测试。

以下是在图表上显示问题的一些示例代码。单击图表左上角闪烁的"i“消息图标即可查看结果:

折叠:'from‘不能大于'to':1> -1。

代码语言:javascript
复制
# 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;
EN

回答 2

Stack Overflow用户

发布于 2019-10-12 21:00:40

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

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

thinkscript if statement failure

票数 0
EN

Stack Overflow用户

发布于 2021-04-02 11:18:55

至少在2021年4月,documentation for the if reserved word说:

...if表达式总是计算和else分支,而if语句只计算由条件为真还是为假定义的分支。

(黑体字和斜体字)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58354407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档