Search code examples
pine-scriptpine-script-v5

Why does it give the error Can't use 'plotshape' locally?


I don’t understand why the code doesn’t want to display data on the chart, when I looked at the documentation I didn’t find an error in my code P.S I am using version 5

if longCondition
    label.new(x = bar_index, y = high, text = "SHORT", color = color.red, textcolor = color.white, style = label.style_label_up, textalign = text.align_center)
    takeProfitLevelValue = high + (high - low) * takeProfitLevel
    plotshape(series=takeProfitLevelValue, title="Take Profit", location=location.belowbar, color=color.green, style=shape.labelup)

I tried to specify through lable.new but still gives an error


Solution

  • You cannot use the plotshape() in a local scope. You should move that to the global scope.

    If you want to use the plotshape() conditionally, just add your condition to the series argument with an and operator.

    if longCondition
        label.new(x = bar_index, y = high, text = "SHORT", color = color.red, textcolor = color.white, style = label.style_label_up, textalign = text.align_center)
        takeProfitLevelValue = high + (high - low) * takeProfitLevel
    
    plotshape(series=longCondition and takeProfitLevelValue, title="Take Profit", location=location.belowbar, color=color.green, style=shape.labelup)