Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Conditionally draw Horiz line with either hline() or plot()

From pinescript I want to draw a horiz line only if a condition is true. I failed with both hline and plot

If I try to conditionally change the plot color to na then the plot is still drawn, but is invisible. Not optimal since this causes the chart to autoscale to include the invisible plot.

If I try to conditionally change the hline price to na, this causes a compile error

Help appreciated, thanks
4 people have
this question
+1
Reply
  • Here is code that shows a method to plot a hline when a condition is true. I am definitely a beginner Pine coder, and there are probably better ways. So far not my favorite language.

    //@version=2
    study("SRAlert", overlay=true)

    // Create series vars marking six SR prices for each pair
    plotHLines = input(title="plotHLines", type=bool, defval=false)
    hlineStyle = solid
    hlineWidth = 1
    hlineColor = black

    // must initially set hline prices as float
    hlinePrice1 = 1.23456
    hlinePrice2 = 1.23456
    hlinePrice3 = 1.23456
    hlinePrice4 = 1.23456
    hlinePrice5 = 1.23456
    hlinePrice6 = 1.23456

    // now can reset hline prices to na (which do not plot)
    hlinePrice1 := na
    hlinePrice2 := na
    hlinePrice3 := na
    hlinePrice4 := na
    hlinePrice5 := na
    hlinePrice6 := na

    // if condition is true then the hline plots
    // --------------------------------
    if ( ticker == "AUDCHF" and plotHLines )
    hlinePrice1 := .749
    hlinePrice2 := .748
    hlinePrice3 := .747
    hlinePrice4 := .746
    hlinePrice5 := .745
    hlinePrice5 := .740

    if ( ticker == "AUDUSD" and plotHLines )
    hlinePrice1 := .78087
    hlinePrice2 := .76807
    hlinePrice3 := .74831
    hlinePrice4 := .73340
    hlinePrice5 := .71762
    hlinePrice6 := .70227

    plot(hlinePrice1, 'hline1',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
    plot(hlinePrice2, 'hline2',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
    plot(hlinePrice3, 'hline3',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
    plot(hlinePrice4, 'hline4',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
    plot(hlinePrice5, 'hline5',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
    plot(hlinePrice6, 'hline6',hlineColor,hlineWidth,hlineStyle,transp=0,offset=0)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • I have a similar question.

    So I tried running this script, in order to change it to my specific need, but is gives an error just below this

    ***
    / if condition is true then the hline plots
    // --------------------------------
    if ( ticker == "AUDCHF" and plotHLines )
    hlinePrice1 := .749
    ***

    can anyone tell me why this stops here?

    the displayed error is: "mismatched input 'hlinePrice1' expecting 'end of line without line continuation'"

    Hope someone can elaborate...
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • I'm guessing "hlinePrice1" should be preceded by four spaces or a tab without seeing the remainder of the code, but I still couldn't get hline() to plot using the if() condition. hline() is finicky about ternary and if() conditionals. Consider using an input() for an adjustable hline until such time TV considers changing hline() limitations, which I'm not sure if this may ever occur. When creating if() statements or functions Pine doesn't use curly braces for "code blocking", instead it uses 1 tab or 4 spaces for future reference.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • 1
    I will continue to request hline() accepts a series until the end of time, but it is ultimately up to them. It's a good idea I condone. In theory it could work like plot(trackprice=true) with a series. You may try this to see if it is feasible. Reply if you find success. I was pondering using show_last==1 with trackprice. I do have that on the bottom of my to do list for Pine extreme code.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited