user3848207
user3848207

Reputation: 4987

Return X weeks high in pine-script

To get the 52-week high of closing price, I use the following code in tradingview;

High52 = ta.highest(close, 52)

I would like to do the reverse and find out the X-week high of the mouse-selected close price.

How do I find X?

I want to find out whether the mouse-selected close price is a 52-week high or a 40-week high or a 20-week high. This is what I mean by X-week high.

This is the definition of 52-week high.

https://www.investopedia.com/terms/1/52weekhighlow.asp#:~:text=What%20Is%2052%2DWeek%20High,that%20equates%20to%20one%20year.

I am using pine-script v5

Upvotes: -1

Views: 1525

Answers (2)

Allart
Allart

Reputation: 928

I am not that familiar with pinescript but I think this might help you in the right direction:

highest = 0
highest_week = 0

// Loop trough each 52 weeks. Not sure if starting at 0 will work. Switch it to 1 to 53 if not.
for offset = 0 to 52

    // Check if the close value of X week is higher then the highest found so far (starting at 0). 
    // If so, set a new highest value and highest week value, to remember which week was the highest.
    // You should use `highest_week := offset + 1` if you dont want week numbers to start at 0.
    if ta.highest(close, offset) > highest
        highest := ta.highest(close, offset)
        highest_week := offset

// After this for loop, highest_week should be the highest week found.

I'd say you change it to the point you would need it. I think with this example you can do it but then again, I have limited knowledge in this. If this ain't it then idk.

Upvotes: 2

evilmandarine
evilmandarine

Reputation: 4563

It is not clear what the result should be if the time frame is not 1 week, because in this case x-week high does not have a meaning. For example for a 1 day time frame, latest close could be higher than one week before, or 2 or more weeks, but in between daily closes could be higher than latest close.

Here's a solution that works if you set the time frame to 1 week ("W") in the main graph. There is an offset parameter that allows to shift the starting point to demonstrate that it works for any point. A loop is required, because as per the documentation:

Looking back in history to analyze bars using a reference value that can only be known on the current bar, e.g., to find how many past highs are higher than the high of the current bar. Since the current bar’s high is only known on the bar the script is running on, a loop is necessary to go back in time and analyze past bars.

Script:

//@version=5
indicator("x-week high", overlay = true)

startOffsetInBars = 0
i         = startOffsetInBars + 1
last      = close[startOffsetInBars]
lastClose = close[i]

if barstate.islast
    while last > lastClose
        i += 1
        lastClose := close[i]

    // days * 10, weeks * 7
    weeks = (time_close[startOffsetInBars] - time_close[i - 1])/((360000 * 24) * 10 * 7)

    lbl1 = label.new(
         x=bar_index[startOffsetInBars],
         y=close[startOffsetInBars],
         text=str.tostring(str.tostring(weeks) + "-week high"),
         textcolor=color.black,
         style=label.style_label_upper_left,
         size=size.small,
         color=color.lime)

    lbl2 = label.new(
         x=bar_index[i], 
         y=close[i],
         text=str.tostring("higher"),
         textcolor=color.black,
         style=label.style_label_upper_left,
         size=size.small,
         color=color.orange)

Results:

enter image description here

enter image description here

Upvotes: 1

Related Questions