Search code examples
pivotpine-scripthorizontal-line

Horizontal line on chart on TradingView


I'm using charts on TradingView and I'd like to draw horizontal lines. The horizontal lines are the Pivot Points.

I've calculated them and each values is stocked in a variable.

width = input(2, minval=1)
xHigh  = security(tickerid,"D", high[1])
xLow   = security(tickerid,"D", low[1])
xClose = security(tickerid,"D", close[1])
vPP = (xHigh+xLow+xClose) / 3
vR1 = vPP+(vPP-xLow)
vS1 = vPP-(xHigh - vPP)
vR2 = vPP + (xHigh - xLow)
vS2 = vPP - (xHigh - xLow)
vR3 = xHigh + 2 * (vPP - xLow) 
vS3 = xLow - 2 * (xHigh - vPP)

I've tried to use this line to do the job

plot(vPP, color=black, title="vPP", style = line, linewidth = width)

But from one day to another, the line doesn't cut. So it's not looking good. See the picture. enter image description here

This is the result I am looking for :

enter image description here

I'd like :

  • to display today and yesterday pivot points.
  • that the lines starts from today until the end of the session
  • to write "PP, S1/S2/S3, R1/R2/R3" in front of the lines

Thanks for your advises


Solution

  • To remove the connecting line, you have to use color na when the value changes.
    For a code example, see the answer of PineCoders-LucF to one of my questions Plotting manual levels for daily high,low,close

    Edit: Your code example, modified to work as you intended.

    //@version=4
    study("My Script", overlay=true)
    
    width = input(2, minval=1)
    xHigh  = security(syminfo.ticker,"D", high[1])
    xLow   = security(syminfo.ticker,"D", low[1])
    xClose = security(syminfo.ticker,"D", close[1])
    
    vPP = (xHigh+xLow+xClose) / 3
    vR1 = vPP+(vPP-xLow)
    vS1 = vPP-(xHigh - vPP)
    vR2 = vPP + (xHigh - xLow)
    vS2 = vPP - (xHigh - xLow)
    vR3 = xHigh + 2 * (vPP - xLow) 
    vS3 = xLow - 2 * (xHigh - vPP)
    
    plot(vPP, color=change(vPP) ? na : color.black, title="vPP", style = plot.style_linebr, linewidth = width)
    

    As requested in the comments, code for @version=3.
    Remark: You should really use @version=4 to access the latest Pine script capabilities.

    //@version=3
    study("My Script", overlay=true)
    
    width = input(2, minval=1)
    xHigh  = security(tickerid,"D", high[1])
    xLow   = security(tickerid,"D", low[1])
    xClose = security(tickerid,"D", close[1])
    
    vPP = (xHigh+xLow+xClose) / 3
    vR1 = vPP+(vPP-xLow)
    vS1 = vPP-(xHigh - vPP)
    vR2 = vPP + (xHigh - xLow)
    vS2 = vPP - (xHigh - xLow)
    vR3 = xHigh + 2 * (vPP - xLow) 
    vS3 = xLow - 2 * (xHigh - vPP)
    
    plot(vPP, color=change(vPP) ? na : black, title="vPP", style = linebr, linewidth = width)