twobeers
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: February 2nd, 2023

Pivot Indicator/Strategy

Hey together,

I need help with the following code. So I want to draw a horizontal line from every Pivot low or high until the price breaks through this levels then the line should be interrupted.

I've two problems with the following code:
1. It just draws horizontal lines from the last few pivot points and not from the historical ones.
2. The lines are not correctly aligned and partially point into the past.

hope somebody can help me. :zen:

Code: Select all

//@version=5
indicator("Fractals with horizontal lines", overlay=true)

//detecting the pivot highs and lows
ph = ta.pivothigh(3, 3)
pl = ta.pivotlow(3, 3)

//Indicates at which candle a pivot high was created
when_occure_high = ta.valuewhen(ph, bar_index, 0)

//Indicates at which candle a pivot low was created
when_occure_low = ta.valuewhen(pl, bar_index, 0)

//Indicates the price of this pivot high
high_value = ta.valuewhen(ph, high[3], 0)

//Indicates the price of this pivot low
low_value = ta.valuewhen(pl, low[3], 0)

//should return the values x2 (horizontal endpoint) for the line
price_cross_high    = int(ta.valuewhen(ta.cross(high_value, high), bar_index, 0))
price_cross_low     = int(ta.valuewhen(ta.cross(low_value, low), bar_index, 0))

//A horizontal line is drawn for each pivot high and pivot low
line_high = line.new(when_occure_high-3, high_value, price_cross_high, high_value, color=color.red)
line_low = line.new(when_occure_low-3, low_value, price_cross_low, low_value, color=color.green)

plotshape(pl, style=shape.triangleup, color=color.green, offset=-3, location=location.belowbar)
plotshape(ph, style=shape.triangledown, color=color.red, offset=-3, location=location.abovebar)

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Pivot Indicator/Strategy

Hey twobeers,

This one is easy. All you need to do is place the following property in your indicator statement

max_lines_count=500 or whatever value you want but 500 is the max. It doesn't seem to only count lines but possibly other objects because I tried 100 and it only displayed a dozen or so lines.

Good fortune!

twobeers
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: February 2nd, 2023

Re: Pivot Indicator/Strategy

Hey Steve,

thank you very much for your help!
That works fine so far. :up:

I wanted to interrupt the lines from past fractal highs or lows if the future price (or current price) crosses. I tried it with this section to detect if the price crosses the fractal (but it doesn't work correctly):

Code: Select all

//should return the values x2 (horizontal endpoint) for the line
price_cross_high    = int(ta.valuewhen(ta.cross(high_value, high), bar_index, 0))
price_cross_low     = int(ta.valuewhen(ta.cross(low_value, low), bar_index, 0))

Here is an example of what I mean:

Image

Maybe this is just as easy to solve but I don't get it. :frustrated:

Thank you in advance and kind regards from Cologne, Germany.

Tobias :happy3:

Return to “Pine Script Q&A”