Pine Script v4, v5 and v6: What Changes When You Convert
Short answer
Some changes between versions are renames you can do with search and replace; others change what the script computes. The //@version=N line decides how TradingView compiles the script, and a script without it is compiled as version 1, so add the correct pragma before converting an old script.
Most Pine scripts you find were written for v4 or v5, while TradingView's current version is v6. Some of the changes between versions are renames you can do with search and replace; others change what the script computes. This guide is for anyone upgrading an old script or converting it to MQL, where you need to know which version's rules the original follows.
Find the version first
The first line, //@version=N, decides how TradingView compiles the script. A script without that line is compiled as version 1, so old copied snippets without a pragma behave differently from what their syntax suggests. Stratlate's converter reads the pragma too: a script that declares a version below 6, or none at all, keeps the pre-v6 evaluation of and/or described below. Add the correct pragma before converting a v3 or v4 script so that it is treated as what it is.
v4 to v5: mostly renames
Version 5 moved most built-ins into namespaces and split the generic input(). The values computed are almost always the same. The changes you will meet most:
study()becameindicator().- Technical functions moved to
ta.:sma,ema,rsi,crossover,highest,valuewhenand so on. security()becamerequest.security(), and itsresolutionparameter becametimeframe.tostring()andtonumber()moved tostr.; maths functions moved tomath..input(14, type=input.integer)becameinput.int(14), with one function per type (input.float,input.bool,input.source,input.timeframe, ...).iff(cond, a, b)was removed; usecond ? a : b.strategy.entry(id, long)becamestrategy.entry(id, direction)withstrategy.long/strategy.short.- The
transpparameter was deprecated in favour ofcolor.new(color, transparency). In v4,bgcolor()andfill()applied 90 transparency by default; in v5 you state it.
Two v5 changes do affect values: the default session days in time(), time_close() and input.session() changed from Monday to Friday to all seven days, and barmerge constants are required where v4 accepted true/false. The same small script in both versions:
//@version=4
study("RSI Signal")
len = input(14, title="Length", type=input.integer)
r = rsi(close, len)
plot(r, color=color.purple)
bgcolor(crossover(r, 30) ? color.green : na, transp=80)//@version=5
indicator("RSI Signal")
len = input.int(14, title="Length")
r = ta.rsi(close, len)
plot(r, color=color.purple)
bgcolor(ta.crossover(r, 30) ? color.new(color.green, 80) : na)v5 to v6: changes that alter results
Version 6 is a smaller syntactic change but a larger semantic one. These are the items that most often change what a script computes or whether it compiles:
- Lazy
and/or. In v6 the right-hand side is skipped when the left side already decides the result. In v4 and v5 both sides always ran. - No implicit number-to-bool cast.
if volumemust becomeif volume > 0orif bool(volume). boolcannot bena.na(),nz()andfixnan()no longer accept bools, and aniforswitchexpression with no matching branch returnsfalsefor a bool instead ofna.- Division of two
const intvalues can be fractional.5 / 2is2.5in v6; v5 truncated it to 2. Wrap withint()ormath.floor()if truncation was intended. - Dynamic requests by default.
request.*()calls may appear in loops, conditionals and with series arguments unlessdynamic_requests = false. - Strategy defaults. The
whenparameter ofstrategy.*()functions is gone (useif), the defaultmargin_long/margin_shortis now 100 percent instead of 0, andstrategy.exit()with both absolute and relative levels uses whichever would trigger first. - Smaller items.
timeframe.periodalways includes a multiplier ("1D", not"D");transpis removed entirely; somearray.*()functions accept negative indices; the end value of aforloop is re-evaluated before every iteration; and several built-in colours such ascolor.redchanged shade.
//@version=5
indicator("Bool na")
bool trend = na
if close > ta.sma(close, 50)
trend := true
plot(na(trend) ? 0 : 1)
if volume
label.new(bar_index, high, "vol")//@version=6
indicator("Bool na")
bool trend = false // bool can no longer be na
if close > ta.sma(close, 50)
trend := true
plot(trend ? 1 : 0)
if volume > 0 // no implicit number -> bool cast
label.new(bar_index, high, "vol")Why lazy evaluation matters for conversion
Functions such as ta.crossover, ta.ema or ta.rsi keep their own history from one bar to the next. If such a call sits on the right of and, v5 runs it on every bar, but v6 runs it only on bars where the left side is true, so it sees a different, sparser history and can return different values. The safe pattern in v6 is to compute the stateful value first:
//@version=5
indicator("Lazy and/or")
isUp = close > open
// v5: ta.crossover runs on every bar, whatever isUp is.
signal = isUp and ta.crossover(ta.sma(close, 5), ta.sma(close, 20))
plotshape(signal)//@version=6
indicator("Lazy and/or")
isUp = close > open
// v6: hoist the stateful call so it still runs on every bar.
cross = ta.crossover(ta.sma(close, 5), ta.sma(close, 20))
signal = isUp and cross
plotshape(signal)When converting to MQL5 the question flips: C-style && and || short-circuit exactly like v6. A v4 or v5 script whose right-hand operand has side effects must evaluate both operands before combining them. Stratlate's converter does this by version: for scripts before v6 it emits an expression that evaluates both sides, and for v6 it keeps short-circuiting, with each conditional ta.* call keeping its own per-call history as TradingView does.
// MQL5's && skips its right side when the left is false, like v6 "and".
// To keep v4/v5 behaviour, run the stateful part on every bar first:
bool cross = FastBuf[i] > SlowBuf[i] && FastBuf[i - 1] <= SlowBuf[i - 1];
bool signal = (close[i] > open[i]) && cross;Should you upgrade before converting?
Not necessarily. Converting the script under its own version reproduces what the author saw on TradingView, which is usually the reference you want to match. Upgrade first only when you will keep maintaining the Pine version as well; then use the Pine Editor's conversion tool, read the changes it makes, and compare the plots of the old and new versions on the same chart before converting. Stratlate accepts v4, v5 and v6 source, including v4 forms such as study(), security(), iff() and input(type=...). One version difference it does not reproduce: it always divides integers the v6 way, so a v4 or v5 line such as half = 5 / 2 that relied on truncation needs an explicit int() or math.floor(). Whichever path you choose, compile and backtest the MQL result.
Pitfalls
- Upgrading by changing only the pragma. Symptom: a script that still compiles but whose signals changed, usually because of lazy
and/oror a bool that used to bena. - Missing pragma. Symptom: TradingView compiles the script as v1, and other tools may assume a different version; results differ between the two.
- Translating v4
transpor bgcolor defaults literally. Symptom: solid backgrounds where the original was 90 percent transparent. - Converting a v5 strategy and comparing with v6 defaults. Symptom: different position sizes or margin calls, because the default margin changed from 0 to 100 percent.
- Comparing
timeframe.periodwith"D"in v6. Symptom: daily-only branches never run.
FAQ
Can I just change //@version=5 to //@version=6?
Sometimes the script still compiles, but that does not mean it computes the same values. Check every and/or with a stateful call on the right, every bool that could be na, divisions of integer constants and strategy margin settings, then compare both versions on the same chart.
Does a Pine to MQL5 converter need the script upgraded to v6 first?
Not with Stratlate: it reads the version pragma, keeps the pre-v6 evaluation of and/or for older scripts, and accepts v4 forms such as study(), security(), iff() and input(type=...). Check any division of two integer constants that relied on v5 truncation.
Do Pine Script v4 and v5 scripts still work on TradingView?
Yes. TradingView compiles each script under the version in its //@version line, so v4 and v5 scripts keep running with their own rules. New language features are added to v6, so an old script has to be upgraded to use them.
Convert any Pine version to MQL5
Paste a v4, v5 or v6 script. The converter applies that version's rules and lists anything approximated in its report.
Open the converter