I am trying to find Highs iteratively, without using built in pivothigh/highest, because they are working by specifying a range of bars to look for. Instead, I am defining the starting bar, from which I search for bar that has previous bar lower, and next bar lower. It should detect this bar as Higher High. Then I will see the pips change between all found highs, and cull unwanted bars if they are outside some threshold.
currently I am getting bars after the highest (marked in blue crosses), but I need the ones marked with red dots.
How can I fix that?
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("22 Feb 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
// Iterate from start bar to the newest bar
for i = bars_since_new_index to 0
// Check for higher high
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
hh := i+1
else
hh := na
plotshape(hh)
I think the problem is in your loop which execute on each bar and then give you the false impression of working.
I propose you to execute the loop only once all the bar are drawn and I used label.new instead of plotshape because you can use label in loop.
Here is the corrected code :
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("14 Mar 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
if barstate.islastconfirmedhistory
// Iterate from start bar to the newest bar
for i = bars_since_new_index to 0
// Check for higher high
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
label.new(bar_index - i - 1, close, "x", yloc=yloc.abovebar, style=label.style_none)