How to convert code from version #2 to version #4 of pine script? without repainting

Viewed 358

I have the code for a pine script program in version 2, but I need to convert this to version 4 so that it'll be the same version as the rest of my program.

//@version=2
study("GetTrendStrategy", overlay=true)
tim=input('50')
out1 = security(tickerid, tim, open)
out2 = security(tickerid, tim, close)
plot(out1,color=red)
plot(out2,color=green)

longCondition = crossover(security(tickerid, tim, close),security(tickerid, tim, open))
plotshape(longCondition ? open : na, title="Buy", style=shape.arrowup, location=location.belowbar, size=size.normal, text="B", transp=0, textcolor = white, color=blue, transp=0)

shortCondition = crossunder(security(tickerid, tim, close),security(tickerid, tim, open))
plotshape(shortCondition ? open : na, title="Sell", style=shape.arrowdown, location=location.abovebar, size=size.normal, text="S", transp=0, textcolor = white, color=red, transp=0)
1 Answers
//@version=4
study("GetTrendStrategy", overlay=true)
tim=input('50')
out1 = security(syminfo.tickerid, tim, open)
out2 = security(syminfo.tickerid, tim, close)
plot(out1,color=color.red)
plot(out2,color=color.green)

longCondition = crossover(security(syminfo.tickerid, tim, close),security(syminfo.tickerid, tim, open))
plotshape(longCondition ? open : na, title="Buy", style=shape.arrowup, location=location.belowbar, size=size.normal, text="B", textcolor = color.white, color=color.blue)

shortCondition = crossunder(security(syminfo.tickerid, tim, close),security(syminfo.tickerid, tim, open))
plotshape(shortCondition ? open : na, title="Sell", style=shape.arrowdown, location=location.abovebar, size=size.normal, text="S", textcolor = color.white, color=color.red)

If your indicator is repaitning, use following recommendations:

https://www.tradingview.com/chart/?solution=43000478429

https://www.pinecoders.com/faq_and_code/#how-to-avoid-repainting-when-using-the-security-function

Related