how to automatic plot targets on tradingview

Viewed 205

I'm trying automatic plot targets on tradingview, like this image

enter image description here,

but I failed, the TP will Recurrence several times.

enter image description here

Can anyone help me?


plotshape(  (close>TP1_value)  , title="TP1_Reached", style=shape.arrowup, location=location.belowbar, color=green, transp=0, text="TP1_Reached", textcolor=green, size=size.large)
1 Answers

Your problem is, close>TP1_value is true for multiple bars. If you want to plot only once, you can use a variable.

//@version=4
study("My Script", overlay=true)

TP1_value = input(title="Take Profit Price", defval=10000)

var isTP1reached = false  // Use this variable to see if TP1 hass already been reached

takeProfit1 = not isTP1reached and (close > TP1_value)  // If not already reached TP1 and TP condition is true

if (takeProfit1)
    isTP1reached := true

plotshape(series=takeProfit1, title="Take Profit 1", text="Take Profit 1", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

enter image description here

As you can see from the image above, it only plots once. Of course you need to reset the variable according to your needs in order to use it again.

Related