Highest high “of the last n days”, not “n days ago”

眉间皱痕 提交于 2020-06-18 02:52:47

问题


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


回答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=green, size=size.small, transp=40)



来源:https://stackoverflow.com/questions/53074111/highest-high-of-the-last-n-days-not-n-days-ago

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!