Reputation: 646
I want to plot a line between the last highest high and the last lowest low, I used valuewhen to get the value for that, but for a reason that I don't know, it gives me the previous value, that means the line would be always late with a candle.
//@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])
This is how it looks, red line is the valuewhen, blue line is the high/low level, orange line should be the connection line between last lower low and last higher high, but for a reason that I don't know, the valuewhen does not update in real time.
Upvotes: 0
Views: 1240
Reputation: 416
This is how valuewhen
works, it does not take the current bar into account. Try highestbars
and lowestbars
instead:
//@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])
UPDATE
You can fix the lag of valuewhen
with the following code:
//@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])
The green line shows how this new version works:
Upvotes: 2