I am trying to modify the following code to get the exact bar index and price of that bar index.
This works in the original code while using line.new() but when i use plot i am getting the correct price but the bar index is wrong or offseted.
Why it is happening like this? How to solve this?
What I look for: To get the latest zig zag swing point price and bar index values.
Original Code:
//@version=5
indicator("ZZ", overlay=true, max_lines_count=500, max_bars_back=4900)
settings = "Settings"
zigzag_len = input.int(9, "ZigZag Length", group=settings)
show_zigzag = input.bool(false, "Show Zigzag", group=settings)
fib_factor = input.float(0.33, "Factor", 0, 1, 0.01, group=settings)
var float[] high_points_arr = array.new_float(5)
var int[] high_index_arr = array.new_int(5)
var float[] low_points_arr = array.new_float(5)
var int[] low_index_arr = array.new_int(5)
to_up = high >= ta.highest(zigzag_len)
to_down = low <= ta.lowest(zigzag_len)
trend = 1
trend := nz(trend[1], 1)
trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend
last_trend_up_since = ta.barssince(to_up[1])
low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1))
low_index = bar_index - ta.barssince(low_val == low)
last_trend_down_since = ta.barssince(to_down[1])
high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1))
high_index = bar_index - ta.barssince(high_val == high)
if ta.change(trend) != 0
if trend == 1
array.push(low_points_arr, low_val)
array.push(low_index_arr, low_index)
if trend == -1
array.push(high_points_arr, high_val)
array.push(high_index_arr, high_index)
f_get_high(ind) =>
[array.get(high_points_arr, array.size(high_points_arr) - 1 - ind), array.get(high_index_arr, array.size(high_index_arr) - 1 - ind)]
f_get_low(ind) =>
[array.get(low_points_arr, array.size(low_points_arr) - 1 - ind), array.get(low_index_arr, array.size(low_index_arr) - 1 - ind)]
[h0, h0i] = f_get_high(0)
[h1, h1i] = f_get_high(1)
[l0, l0i] = f_get_low(0)
[l1, l1i] = f_get_low(1)
if ta.change(trend) != 0 and show_zigzag
if trend == 1
line.new(h0i, h0, l0i, l0)
if trend == -1
line.new(l0i, l0, h0i, h0)
market = 1
market := nz(market[1], 1)
last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0)
last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0)
market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0 < l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 + math.abs(h1 - l0) * fib_factor ? 1 : market
if ta.change(market) != 0
if market == 1
line.new(h1i, h1, h0i, h1, color=color.green, width=2)
if market == -1
line.new(l1i, l1, l0i, l1, color=color.red, width=2)
Code to get the same using plot, but getting a wrong bar index:
bullCondition = ta.change(market) != 0 and market == 1
bearCondition = ta.change(market) != 0 and market == -1
var float latest = na
if (bullCondition)
latest := h1
else if (bearCondition)
latest := l1
plot(latest, color=color.yellow)
Image:
You cannot do that.
The zigzag indicator is a repainting one. And it is dynamic. It will do some calculations and find the x1
of the line. Each time the difference between the current bar and x1
might be different.
plot()
is a continuous line. It has an offset
argument but it is applied to the whole series. You cannot go back and modify just a few points after it is drawn.