Search code examples
pine-scriptforex

How to write variable with string in Pine Script?


I tried to write text with variable, but I can't

aaaa = 121.150
bbbb = 120.100
target = "TP = " + ( aaaa * 0.9 ) + "\n SL = " + bbbb*1.01
plotshape(data , style=shape.triangledown,location=location.abovebar, color=red, text=target)

I want the output looks like this:


Solution

  • To put series (dynamic) text on the chart you will need to use Pine v4 and label.new(). You will only see the last ~50 labels created:

    //@version=4
    study("", "", true)
    aaaa = 121.150
    bbbb = 120.100
    target = "TP = " + tostring(aaaa * 0.9) + "\n SL = " + tostring(bbbb*1.01)
    cond = rising(close, 1)[1] and falling(close, 1)
    if cond
        label.new(bar_index, high, target, yloc = yloc.abovebar, color = color.red, style = label.style_triangledown)
    

    enter image description here