user28885078
user28885078

Reputation: 11

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.

Upvotes: 1

Views: 54

Answers (1)

vitruvius
vitruvius

Reputation: 21342

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

Upvotes: 0

Related Questions