Explainers / Pine Script v5 to v6
Pine Script v5 to v6: what changes, what breaks, and what changes without telling you
Version 6 of Pine Script is mostly version 5 with stricter types and a few new defaults. The Pine Editor has a converter that rewrites most of it for you. The trouble is that the changes come in two kinds. Some stop the script compiling, so the editor points straight at them. Others compile cleanly and change what the script calculates, and nothing on screen says so. This page lists both, from TradingView's own migration guide, in plain English, with the usual fix.
How the converter works
Open the v5 script in the Pine Editor. When a newer version exists, the //@version=5 line is highlighted. The script's menu (the dropdown next to its name) has "Convert code to v6". Three things to know about it:
- It only runs on a script that already compiles in v5. Fix the v5 errors first.
- It rewrites what it can. Removing
transpand similar mechanical changes are handled. TradingView's guide does not publish a full list of what it handles and what it leaves to you. - Its output can still fail to compile. Rarely, but it happens; the editor highlights the lines.
There is no official online converter outside the editor. A converter on another site is either the same idea done by someone else, or a chatbot. Either can produce code that compiles; neither can tell you whether the result calculates what the old script did. The second half of this page is why that matters.
Changes that stop a script compiling
These are the easy ones: the editor stops and shows you the line.
- A bool can no longer be na.
na(),nz()andfixnan()no longer accept a bool, and a bool variable cannot start as na. Remove those calls. If the script really needs three states (yes, no, not yet known), use an int such as -1, 0 and 1. - Numbers no longer turn into bools by themselves. In v5,
if bar_indexorx ? a : bwith a number forxtreated 0 and na as false and anything else as true. In v6 write it out:bool(bar_index), or better, the comparison you meant, such asx != 0. - A plot style cannot be na. A
switchorifthat picks aplot.style_*and can fall through to na now fails. Give theswitcha default branch and theifan else. - **The
whenparameter is gone** fromstrategy.entry,strategy.order,strategy.exit,strategy.close,strategy.close_all,strategy.cancelandstrategy.cancel_all. Put the call inside anifblock with the same condition. - **
transpis gone** fromplot,plotshape,plotchar,plotarrow,bgcolorandfill. Usecolor.new(colour, transparency). One catch: if the colour insidecolor.new()is calculated, the colour picker disappears from the script's Style settings. Keep the base colour a constant or an input. - **A
varvalue cannot be used where a simple length is required.** In v5,ta.ema(close, myVarCounter)compiled and quietly used only the counter's first value. In v6 it is an error. Use a constant or an input for lengths. - History on a literal or a constant fails.
6[1]ortrue[10]are errors. Remove the brackets. - History on a field of a user-defined type fails.
obj.field[10]is invalid; write(obj[10]).field, or copy the field into a variable and index that. - **A series
offsetfails.** In v5 a changingoffsetgave a warning and used only the last value across the whole chart. In v6 it must be a simple value. - **
linewidthbelow 1 fails** inplot()andhline(). Clamp it, for exampleinput.int(2, minval = 1). - A repeated parameter fails.
plot(x, color = a, color = b)was a warning in v5 (the first one won). Delete the duplicate. - **
request.*()inside a local block, with dynamic requests turned off, fails.** v6 turns dynamic requests on by default; if your converted script setsdynamic_requests = false, anyrequest.security()inside anif, a loop or a function called from one is rejected.
Changes that compile and quietly change results
These are the ones that matter. The converted script compiles, the chart draws, and the numbers are not the numbers the v5 script produced.
- **
andandornow stop early.** In v6,a and bdoes not evaluatebwhenais false. That is harmless for plain comparisons and harmful for anything with memory. TradingView's own example isclose > open and ta.rsi(close, 14) > 50: on every bar where the candle is red,ta.rsiis skipped, its internal history has gaps, and its values on the green bars are wrong. Fix: calculateta.*calls, and anything else that depends on previous bars, on their own line in the global scope, then use the variable in the condition. - Strategies now default to a margin setting of 100. In v5,
margin_longandmargin_shortdefaulted to 0, so a backtest could hold positions larger than the account. In v6 they default to 100: entries larger than the available funds are refused, and losing shorts can be margin-called and partly closed. A v6 backtest of the same script can show fewer trades, different sizes and margin-call events that v5 never had. To compare with the old results, set both to 0 instrategy(); to keep the more realistic behaviour, keep 100 and expect the numbers to move. - **
strategy.exit()now uses whichever level is hit first.** In v5, when a call had both an absolute level (limit,stop) and a relative one (profit,loss, in ticks), the absolute one always won and the relative one was ignored. In v6 both are live and the first one price reaches closes the trade. A script that passedprofit = 0orloss = 0as a placeholder now exits at the entry price straight away. - Dividing two constant whole numbers keeps the fraction.
5 / 2gave 2 in v5 when both sides were constants and gives 2.5 in v6. A length, an index or a loop bound built that way changes. Wrap it inint(),math.floor(),math.round()ormath.ceil(), whichever the old code meant. - **
timeframe.periodalways carries a number.** A daily chart reports"1D", not"D"(likewise"1W"and"1M"). A test such astimeframe.period == "D"is now false on a daily chart, so whatever it switched on or off is silently switched. Compare against"1D", or usetimeframe.isdaily. - A bool that would have been na is now false. An
iforswitchwith no matching branch that returns a bool gives false, andflag[1]on the first bar gives false. A v5 script that relied on na meaning "not decided yet" now reads it as "no". - **
forloop bounds are re-evaluated on every pass.** In v6,for i = 0 to array.size(a) - 1recalculates the end after each iteration. If the loop adds to the array, it never ends and the script stops with a runtime error. Store the bound in a variable before the loop. - Strategies with more than 9,000 orders keep running. v5 stopped with a runtime error (outside Deep Backtesting). v6 drops the oldest orders instead, and those trades disappear from the Strategy Tester, so the summary covers only the trades that are left.
strategy.closedtrades.first_indextells you where the kept history starts. - Some built-in colours changed shade.
color.red,color.tealandcolor.yelloware slightly different colours, and alabel.new()with no text colour now writes white text instead of black. Harmless for calculations; worth a look if a script's labels are now unreadable on a light background. - Negative array indices now work.
array.get(a, -1)returns the last element instead of raising an error. Not a break, but a v5 script that relied on that error to stop is now silent.
A checklist for a converted script
- Convert in the editor, fix whatever it highlights, and save under a new name so the v5 version stays on the chart beside it.
- Search the source for
andandor. Anyta.call,[1]reference orvarupdate on the right-hand side of one gets moved onto its own line above. - For a strategy: note the v5 Strategy Tester summary, then run v6 once with
margin_long = 0, margin_short = 0and once without. If the first matches v5 and the second does not, the difference is margin, not a bug. - Search for
strategy.exit(calls that have both alimitorstopand aprofitorloss. Decide which one the script meant. - Search for
timeframe.period ==and any division of two whole-number constants. - Put both versions on the same chart and compare the plots bar by bar on a few hundred bars. Matching lines are the only proof the conversion is faithful.
Where it fails
- This page follows TradingView's published migration guide as read on 1 December 2026. TradingView updates the language and the guide; a later change is not here.
- The guide prints no error messages, so this page does not quote any. The words your editor shows may differ from the descriptions above.
- The converter's exact coverage is not published. Whether it fixes a given item for you depends on the version of the editor you run it in.
- Matching plots prove the conversion only on the bars you compared. A difference that appears only on gaps, on a different timeframe or in realtime can survive a side-by-side check on history.
- Margin and exit changes move backtest numbers in either direction. Neither the v5 nor the v6 result is the "true" one; they answer different questions about the same rules.
- This page cannot see your script. It lists what changed in the language, not what changed in your code.
Educational only. Not financial advice. Nothing here is a recommendation to buy or sell anything, and nothing here says that a script which compiles, converts or matches its old version is correct or safe to trade. Pine Script and TradingView are products of TradingView, Inc.; this page describes their published language and migration guide in our own words and is not affiliated with them.
Also explained: Repaint checker, VWAP, Supertrend, RSI, UT Bot Alerts, Nadaraya-Watson Envelope, Squeeze Momentum, What is repainting on TradingView?, Is TradingView backtesting accurate?, Chandelier Exit, WaveTrend Oscillator, QQE MOD, Pivot highs and lows, Hull Suite, Range Filter, Twin Range Filter, HalfTrend, ZigZag++, SSL Hybrid, CM MacD Ultimate MTF, Volume Profile / Fixed Range, ADX and DI, Support Resistance Channels, Market Structure Break & Order Block, Order Block Finder, Pivot Point SuperTrend, Trendlines with Breaks [LuxAlgo], Divergence for Many Indicators, TradingView script selling terms, Support and Resistance Levels with Break, Liquidity Swings [LuxAlgo], Pine Script errors, Breakout Probability (Expo), Did ChatGPT or Claude write your Pine Sc, Lorentzian Classification, SuperTrend AI (Clustering), Machine Learning Adaptive SuperTrend, Dynamic Swing Anchored VWAP, Support and Resistance Power Channel by , Support and Resistance (High Volume Boxe, Trendline Breakouts With Targets by Char, Breakout Finder by LonesomeTheBlue, Volume Flow Indicator by LazyBear, FVG Order Blocks by BigBeluga, CM_Williams_Vix_Fix by ChrisMoody, Bollinger + RSI Double Strategy by Chart, Ultimate Moving Average MTF by ChrisMood, Fibonacci Bollinger Bands by Rashad, Candlestick Patterns Identified by repo3, MACD + SMA 200 Strategy by ChartArt, Moving Average Cross Alert, Multi-Timefr, WaveTrend Oscillator [WT] by LazyBear, WaveTrend with Crosses [LazyBear], CM_Price-Action-Bars by ChrisMoody, RSI Candles by glaz, CM RSI-2 Strategy Lower Indicator by Chr, CM Stochastic Multi-TimeFrame by ChrisMo, CM_Ultimate RSI Multi Time Frame by Chri, TonyUX EMA Scalper - Buy / Sell by tux, MACD 4C by vkno422, My Pine Script is valid. What did the va
Get the next teardown by email
One short email when a new teardown or free tool goes live, about twice a month. No signals, no hype. Unsubscribe in one click.
Almost done. Check your inbox and click the confirmation link.
Sent with Kit. Your address is used only for these emails. Privacy
No TradingView account yet? This affiliate link gives you a $15 coupon toward a paid plan, and we earn a commission if you buy one. The free plan is enough to follow this page.