Reputation: 141
I would like to display the weekly/monthly/yearly highs and lows for the current and previous week/month/year on any timeframe. As an example, if I'm on the 4h chart, I would like to see an indication of the the current week's high/low as well as an indication of the previous week's high/low. The same for the current and previous month/year. The indications should only occur at the bar on which the high/low was formed.
I wrote something like this (only for the weekly high/low; the others would be implemented similarly), but it shows indications on every bar (for the 4h chart for example):
//@version=5
indicator(title="test", shorttitle="test", overlay=true)
highs = request.security(syminfo.tickerid, "W", high)
lows = request.security(syminfo.tickerid, "W", low)
plotchar(ta.valuewhen(high == highs[0], high, 1), "current_high", "▼", location.abovebar, size = size.tiny, color = color.lime)
plotchar(ta.valuewhen(low == lows[0], low, 1), "current_low", "▲", location.belowbar, size = size.tiny, color = color.blue)
plotchar(ta.valuewhen(high == high[1], high, 1), "previous_high", "▼", location.abovebar, size = size.tiny, color = color.teal)
plotchar(ta.valuewhen(low == lows[1], low, 1), "previous_low", "▲", location.belowbar, size = size.tiny, color = color.maroon)
Bonus question: how do I indicate the highs/lows using style_label_down/style_label_up because labels require x,y coordinates as input?
Upvotes: 0
Views: 1142
Reputation: 2161
You can check if it's a new week using ta.change()
function, and then use label
to show the results you wish for only when a new week begins or if it's the last bar. For example:
//@version=5
indicator(title="test", shorttitle="test", overlay=true)
isNewWeek = ta.change(time("W"))
highs = request.security(syminfo.tickerid, "W", high)
lows = request.security(syminfo.tickerid, "W", low)
if isNewWeek or barstate.islast
label.new(bar_index, high,
text="Current High " + str.tostring(highs) +
"\nCurrent Low " + str.tostring(lows) +
"\nPrevios High " + str.tostring(highs[1]) +
"\nPrevious Low " + str.tostring(lows[1]),
style=label.style_label_down)
** Notice the line breaks if you are copy-pasting the code.
Upvotes: 1