图表多时间输入高亮异常:第二个time_selection未显示问题排查
问题原因分析
你的代码逻辑存在覆盖赋值的问题,导致时间选择项的高亮逻辑相互冲突:
代码中两个独立的
if-else块是顺序执行的,后续的分支会覆盖前面的赋值结果:- 当当前K线时间等于
time_selection_1时,第一个if会把highlight_date设为true,但紧接着第二个if-else因条件不满足(时间不等于time_selection_2),会执行else分支将highlight_date重新设为false,最终不会高亮。 - 当当前K线时间等于
time_selection_2时,第一个if-else先把highlight_date设为false,第二个if再将其设为true,此时才会高亮。 - 你看到
time_selection_1能高亮,大概率是测试时的特殊场景导致(比如两个时间设置重复),但本质逻辑是错误的。
- 当当前K线时间等于
正确的写法应该是合并判断条件,避免后续分支覆盖正确赋值:
time_selection_1 = input.time(timestamp("Jun 01 2023 10:00:00 UTC+3"), 'Date/Time') time_selection_2 = input.time(timestamp("Jun 02 2023 10:00:00 UTC+3"), 'Date/Time') var bool highlight_date = na // 合并两个条件,满足任意一个时间点就高亮 highlight_date := time == time_selection_1 or time == time_selection_2
或者保留if结构的写法:
time_selection_1 = input.time(timestamp("Jun 01 2023 10:00:00 UTC+3"), 'Date/Time') time_selection_2 = input.time(timestamp("Jun 02 2023 10:00:00 UTC+3"), 'Date/Time') var bool highlight_date = na if time == time_selection_1 or time == time_selection_2 highlight_date := true else highlight_date := false
这样就能保证所有选中的时间点都能被正确高亮。
内容的提问来源于stack exchange,提问作者x945
相关产品推荐
相关产品推荐

