Pine Script v6

Pine Script v6: The Complete
Reference for TradingView Traders

Pine Script v6 is the current TradingView scripting version. It introduced a strict type system, mandatory ta.* and request.* namespaces, typed input functions, and tighter scope rules. This page is the reference you need to write — or migrate — correct v6 code.

Current TradingView versionStrict type systemta.* namespaces

Quick answer

Pine Script v6 is the current scripting language version on TradingView. It added a strict type system, mandatory namespaces such as ta.* for technical analysis and request.* for data, typed input functions, and tighter scope rules. Code written for v4 or v5 often needs migration to compile under v6. TradePilot generates correct v6 code from plain English and loads it straight into your TradingView editor.

What changed in v6

Six breaking changes from Pine Script v5

Every item below is a source of compile errors when migrating a v5 script. Fix all six and your script will likely compile on the first attempt.

Namespaced built-in functions

All technical analysis functions moved into the ta.* namespace. Use ta.ema(), ta.rsi(), ta.crossover(), ta.atr() — the bare forms (ema(), rsi()) that worked in v4/v5 are compile errors in v6.

Typed input functions

Inputs are now typed: input.int(), input.float(), input.bool(), input.string(), input.color(), input.source(). The generic input() shorthand is gone. Each function has minval, maxval, step, and group parameters.

Explicit type declarations

v6 has a strict type system. Declare variable types explicitly — float fastEma = ta.ema(close, 9) — and never mix types without casting. The compiler rejects implicit coercions it previously allowed.

Global-scope restrictions

plot(), bgcolor(), hline(), and alertcondition() calls must be at the global scope. Placing them inside if/for blocks is a compile error in v6. Move all visual and alert declarations to the top level.

var persistence keyword

Use the var keyword to declare a variable that persists its value across bars. Without var, every variable resets on each bar. This replaces the old series initialisation pattern and makes intent explicit.

request.security lookahead

request.security() now requires the lookahead parameter explicitly when you need it. Using lookahead=barmerge.lookahead_on without understanding it introduces future-leak; the default is barmerge.lookahead_off.

v6 quick reference

Canonical Pine Script v6 strategy skeleton

Copy this as a starting point. Every section — inputs, calculations, conditions, visuals, orders, alerts — uses correct v6 syntax and will compile on TradingView without modification.

strategy_skeleton_v6.pine
//@version=6
strategy("Strategy Name", overlay=true, margin_long=100, margin_short=100)

// ── Inputs ──────────────────────────────────────────────
fastLen  = input.int(defval=9,   title="Fast EMA",      minval=1, group="Settings")
slowLen  = input.int(defval=21,  title="Slow EMA",      minval=1, group="Settings")
rsiLen   = input.int(defval=14,  title="RSI Length",    minval=1, group="Settings")
rsiLevel = input.int(defval=50,  title="RSI Threshold", minval=1, maxval=99, group="Settings")
slPerc   = input.float(defval=1.5, title="Stop Loss %",   step=0.1, group="Risk")
tpPerc   = input.float(defval=3.0, title="Take Profit %", step=0.1, group="Risk")

// ── Calculations ─────────────────────────────────────────
float fastEma = ta.ema(close, fastLen)
float slowEma = ta.ema(close, slowLen)
float rsi     = ta.rsi(close, rsiLen)

// ── Conditions ───────────────────────────────────────────
bool longEntry = ta.crossover(fastEma, slowEma) and rsi > rsiLevel
bool longExit  = ta.crossunder(fastEma, slowEma)

// ── Visuals (must be at global scope in v6) ──────────────
plot(fastEma, "Fast EMA", color=color.aqua,   linewidth=1)
plot(slowEma, "Slow EMA", color=color.orange, linewidth=1)
bgcolor(longEntry ? color.new(color.green, 92) : na, title="Entry Bar")

// ── Orders ───────────────────────────────────────────────
if longEntry
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long",
         from_entry = "Long",
         stop        = close * (1 - slPerc / 100),
         limit       = close * (1 + tpPerc / 100))

if longExit
    strategy.close("Long", comment="EMA cross exit")

// ── Alerts ───────────────────────────────────────────────
alertcondition(longEntry, title="Long Entry", message="Long entry signal fired")

Correct Pine Script v6 — all ta.* namespaces, typed inputs, global-scope plots

Common v6 errors

Five compile errors and their exact fixes

These are the most common errors traders hit when writing or migrating to v6. Each card shows the exact TradingView error message, why it happens, and the line-level fix.

Error

Undeclared identifier 'ema'

Cause

Calling ema() without the ta. prefix — valid in v4/v5 but a compile error in v6.

Fix

Replace ema(close, 9) with ta.ema(close, 9). Apply the same fix to rsi(), sma(), ema(), atr(), crossover(), crossunder(), and all other ta.* functions.

Error

Undeclared identifier 'input'

Cause

Using the generic input() function or an old typed shorthand that no longer exists in v6.

Fix

Replace input(9, 'Length', input.integer) with input.int(defval=9, title='Length'). Use input.float() for decimals, input.bool() for checkboxes.

Error

Cannot call plot() inside a local scope

Cause

A plot(), bgcolor(), or hline() call is inside an if block, a for loop, or a function body.

Fix

Move all plot() and bgcolor() calls to global scope. If you need conditional coloring, use a ternary in the color parameter: plot(close, color=longCond ? color.green : color.red).

Error

Cannot use 'strategy.entry' outside a strategy

Cause

The script declaration is //@version=6\nindicator(...) but the code uses strategy.* calls.

Fix

Change the declaration to strategy(...). An indicator script cannot contain strategy.entry(), strategy.exit(), or strategy.close().

Error

Type mismatch: expected int, got float

Cause

Passing a float value to a parameter that requires int, or mixing series types without explicit casting.

Fix

Use int() to cast: ta.ema(close, int(myFloatLen)). Check that input.int() is used for length parameters, not input.float().

FAQ

Pine Script v6 questions

What is Pine Script v6?

Pine Script v6 is the current version of TradingView's scripting language, released in 2023. It introduced a strict type system, mandatory ta.* and request.* namespaces, typed input functions (input.int, input.float), and tighter scope rules for plot() and bgcolor() calls. v6 is the version TradePilot generates by default.

Is Pine Script v6 backwards compatible with v5?

Not fully. v6 breaks v5 scripts that use un-namespaced functions (ema() instead of ta.ema()), the generic input() shorthand, or plot()/bgcolor() calls inside local scopes. TradePilot can migrate a v5 script to v6 — paste it into the chat and ask for a v6 conversion.

Where is the Pine Script v6 user manual?

The official Pine Script v6 reference is published at pine-script-docs.tradingview.com. It covers every built-in function, type, and operator. TradePilot injects a condensed version of this reference into every AI request so the generated code uses correct v6 signatures without you needing to look anything up.

What functions changed from v5 to v6?

The most impactful changes: (1) all ta.* functions lost their bare names — ema() is now ta.ema(); (2) input() was replaced by input.int(), input.float(), input.bool(), input.string(), input.color(), input.source(); (3) request.security() gained an explicit lookahead parameter. Variable type declarations became mandatory and plot()/bgcolor() must be at global scope.

How do I fix 'undeclared identifier' in Pine Script v6?

This error almost always means you are using a function name without its v6 namespace prefix. Check whether it is a technical analysis function (add ta.), a request function (add request.), or an input function (replace input() with input.int() or input.float()). If it is a custom variable, make sure it is declared before use and at the correct scope.

Does TradePilot generate Pine Script v6?

Yes. TradePilot generates Pine Script v6 exclusively. Unlike ChatGPT or Claude (trained on outdated v4/v5 data), TradePilot injects the complete v6 function reference into every request — every ta.* signature, every input type, every strategy.* call — so the output compiles correctly on TradingView the first time.

Generate Pine Script v6 in seconds

Stop cross-referencing the manual. Describe your strategy in plain English and TradePilot writes correct Pine Script v6 — with the complete function reference in context, so it compiles the first time.