AI-written Pine Script usually fails for a short list of reasons: v4 and v5 function names under a //@version=6 header, functions that don't exist, the v6 bool and na rules, indentation, and plot() inside if blocks; worse, some scripts compile but repaint or backtest without costs. Run the code through a Pine Script checker, fix each error from its message, then confirm it in TradingView's Pine Editor and Strategy Tester.
Below are ten of these mistakes, each with a short broken example, the fixed Pine Script v6 script and the page that explains the error. The first seven stop a script from compiling. The last three are worse: TradingView accepts the script, and nothing tells you it's wrong.
A disclosure: we make an AI Pine Script tool, so we have an interest here. The rules below come from TradingView's own documentation, and the fixes work whichever AI wrote the code. Every fixed script on this page passes Edgoro Pine Check, our own v6 checker, with no errors and no notes. Pine Check reads code; it isn't TradingView's compiler, so the Pine Editor stays the final check.
Why AI assistants get Pine Script wrong
General-purpose AI assistants such as Claude learned Pine Script from years of public code, and most of it was written for v4 or v5. Pine Script v6 kept v5's function names but tightened rules that older code relied on: bools can't be na, numbers are no longer read as true or false, and and / or stop evaluating as soon as the result is known. An assistant also writes code without running TradingView's compiler, so an invented function looks as plausible to it as a real one. For a broader look at what goes well and what doesn't, read what general-purpose AI assistants get right and wrong in Pine Script.
Mistakes that stop the script from compiling
1. v4 and v5 names under a //@version=6 header
A common pattern: a correct first line on top of older code.
//@version=6
study("RSI signals")
r = rsi(close, 14)
up = crossover(r, 30)
plot(r, color = iff(r > 70, color.red, color.blue))
bgcolor(up ? color.green : na, transp = 80)
Under v6, study, rsi, crossover and iff each give Could not find function or function reference (for example Could not find function or function reference 'study'), and the last line gives The "bgcolor" function does not have an argument with the name "transp".
//@version=6
indicator("RSI signals")
r = ta.rsi(close, 14)
up = ta.crossover(r, 30)
plot(r, "RSI", r > 70 ? color.red : color.blue)
hline(30, "Oversold")
bgcolor(up ? color.new(color.green, 80) : na)
| Old code | Pine Script v6 | Error page |
|---|---|---|
study() | indicator() | Could not find function 'study' |
rsi(), ema(), crossover() | ta.rsi(), ta.ema(), ta.crossover() | Could not find function or function reference |
security() | request.security() | Could not find function 'security' |
iff(cond, a, b) | cond ? a : b | Could not find function or function reference |
transp = 80 | color.new(color.green, 80) | No argument named transp |
strategy.entry(..., when = cond) | if cond, then strategy.entry(...) in the block | No argument named when |
Edgoro Pine Check reports every line of the broken script, with the v6 name to use.
2. Functions that don't exist
AI assistants fill gaps with names that sound right. ta.sum() is a typical one: Pine Script has a sliding sum, but it lives in the math namespace.
//@version=6
indicator("20-bar net change")
netChange = ta.sum(close - close[1], 20)
plot(netChange)
TradingView stops with Could not find function or function reference 'ta.sum'. The same happens to invented variables: strategy.position_entry_price gives Undeclared identifier, because the real one is strategy.position_avg_price.
//@version=6
indicator("20-bar net change")
netChange = math.sum(close - close[1], 20)
plot(netChange, "Net change", color.teal)
hline(0, "Zero")
Pine Check doesn't catch this one. It knows the old names that moved to ta., math., str. and request. (a bare sum() is reported, with math.sum() as the fix), but it doesn't hold a list of every built-in, so an invented ta. or strategy. name gets through. TradingView's compiler catches it when you save, and the Pine Script v6 reference manual settles whether a name exists. Error pages: Could not find function or function reference and Undeclared identifier.
3. A bool that can be na
In v5, a bool could be true, false or na, and AI assistants still use na to mean "not set yet". TradingView's migration guide to v6 is explicit: bools can no longer be na, and na(), nz() and fixnan() no longer accept them.
//@version=6
indicator("Trend flag")
ma = ta.sma(close, 50)
var bool trendUp = na
if ta.crossover(close, ma)
trendUp := true
if ta.crossunder(close, ma)
trendUp := false
plot(na(trendUp) ? 0 : trendUp ? 1 : -1)
Line 4 fails with Cannot assign a value of the "simple na" type to the "trendUp" variable, and na(trendUp) fails because na() no longer takes a bool. When a flag really has three states, use an int:
//@version=6
indicator("Trend flag")
ma = ta.sma(close, 50)
crossUp = ta.crossover(close, ma)
crossDown = ta.crossunder(close, ma)
// 1 = up, -1 = down, 0 = no cross yet
var int trend = 0
if crossUp
trend := 1
if crossDown
trend := -1
plot(trend, "Trend", trend > 0 ? color.teal : trend < 0 ? color.red : color.gray, style = plot.style_stepline)
Pine Check reports both lines. Error pages: bool cannot be na and Cannot call na with a bool.
4. A number used as a condition
Up to v5, Pine read 0 and na as false and any other number as true. v6 removed that conversion, so if ph and volume ? ... no longer compile.
//@version=6
indicator("Pivot highs", overlay = true)
ph = ta.pivothigh(high, 5, 5)
if ph
label.new(bar_index - 5, ph, "PH", style = label.style_label_down)
bgcolor(volume ? na : color.new(color.gray, 80))
if ph gives The condition of the "if" statement must evaluate to a "bool" value., and the ternary on the last line gives a Cannot call "operator ?:" error that names volume. Write the test you mean:
//@version=6
indicator("Pivot highs", overlay = true)
ph = ta.pivothigh(high, 5, 5)
if not na(ph)
label.new(bar_index - 5, ph, "PH", style = label.style_label_down)
// Grey background on bars without volume
bgcolor(volume == 0 ? color.new(color.gray, 80) : na)
Pine Check reports price and volume series used directly as conditions, such as volume ? here. It doesn't report if ph: a variable that holds a number is only caught by TradingView's compiler. Error page: if condition must be a bool.
5. Wrong indentation in blocks and wrapped lines
Pine Script uses indentation as structure. TradingView's User Manual sets two rules: the lines of a block (if, for, a function) are indented by 4 spaces or a tab, and a line that continues the one above, outside parentheses, must not be indented by a multiple of 4. Code pasted from a chat answer often breaks both, especially after an editor re-indents it.
//@version=6
indicator("Trend filter", overlay = true)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
longTrend = close > ema50 and
ema50 > ema200
if longTrend and not longTrend[1]
label.new(bar_index, low, "Trend up", style = label.style_label_up)
plot(ema50, "EMA 50", color.teal)
TradingView reports Syntax error at input "end of line without line continuation" on line 5: its 4-space continuation looks like a block, so line 5 ends on and with nothing after it. The 2-space line under the if gives a Syntax error at input as well.
//@version=6
indicator("Trend filter", overlay = true)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
longTrend = close > ema50 and
ema50 > ema200
if longTrend and not longTrend[1]
label.new(bar_index, low, "Trend up", style = label.style_label_up)
plot(ema50, "EMA 50", color.teal)
plot(ema200, "EMA 200", color.orange)
Inside parentheses, any indentation works, so wrapping the whole condition in ( ) is the other fix. Pine Check reports both lines. Error pages: end of line without line continuation and Syntax error at input.
6. plot() inside an if block
Asked to "plot the level when price breaks out", an assistant often puts plot() inside the if.
//@version=6
indicator("Breakout", overlay = true)
hh = ta.highest(high, 20)[1]
if close > hh
plot(hh, color = color.green)
alertcondition(true, "Breakout", "Close above the 20-bar high")
TradingView answers Cannot use "plot" in local scope, and alertcondition() follows the same rule. Plot functions run at the top level of the script; the condition goes into their arguments:
//@version=6
indicator("Breakout", overlay = true)
hh = ta.highest(high, 20)[1]
breakout = close > hh
plot(hh, "20-bar high", color.gray)
plotshape(breakout, "Breakout", shape.triangleup, location.belowbar, color.green)
alertcondition(breakout, "Breakout", "Close above the 20-bar high")
Drawings such as label.new(), alert() and strategy orders can stay inside an if. Pine Check reports plot(), plotshape(), bgcolor(), fill(), alertcondition() and the other top-level-only calls inside blocks. Error page: Cannot use plot in local scope.
7. Syntax from other languages, and chat formatting
Pine looks a little like Python and a little like JavaScript, and assistants mix them up: && and || instead of and and or, null instead of na, elif instead of else if. Copying from a chat window adds two more problems: curly quotes, and the three-backtick lines around the code block.
//@version=6
indicator(“Inside bar”, overlay = true)
inside = high < high[1] && low > low[1]
plotshape(inside, "Inside", shape.square, location.abovebar)
The curly quotes give no viable alternative at character "“", and && gives the same message with "&". A leftover fence line makes the script fail on line 1.
//@version=6
indicator("Inside bar", overlay = true)
inside = high < high[1] and low > low[1]
plotshape(inside, "Inside bar", shape.square, location.abovebar, color.orange)
Pine Check reports curly quotes and the typographic minus sign. It doesn't report &&, null, elif or a leftover fence; TradingView does when you save. Error page: no viable alternative at character.
Mistakes that compile and still mislead you
8. ta.* calls that don't run on every bar
In v6, and and or are lazy: when the left side already decides the result, the right side isn't evaluated. TradingView's migration guide warns that skipping a call such as ta.rsi() this way breaks the history the function keeps for its own calculation.
//@version=6
indicator("RSI pullback in an uptrend", overlay = true)
trendUp = close > ta.sma(close, 200)
signal = trendUp and ta.crossover(ta.rsi(close, 14), 30)
plotshape(signal, "Signal", shape.triangleup, location.belowbar, color.teal)
Here ta.rsi() and ta.crossover() only run on bars above the 200 SMA, so the RSI is calculated on those bars alone and the "previous value" of the crossover can be from days earlier. Signals then appear where the chart's RSI didn't cross 30, or go missing where it did. TradingView compiles it with a warning that the function should be called on each calculation for consistency. Calculate first, combine second:
//@version=6
indicator("RSI pullback in an uptrend", overlay = true)
rsiCross = ta.crossover(ta.rsi(close, 14), 30)
trendUp = close > ta.sma(close, 200)
signal = trendUp and rsiCross
plotshape(signal, "Signal", shape.triangleup, location.belowbar, color.teal)
The same applies inside if blocks, switch, ternary branches and loops. Pine Check adds a note for these calls. Error page: should be called on each calculation for consistency.
9. request.security() that reads the future
Asked for "the daily trend on an intraday chart", an assistant may write:
//@version=6
indicator("Daily trend", overlay = true)
dEma = request.security(syminfo.tickerid, "D", ta.ema(close, 20), lookahead = barmerge.lookahead_on)
plot(dEma, "Daily EMA 20", color.orange)
bgcolor(close > dEma ? color.new(color.teal, 90) : na)
On historical bars, lookahead_on with an expression that isn't offset hands every intraday bar of a day the daily EMA calculated from that day's final close, hours before that close existed. TradingView's User Manual says this leaks future data into history, and recommends offsetting the expression by one bar with lookahead_on, which always returns the last confirmed value.
//@version=6
indicator("Daily trend", overlay = true)
// [1] with lookahead_on: the last completed day's value, on history and in real time
dEma = request.security(syminfo.tickerid, "D", ta.ema(close, 20)[1], lookahead = barmerge.lookahead_on)
plot(dEma, "Daily EMA 20", color.orange)
bgcolor(close > dEma ? color.new(color.teal, 90) : na)
There is no error page for this one, because the broken script compiles without a message: the history just looks better than real time. Pine Check adds a note for lookahead_on without a [1] offset. The guides: Pine Script repainting explained and request.security() for multi-timeframe data.
10. Strategies without costs or exits
Unless you ask, an assistant often declares strategy() with a title only. The Strategy Tester then runs without commission or slippage, and an entry on a state (fast > slow is true for bar after bar) keeps the strategy in the market with no stop.
//@version=6
strategy("MA trend", overlay = true)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if fast > slow
strategy.entry("Long", strategy.long)
if fast < slow
strategy.entry("Short", strategy.short)
It compiles and produces a report, before trading costs, in which a position only closes when the opposite entry fires. Older code also adds when = ... to strategy.entry(), which v6 removed (no argument named when). A version with costs in the declaration, entries on crossovers, a stop stored at entry and an exit:
//@version=6
strategy("MA trend", overlay = true, initial_capital = 10000,
default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 2)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
atrValue = ta.atr(14)
crossUp = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
var float stopPrice = na
if crossUp and strategy.position_size == 0
stopPrice := close - 2 * atrValue
strategy.entry("Long", strategy.long)
if crossDown
strategy.close("Long")
strategy.exit("Stop", "Long", stop = stopPrice)
plot(fast, "Fast SMA", color.teal)
plot(slow, "Slow SMA", color.orange)
Set commission_value and slippage to your broker's real costs: 0.05% and 2 ticks are placeholders. Pine Check adds notes for a strategy() without commission or slippage and for entries that never exit. The guides: the Strategy Tester settings that change your results and stop loss and take profit in Pine Script.
The ten mistakes at a glance
| # | Mistake | What TradingView shows | Edgoro Pine Check |
|---|---|---|---|
| 1 | v4 and v5 names, transp, when | Could not find function or function reference / does not have an argument with the name | Error |
| 2 | Functions that don't exist (ta.sum) | Could not find function or function reference | Not caught: TradingView's compiler catches it |
| 3 | A bool set to na, na() on a bool | Cannot assign a value of the "simple na" type / Cannot call "na" | Error |
| 4 | A number used as a condition | The condition of the "if" statement must evaluate to a "bool" value | Error for price and volume series; not for variables |
| 5 | Block and wrapped-line indentation | Syntax error at input | Error |
| 6 | plot() inside an if | Cannot use "plot" in local scope | Error |
| 7 | Curly quotes, &&, chat formatting | no viable alternative at character | Error for curly quotes; not for && or fences |
| 8 | ta.* calls that skip bars | A warning: should be called on each calculation | Note |
| 9 | lookahead_on without [1] | Nothing | Note |
| 10 | Strategies without costs or exits | Nothing | Note |
The full list of messages, each with a broken and a fixed script, is in the Pine Script errors reference.
How to check AI-written Pine Script before you trust it
- Run it through Edgoro's free Pine Script checker. Paste the whole script. Edgoro Pine Check reads it against the v6 rules in your browser, lists each problem with its line and a link to the fix, and adds notes on repainting, missing costs and missing exits. It's free, needs no account and doesn't upload your code. A clean report means none of its checks failed; it isn't a compile.
- Fix what's left with the free AI Pine Script error fixer. Paste the script and the exact error message; it explains the cause and returns a corrected v6 script with a list of what changed. You get 3 free fixes a month without an account. Or fix it by hand from the error page.
- Save it in TradingView's Pine Editor. TradingView's compiler has the final word. It catches what a checker can't, such as invented functions and type errors like "series int used but simple int expected". Fix the first error first: one missing bracket can produce several messages.
- Test what it does, not only whether it compiles. Check five signals on the chart against your rules by hand. Use Bar Replay to see whether signals appear in real time where they appear on history. For a strategy, set your real commission and slippage, then read the list of trades in the Strategy Tester, not only the net profit.
Get fewer mistakes from the start
- Ask for Pine Script v6 by name, and for an
indicator()or astrategy(). - Describe the rules in numbers: "RSI(14) crosses above 30", not "RSI is oversold". The template in how to describe a trading strategy to AI covers entries, exits, filters and costs.
- Paste the exact error back, with its line number, instead of "it doesn't work".
- Choose the tool for the job. Our comparison of the best AI for Pine Script checks seven tools, Edgoro included, on price, Pine v6 support, error checks and free options.
Sources and further reading
- Migration guide to Pine Script version 6 — the v6 bool rules, lazy
and/or, and the removedwhenandtranspparameters. - Pine Script User Manual: Script structure — block indentation and line wrapping.
- Pine Script User Manual: Other timeframes and data —
request.security()and lookahead. - Pine Script User Manual: Strategies — commission, slippage and how orders fill.
- Pine Script v6 reference manual — whether a function or parameter exists.
Try it with Edgoro
Edgoro's AI Pine Script generator writes Pine Script v6 only, and runs Edgoro Pine Check on every script before you see it: the errors above go back to the AI for a fix, and the notes come with a one-click fix. TradingView still compiles the script when you save it, and you paste any message back into the same chat. To try it without an account, the free AI Pine Script generator writes 3 scripts a month.
AI-written code can contain mistakes even when it compiles and passes a checker. Test any script before relying on it; trading involves substantial risk of loss, and nothing here is investment advice.
Frequently asked questions
Why is my AI-generated Pine Script not working?
Usually because the AI mixed Pine Script versions or broke a v6 rule: v4 or v5 names such as study() or rsi() under //@version=6, a bool set to na, a number used as a condition, the wrong indentation, or plot() inside an if block. Read the first error TradingView shows, fix it from its message, and save again.
If AI-written Pine Script compiles, is it correct?
Not necessarily. A script can compile and still read future data through request.security() with lookahead_on, skip bars in ta.* calls placed after and/or, or backtest a strategy without commission and slippage. Check the signals on the chart and in Bar Replay, and set real costs before you read a Strategy Tester report.
Is ta.sum a Pine Script function?
No. The sliding sum in Pine Script v6 is math.sum(source, length). ta.sum() is a name AI assistants invent, and TradingView rejects it with Could not find function or function reference.
Does Edgoro Pine Check compile Pine Script?
No. Edgoro Pine Check is a static checker: it reads the code against the Pine Script v6 rules in your browser and flags repainting, missing costs and missing exits. TradingView doesn't offer its compiler to other sites, so the Pine Editor remains the final check.
How do I get an AI assistant to write Pine Script v6?
Ask for Pine Script v6 by name, say whether you want an indicator or a strategy, give every rule with numbers, and ask for commission and slippage in strategy(). Then run the result through a Pine Script checker, save it in the Pine Editor and paste any error back with its line number.
Want our studies and guides first when you search? Add Edgoro as a preferred source on Google.