You can use highest()
for that purpose.
highest(source, length) → series
You need to be careful though. close > highest(close, 4)
can never be true
. Because, if the current bar's close price is the highest among those 4 bars, highest()
will return current bar's close price. Therefore, that check would be close > close
, which can never be true.
You can either do close > highest(nz(close[1]), 4)
or close == highest(close, 5)
(It is 5
, because current bar is also included. But you want to compare the previous 4 bars).
Have a look at the following code and chart. One is using close > highest(nz(close[1]), 4)
and the other one is using close == highest(close, 5)
. As you can see, the output is identical.
//@version=3
study(title="Compare 2", overlay=true)
_hi = close > highest(nz(close[1]), 4)
plotshape(series=_hi, title="_hi", text="hi", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, transp=40)
