'Highest high "of the last n days", not "n days ago"
If I want to compare the close of today with the high of the last 4 days, what is the format? I have used
_hi = close > high[4] ? 1 : 0
But that only counts the high 4 days ago, not the days in between. I have tried
_hi = close > high[1,2,3,4] ? 1 : 0
Error message
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | anar khalilov |