I'm not sure if I understood what you wanted to do. Is this the result you wanted?
You can paint a picture of the result you want, then it may be easier for us to understand.
My script first checks where the pivot points are. The pivot points are displayed with "H" and "L".
It takes the most
recent pivot point and draws a line to the
right corner.
If a new pivot point appears. Then it stops drawing the previous line and start a new line.
First tried to use
hline, but is not possible to change the start and stop. I used a normal
line, where it is possible.
Here is the code:
Code: Select all
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CanYouCatchMe
//@version=4
study("horizontal pivot lines", overlay=true)
prd = input(defval = 20, title="Pivot Point Period", minval = 10, maxval = 50) //How many pivot points. Lower value results in more points.
float ph = na, float pl = na //If there is no pivit points. Default value
ph := pivothigh(prd, prd) //Returns the price of the pivot point "prd" ago. If there was no pivot "prd" ago, then it returns 'na'.
pl := pivotlow(prd, prd)
//Plotting lables. NOTE: offset = -prd, Because it gets the price "prd" ago.
plotshape(ph, text="H", style=shape.labeldown, color=color.new(color.white, transp = 0), textcolor=color.black, location=location.abovebar, offset = -prd)
plotshape(pl, text="L", style=shape.labeldown, color=color.new(color.white, transp = 0), textcolor=color.black, location=location.belowbar, offset = -prd)
//Saving the referance of the lines, so the the "x2" position can be moved the right.
var line l1 = na, var line l2 = na
//High lines
if(ph) //if there was a pivot point "prd" ago.
line.set_x2(l1, bar_index-prd) //Changing old line last time
l1 := line.new(x1=bar_index-prd, y1=ph, x2=bar_index, y2=ph) //New line added.
line.set_x2(l1, bar_index) //Moving "x2" to the right corner.
//Low lines
if(pl)
line.set_x2(l2, bar_index-prd)
l2 := line.new(x1=bar_index-prd, y1=pl, x2=bar_index, y2=pl, color=color.yellow)
line.set_x2(l2, bar_index)