Daniel
Daniel

Reputation: 51

Extending lines on pine-script

I've got this code that as it is now, is connecting the pivots high and the low ones but I'd like the lines to extend to create trend lines and to extend horizontally to create Support/Resistance lines as well. In the image below you can see the blue lines that the script is now creating and the red ones that i created manually to explain what I'd like it to do.

Continuos red lines for trends and dashed for S/R

I tried using line.set_extend but this code extends the lines forever creating a very confusing chart. I'd like to be able to set a maximum length the lines can extend or even better, a maximum of trend lines that can be displayed at a single time. Unluckily this goes beyond my coding capabilities.

Hopefully some of you has the skills to help me.

Thank you :)

//@version=4

study("PivotsExt", overlay=true)

Barsleft = input(10)
Barsright = input(10)

ph = pivothigh(Barsleft, Barsright)
pl = pivotlow( Barsleft, Barsright)

ph_ext = ph 
pl_ext = pl 

if (ph)
    ph_ext := ph

if (pl)
    pl_ext := pl

plot(ph_ext, color= color.blue, offset=-Barsright)
plot(pl_ext, color= color.red, offset=-Barsright)

Upvotes: 1

Views: 5010

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

Here we declare your 2 variables holding the pivot levels on the first bar only using var, which makes their value persistent across bars. We also adapt the plot() calls so they don't plot a diagonal line when levels change:

//@version=4

study("PivotsExt", overlay=true)

Barsleft = input(10)
Barsright = input(10)

ph = pivothigh(Barsleft, Barsright)
pl = pivotlow( Barsleft, Barsright)

var ph_ext = ph 
var pl_ext = pl 

if (ph)
    ph_ext := ph

if (pl)
    pl_ext := pl

// plot(ph_ext, color= color.blue, offset=-Barsright, style = plot.style_circles)
plot(ph_ext, color= change(ph_ext) ? na : color.blue, offset=-Barsright)
plot(pl_ext, color= change(pl_ext) ? na : color.red, offset=-Barsright)

enter image description here

Upvotes: 2

Related Questions