我想在最后的最高价和最低的最低价之间绘制一条线,当我使用value来获取其值时,但是由于我不知道的原因,它给了我先前的值,这意味着这条线总要晚点蜡烛。
//@version=4
study("My Script", overlay = true)
upper = highest(high, 20)
lower = lowest(low, 20)
plot(upper)
plot(lower)
val_u = valuewhen(upper > upper[1], upper, 0)
val_l = valuewhen(lower < lower[1], lower, 0)
plot(val_u, color = color.red)
plot(val_l, color = color.red)
barss_u = barssince(upper > upper[1])
barss_l = barssince(lower < lower[1])
l = line.new(bar_index[barss_u], val_u, bar_index[barss_l], val_l, width = 1, color = color.orange, style=line.style_dashed)
line.delete(l[1])
这是它的外观,红色线是值,蓝色线是高/低电平,橙色线应该是最后一个较低的低点和最后一个较高的高点之间的连接线,但是由于我不知道的原因,则该值何时不会实时更新。
答案 0 :(得分:2)
这是valuewhen
的工作方式,它没有考虑当前的限制。尝试使用highestbars
和lowestbars
:
//@version=4
study("My Script", overlay = true)
barss_u = highestbars(20) * -1
barss_l = lowestbars(20) * -1
val_u = high[barss_u]
val_l = low[barss_l]
l = line.new(bar_index[barss_u], val_u, bar_index[barss_l], val_l, width = 1, color = color.orange, style=line.style_dashed)
line.delete(l[1])
更新
您可以使用以下代码修复valuewhen
的滞后:
//@version=4
study("My Script", overlay = true)
upper = highest(high, 20)
lower = lowest(low, 20)
plot(upper)
plot(lower)
x_val_u = valuewhen(upper > upper[1], upper, 0)
x_val_l = valuewhen(lower < lower[1], lower, 0)
val_u = high >= upper ? high : x_val_u
val_l = low <= lower ? low : x_val_l
plot(val_u, color = color.red)
plot(val_l, color = color.red)
barss_u = barssince(upper > upper[1])
barss_l = barssince(lower < lower[1])
l = line.new(bar_index[barss_u], val_u, bar_index[barss_l], val_l, width = 1, color = color.orange, style=line.style_dashed)
line.delete(l[1])
绿线显示此新版本的工作方式: