You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script分组布尔判断结果异常全为True问题求助

问题:布尔分组逻辑错误导致所有组返回true

我有一个首字符为数字的字符串型用户输入,通过str.substring()和str.tonumber()将其转换为float类型,绘图验证输入转换正确。随后将数字进行分组,期望用户选中的数字所在组为true,其他组为false,但所有组均返回true。

已验证数据类型(float与bool),数字绘图正确,但分组布尔结果不符合预期。例如选择i4时,期望仅group2和group3为true,其余为false。

原代码:

//@version=5
strategy("My strategy", overlay=true)

i1 = "1 - Option 1"
i2 = "2 - Option 2"
i3 = "3 - Option 3"
i4 = "4 - Option 4"
i5 = "5 - Option 5"
i6 = "6 - Option 6"
i7 = "7 - Option 7"
input = input.string(i3, title = "Make a choice", options = [i1, i2, i3, i4, i5, i6, i7])

// Get the number from the user input
inputNr = str.tonumber(str.substring(input, 0, 1))

// Test the input number and plot it on the chart
plot(inputNr * 1000, "Number")  // The number plots correct (so far so good)

// Make the numbers part of a group. 
// The group(s) containing the numbers should become true while other groups should become false, all based on the user input
group1 = inputNr == 5 or 6 or 7
group2 = inputNr == 1 or 2 or 3 or 4
group3 = inputNr == 3 or 4 or 5
group4 = inputNr == 6 or 7

// Test if a group is true by plotting a shape
// Here I get stuck... Every group, regardless of the user input gives a true
// My expected outcome was that if I sellect i4 from the list that only group2 & group3 would return true while others become false
plotshape(group1, location = location.top, style = shape.arrowdown) 

问题原因与修正

错误原因

布尔逻辑写法不规范。在Pine Script中,inputNr == 5 or 6 or 7等价于(inputNr ==5) or 6 or7,而非inputNr ==5 or inputNr ==6 or inputNr ==7。由于非零数字在布尔判断中会被视为true,所以无论inputNr取值是什么,整个表达式都会返回true,导致所有组永远为真。

修正后的代码

//@version=5
strategy("My strategy", overlay=true)

i1 = "1 - Option 1"
i2 = "2 - Option 2"
i3 = "3 - Option 3"
i4 = "4 - Option 4"
i5 = "5 - Option 5"
i6 = "6 - Option 6"
i7 = "7 - Option 7"
input = input.string(i3, title = "Make a choice", options = [i1, i2, i3, i4, i5, i6, i7])

// Get the number from the user input
inputNr = str.tonumber(str.substring(input, 0, 1))

// Test the input number and plot it on the chart
plot(inputNr * 1000, "Number")  // The number plots correct (so far so good)

// 修正后的分组逻辑:每个条件都完整判断inputNr与目标值的相等关系
group1 = inputNr == 5 or inputNr == 6 or inputNr == 7
group2 = inputNr == 1 or inputNr == 2 or inputNr == 3 or inputNr == 4
group3 = inputNr == 3 or inputNr == 4 or inputNr == 5
group4 = inputNr == 6 or inputNr == 7

// 测试分组结果,添加偏移量区分不同组的箭头
plotshape(group1, location = location.top, style = shape.arrowdown, color=color.red, title="Group1")
plotshape(group2, location = location.top, style = shape.arrowdown, color=color.green, title="Group2", offset=1)
plotshape(group3, location = location.top, style = shape.arrowdown, color=color.blue, title="Group3", offset=2)
plotshape(group4, location = location.top, style = shape.arrowdown, color=color.orange, title="Group4", offset=3)

效果说明

修正后每个分组条件都明确判断inputNr是否等于对应数字,只有当inputNr属于组内的某个数字时,该组才会返回true。例如选择i4(对应数字4)时,group2和group3会返回true,其余组为false,符合预期。

内容的提问来源于stack exchange,提问作者user18767111

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 03:55:23