Pine Script has many versions, and they are not very compatible with each other, making it difficult to switch from one version to another. You might experience quite a lot of issues when you try to copy part of the code from a script in another version or merge different version scripts.
My universal advice is to use transform everything to the last (v5 currently) version of Pine Script and then merge your code. Upgrading code for a newer version of your code was a headache in older versions, but from v3, it became straightforward. If you have a v3 or v4 pine script indicator, you can upgrade it automatically.
Let’s take, for example pretty old script I have in V3 of Pine Script:
//@version=3
strategy("RSI Strategy", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
// Inputs
length = input(65)
overSold = input(40)
overBought = input(60)
price = input(close)
// RSI
vrsi = rsi(price, length)
if (crossover(vrsi, overSold))
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
if (crossunder(vrsi, overBought))
strategy.entry("RsiSE", strategy.short, comment="RsiSE")
Now you don’t need to translate your script manually; you can click three dots in the top right corner of your screen and click “Convert to v4”:
After that TradingView goes through all your code and rename all needed functions and build its variables automatically, and you’ll see a v4 code in your editor:
//@version=4
strategy("RSI Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs
length = input(65)
overSold = input(40)
overBought = input(60)
price = input(close)
// RSI
vrsi = rsi(price, length)
if crossover(vrsi, overSold)
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
if crossunder(vrsi, overBought)
strategy.entry("RsiSE", strategy.short, comment="RsiSE")
When you have a v4 script in your editor, the same button will now say “Convert to v5,” and in addition, you’ll see a button “Back to v3” if you need to downgrade the version of your script to version 3 of Pine Script.
After clicking this button, TradingView will again go through your code and replace the v4 code with v5:
//@version=5
strategy('RSI Strategy', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs
length = input(65)
overSold = input(40)
overBought = input(60)
price = input(close)
// RSI
vrsi = ta.rsi(price, length)
if ta.crossover(vrsi, overSold)
strategy.entry('RsiLE', strategy.long, comment='RsiLE')
if ta.crossunder(vrsi, overBought)
strategy.entry('RsiSE', strategy.short, comment='RsiSE')
Follow me on TradingView and YouTube.