Pine Script v4, v5 and v6: What the Version Line Means

Beginner6 min read
A curving white marble staircase climbing past a pale wall, one flight after another
Image by Daniel von Appen on Wikimedia Commons, CC0

What Does the Pine Script Version Line Mean?

The line //@version=6 at the top of a script tells TradingView which version of Pine Script the code was written for. Version 6 is the newest, and versions 5 and 4 still appear in many older scripts. Each version has its own rules and names, so a script written for one version can show errors in another.

Why do versions exist at all? TradingView adds features and fixes awkward rules over time. A change that would break old scripts cannot be made quietly, so each version is a separate set of rules, and your script says which set it follows. That way an old script keeps working while new scripts get the improvements.

You met this line in What Is Pine Script?. This guide explains why the versions differ, what changed between them, and how to handle an older script.

How Do You Tell Which Version a Script Uses?

Look at the first line of the code. A version 4 script starts with //@version=4, a version 5 script with //@version=5, and so on. The line is a compiler annotation, a note that the editor reads before it reads the code.

  • Quick clues for v4: the declaration is study(), and indicators such as sma() and rsi() have no ta. in front.
  • Quick clues for v5 and v6: the declaration is indicator() or strategy(), and calls look like ta.sma() and input.int().
  • No line at all: TradingView’s documentation says version 1 is assumed when the annotation is missing. That is far older than any script in the library, so a missing line usually means a partial copy.

On the ForexR Pine Script Library, every script page shows its version in the kicker line and in a stat box, so you can check before you paste.

What Changed From Version 4 to Version 5?

The biggest change was namespaces, which are prefixes that group related functions. Version 5 put built-in functions into named groups so the language is easier to read and to extend:

  • ta. for technical analysis: sma() became ta.sma().
  • math. for maths: abs() became math.abs().
  • request. for outside data: security() became request.security().
  • str. for text: tostring() became str.tostring().
Diagram comparing the same moving-average script in version 4, using study() and sma(), and in version 6, using indicator() and ta.sma(), each under its version line
Illustration: the same 20-bar average in two versions. The first line tells you which rules apply; version 5 introduced the indicator() and ta. names that version 6 keeps.

Other changes you will notice in old code:

  • study() became indicator(). The job is the same: it declares an indicator.
  • One input() became many. Version 5 added input.int(), input.float(), input.bool(), input.string(), input.color(), input.timeframe() and others, one for each type of setting.
  • iff() and offset() were removed. You use the ?: operator and the [] history operator instead.
  • transp was replaced. Transparency now lives inside the colour, with color.new().
  • strategy.exit() must do something. It needs at least one exit level, such as a stop or a limit.

Here is the same moving average in both styles:

//@version=4
study("SMA", overlay=true)
len = input(20, minval=1)
plot(sma(close, len))

And in version 6:

//@version=6
indicator("SMA", overlay=true)
len = input.int(20, minval=1)
plot(ta.sma(close, len))

What Changed From Version 5 to Version 6?

Version 6 tightened rules that version 5 left loose. According to TradingView’s migration guide, the main changes are these:

  • Numbers are no longer true or false. An int or float is not silently turned into a bool. If you want that, write bool(x).
  • A bool can no longer be na.na means “no value”. A bool now always holds true or false, and conditions that used to give na give false.
  • and and or are lazy. The second part is evaluated only if it is needed. This lets you write a check such as “the array has items and the first item is above 10” in one line.
  • Dynamic requests are on by default. In version 5 you had to switch them on. They let request.*() calls take values that change from bar to bar.
  • The when argument is gone from the strategy.*() order functions. You use an if block.
  • Default margin changed. The default margin_long and margin_short are now 100, where they were 0.
  • Division changed. Dividing two ints that do not divide evenly now gives a fraction. Wrap the result in int() if you need a whole number.

The when change in practice looks like this. Version 5:

strategy.entry("Long", strategy.long, when=ta.crossover(fast, slow))

Version 6:

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

The margin change matters most for backtests. A strategy converted to version 6 can behave differently on the same chart, because of the new margin default and a change in strategy.exit(): when you give an exit level in two forms, such as a distance in ticks (profit) and a price (limit), version 6 uses whichever level price is expected to reach first. Always compare the converted version with the original, as how to backtest a Pine strategy advises.

How Do You Convert a Script to a Newer Version?

The Pine Editor has a converter. For version 5 to 6, open the editor’s “Manage script” menu and choose “Convert code to v6”. The converter only works if the version 5 code compiles. Even then, TradingView says some scripts need manual fixes afterwards.

For version 4, TradingView says the editor can convert indicators and strategies to version 5 automatically, but not every script converts. If it fails, you fix the errors by hand. Move one version at a time: 4 to 5, then 5 to 6.

Do not just change the number on the first line. The code inside follows the old rules, so the number and the code no longer match, and you get errors. The errors are explained in Pine Script errors and limits.

Which Version Should You Use?

  • Writing a new script: use version 6. It is the current one and gets the new features.
  • Using someone else’s script: leave its version alone. If it runs and you understand it, you do not need to convert it.
  • Following an old tutorial: check its version line. Most tutorials for version 4 need the changes above before they run in version 6.
  • Combining scripts: a script can only import a library by naming its version. Keep the pieces in the same version where you can.

How Does the ForexR Library Handle Versions?

The library lists scripts for versions 4, 5 and 6. Version 5 and version 6 each make up about four in ten scripts, and version 4 is under two in ten. You can filter by version with the chips on the list page, or open the version 6 scripts or version 5 scripts directly. A newer version is not proof that a script is better, and the library does not test any of them. A version 4 script can be well made, and a version 6 script can repaint.

Next, try the first indicator guide in version 6, and see how to add a Pine Script to TradingView for the paste steps. A converted script is still only a chart tool. It describes the past, backtests flatter, and leveraged forex and CFD trading carries a high risk of loss, so only risk money you can afford to lose.

FAQ

Can I run a Pine Script v4 script in TradingView today?

Usually yes. TradingView still compiles older versions, so you can paste a version 4 script as it is and keep its version line unchanged. If you want to edit it with newer features, convert it step by step, version 4 to 5 and then 5 to 6, and fix any errors.

What happens if I change //@version=5 to //@version=6 by hand?

The editor will probably show errors, because the code below still follows version 5 rules. Changes such as the removed when argument and stricter bool handling need real edits. Use the Manage script menu’s convert option instead, then fix what it leaves and compare results.

Why does my old Pine Script show an undeclared identifier error?

A version 4 script calls functions like sma() or study() with no prefix, which version 5 and 6 no longer accept. Either keep the version 4 line so TradingView reads it as version 4, or convert it. Also check you copied the whole script.

Is Pine Script version 6 better than version 5?

Version 6 is newer, stricter and the one TradingView builds on, so it suits new scripts. That does not make a version 6 script more profitable or less likely to repaint. Judge a script by its logic, its risk notes and your own tests, not by its version number.

Related on ForexR

Next lesson Pine Script vs MQL5: TradingView or MetaTrader for Trading? Continue

More Beginner Guides

What Is Forex Trading? A Beginner's Guide to the FX Market How to Start Forex Trading: A Step-by-Step Beginner Guide Currency Pairs Explained: Majors, Minors & Exotics Best Currency Pairs to Trade for Beginners: Where to Start Forex Trading Terms: A Plain-English Glossary for Beginners Going Long vs Going Short in Forex: Buy and Sell Explained Forex Spreads and Pips: What They Cost You Forex Lot Sizes and Order Types Explained for Beginners Leverage and Margin in Forex: A Beginner's Guide Margin Call and Stop Out in Forex: How They Work What Is Swap in Forex? Overnight Fees Explained Simply How Much Money Do You Need to Start Forex Trading? How to Choose a Forex Broker: Regulation, Costs & Checklist How Forex Brokers Make Money: Spreads, Swaps, IBs and B-Books Forex Rebates and Cashback Explained: Who Pays and What You Get Is Forex Trading Legal? How Forex Regulation Works Worldwide Forex Demo Account: How to Practise Trading Properly MT4 vs MT5: Key Differences and Which One to Choose How to Use MT4 and MT5: MetaTrader Tutorial for Beginners Types of Expert Advisors: Robots, Trade Managers, Copiers & More How to Use the ForexR EA Library: Find, Check & Download EAs How to Install an Expert Advisor in MT4 and MT5 (Step by Step) What Is Pine Script? TradingView's Chart Language Explained How to Use the ForexR Pine Script Library: Find & Copy Scripts How to Add a Pine Script to TradingView: Copy, Paste & Run Pine Script vs MQL5: TradingView or MetaTrader for Trading? How to Read a Forex Chart: Candlesticks & Trends Forex Trading Sessions: Hours and Best Times Best Time to Trade Forex: Best Hours, Days and Sessions How to Use an Economic Calendar for Forex How to Use Forex Market Analysis and News to Trade Forex vs Stocks: Which Is Better for Beginners to Trade? Forex vs Crypto Trading: Key Differences and Which Suits You How to Trade Gold (XAU/USD): A Beginner’s Six-Step Guide Why Does Gold Have Value? A Plain-English History of Money What Moves the Gold Price? 7 Drivers Explained in Plain English Physical Gold vs Gold CFDs, ETFs and Futures: 10 Ways to Own Gold Gold Lot Size, Pip Value and Margin Explained (XAU/USD) Best Time to Trade Gold: Sessions, Hours and the Daily Break Gold Spread, Swap Fees and Weekend Gaps: What Gold Costs Troy Ounces, Grams and Karats: How Gold Is Priced and Weighed Gold Trading Glossary: 40 Terms Explained in Plain English How to Read a Gold Price Forecast (and Why They Miss) 10 Gold Trading Mistakes That Empty Small Accounts Fast What Is Copy Trading in Forex? How It Works and What It Costs Forex Signals Explained: How They Work and How to Judge Them What Is AI Trading? A Plain-English Guide for Beginners AI Trading vs Algorithmic Trading: The Real Difference How to Use AI Chatbots for Trading Research Safely Forex Scams: How to Spot Them and Keep Your Money Safe AI Trading Scams: How to Spot Fake Bots and Deepfakes 12 Common Forex Trading Mistakes and How to Fix Them Is Forex Trading Profitable? What the Numbers Say Is Forex Trading Halal or Haram? A Neutral Explainer Is Gold Trading Halal? Spot, CFDs and Swap-Free Accounts Zakat on Gold, Crypto and Trading Accounts: A Simple Guide

Choose a Regulated Broker

Put what you’ve learned to work with a regulated broker. Compare costs, platforms and licences side by side.

Compare top brokers