How can I calculate and display a single highest high and lowest low within a predefined interval in Pine Script?

I am trying to calculate and display a single highest high and lowest low within predefined intervals (e.g., every 20 candles) in Pine Script v6. The goal is to integrate this logic into a Supertrend indicator to accurately identify the highest and lowest points of each trend.

While I can isolate entry prices based on Supertrend transitions, calculating the percentage fluctuation between the highest and lowest points of each trend doesn’t work as expected. The challenge is to correctly identify and limit these high/low points to just one per interval or trend segment without encountering syntax errors or invalid assignments in Pine Script.

here is my actual code :

indicator("Pivot High Low Points with Vertical Markers", overlay=true)
 
// User Settings
lb = input.int(5, title="Left Bars")
rb = input.int(5, title="Right Bars")
 
mb = lb + rb + 1
 
// Calculating the high and low pivots
pivot_high = ta.highestbars(high, lb + rb + 1) == -lb ? high[lb] : na
pivot_low = ta.lowestbars(low, lb + rb + 1) == -lb ? low[lb] : na
 
// Displaying top and bottom pivots
plotshape(pivot_high, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny, offset=-lb)
plotshape(pivot_low, style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.tiny, offset=-lb)
 
// Add a label with the price for each high and low pivot
if not na(pivot_high)
    label.new(bar_index[lb], pivot_high, text=str.tostring(pivot_high, "#.##"), style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small, yloc=yloc.abovebar)
 
if not na(pivot_low)
    label.new(bar_index[lb], pivot_low, text=str.tostring(pivot_low, "#.##"), style=label.style_label_up, color=color.lime, textcolor=color.white, size=size.small, yloc=yloc.belowbar)
 
// Setting for adding vertical bars every 20 candles
vertical_interval = input.int(20, title="Vertical Marker Interval", minval=1)
 
// Add a vertical bar every 'vertical_interval' candles
if bar_index % vertical_interval == 0
    line.new(x1=bar_index, y1=low - 10, x2=bar_index, y2=high + 10, width=1, color=color.blue, style=line.style_dashed)```
 
 
i already tryed to use differents approach but none of them work : 
 
Failed Attempts:
 
Attempt with := for variable assignment: When trying to assign values to interval_high and interval_low using := in this code:
 
`if bar_index % vertical_interval == 0
    interval_start_bar := bar_index
    [interval_high, interval_low] := get_interval_pivots(interval_start_bar)`

The error occurred: "Syntax error at input ':='." This issue arose because Pine Script does not allow tuple assignment like this.
 
Attempt with get_interval_pivots function: The idea was to calculate the highest and lowest points within the interval of 20 candles using a custom function:
 
`get_interval_pivots(start_index) =>
    var float highest = na
    var float lowest = na
    for i = start_index to start_index + vertical_interval - 1
        highest := na(highest) ? high[i] : math.max(highest, high[i])
        lowest := na(lowest) ? low[i] : math.min(lowest, low[i])
    [highest, lowest]`
 
Then, using the following code:
 
`if bar_index % vertical_interval == 0
    interval_start_bar := bar_index
    [interval_high, interval_low] := get_interval_pivots(interval_start_bar)`
 
This also failed, as Pine Script does not allow tuple assignment in this manner, leading to the error.

New contributor

user28885078 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Of course this is too much to handle for ChatGPT..

You can just use some flags.

You need two variables from the Supertrend. One for when a new trend starts, and one when you are in a trend.

bool is_new_uptrend = false
bool is_in_uptrend = false

Once you figure out how to get set those variables, just use a var variable to keep track of the high or low of the trend.

var float up_trend_high = na

if (is_new_uptrend)
    up_trend_high := high
else if (is_in_uptrend )
    if (high > up_trend_high)
        up_trend_high := high

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật