Prompt Details
Model
(claude-5-sonnet)
Token size
2,288
Example input
[PASTE YOUR PINE SCRIPT CODE]: //@version=5 indicator("Broken SMA Signal", overlay=true) length = input.int(20, "SMA Length") average = sma(close, length) bullishSignal = crossover(close, average) plot(average, title="SMA", color=color.blue, linewidth=2) plotshape( bullishSignal, title="Bullish Signal", style=shape.triangleup, location=location.belowbar, color=color.green ) alertcondition( bullishSignal, title="Bullish Alert", message="Price crossed above the SMA" )
[DESCRIBE THE ERROR MESSAGE OR UNEXPECTED BEHAVIOR]: TradingView says “Could not find function or function reference 'sma'” when I add this indicator to the chart. Please fix it without changing what the indicator does.
Example output
## Step 1: Input and Version Audit
The script declares `//@version=5`. In Pine v5, all built-in technical-analysis functions were moved under the `ta.` namespace. Bare calls like `sma()` and `crossover()` are Pine v4 syntax and no longer exist as global functions in v5 — hence the compiler error.
Checking the rest of the script against v5 syntax: `input.int()`, `color.blue`, `color.green`, `shape.triangleup`, `location.belowbar`, `plotshape()`, and `alertcondition()` are all correctly namespaced for v5. `crossover()` on line 6 has the same problem as `sma()` but wasn't flagged by the compiler yet, since Pine reports the first unresolved reference it hits during compilation — it would surface next.
No mixed-version syntax, scope issues, indentation problems, or type-qualifier conflicts elsewhere in the script.
## Step 2: Issue Classification
| Issue | Classification |
|---|---|
| `sma()` undefined in v5 | Compile-time error |
| `crossover()` undefined in v5 | Compile-time error (latent, would trigger next) |
## Step 3: Targeted Technical Audit
This is a pure indicator with no arrays, strategy orders, or `request.security()` calls, so those checks don't apply.
- **Calculation order / warm-up:** `ta.sma(close, length)` with `length = 20` returns `na` for the first 19 bars (bar_index 0–18) and produces its first valid value at bar_index 19 — this is normal and unchanged from the original intent.
- **Alerts:** `bullishSignal` depends on `ta.crossover()`, which is confirmed/final once a bar closes but can flip on the currently open (realtime) bar until close. This is expected behavior, not a defect — the user should select "Once per bar close" if they want the alert to fire only on confirmed crossovers, or "Once per bar" if they accept it may re-evaluate intrabar. This is a setting chosen at alert-creation time in TradingView's UI, not something fixable in the script.
## Step 4: Root Cause
**Confirmed:** The script is declared as Pine v5 but uses Pine v4 bare function names (`sma`, `crossover`) that don't exist in the v5 namespace. The fix is to qualify both with `ta.`.
**Probable (needs your confirmation in TradingView):** None beyond the above — this is a straightforward namespace fix, not ambiguous.
**Unrelated issues:** None found.
## Step 5: Corrected Script
```pinescript
//@version=5
indicator("Broken SMA Signal", overlay=true)
length = input.int(20, "SMA Length")
average = ta.sma(close, length) // FIXED: v5 requires ta. namespace
bullishSignal = ta.crossover(close, average) // FIXED: v5 requires ta. namespace
plot(average, title="SMA", color=color.blue, linewidth=2)
plotshape(
bullishSignal,
title="Bullish Signal",
style=shape.triangleup,
location=location.belowbar,
color=color.green
)
alertcondition(
bullishSignal,
title="Bullish Alert",
message="Price crossed above the SMA"
)
```
This correction is based on static inspection of the code and Pine v5's documented namespace requirements — I have not compiled or run this in TradingView. Please validate it there before relying on it.
## Step 6: Change Log
| Original problem | Correction | Why it works | Behavior changed? |
|---|---|---|---|
| `sma()` not found | `ta.sma()` | v5 moved all built-in TA functions under `ta.` | No — identical calculation, same output |
| `crossover()` would fail next | `ta.crossover()` | Same v5 namespace requirement | No — identical calculation, same output |
Nothing about the indicator's logic, plots, alert condition, or settings was altered — only the function references were re-namespaced.
## Step 7: Behavior and Timing Explanation
- **Warm-up:** With `length = 20`, `average` is `na` for bar_index 0–18 (the first 20 bars, zero-indexed) and produces its first real value at bar_index 19. No plot or signal appears before that.
- **Historical vs. realtime:** `ta.crossover()` compares the current and previous bar's `close` vs. `average`. On historical (closed) bars this is stable. On the currently forming realtime bar, `close` is still moving, so `bullishSignal` can turn true and then false again before the bar closes — that's expected recalculation, not repainting of past bars.
- **Alert frequency:** Since `bullishSignal` can flicker intrabar, choose "Once per bar close" in the alert dialog if you only want confirmed crossovers to notify you.
## Step 8: TradingView Validation Checklist
- Confirm the script compiles cleanly in the Pine Editor
- Add to chart and confirm no runtime errors appear
- Visually compare the SMA plot and triangle markers against the original intended behavior
- Manually check the SMA value against a couple of bars (e.g., avg of last 20 closes)
- Confirm no plot/signal appears before bar_index 19
- Watch behavior on an open bar vs. after it closes to confirm the crossover signal stabilizes
- Set up the alert and verify it fires with your chosen frequency ("Once per bar" vs. "Once per bar close")
- Test on a different symbol/timeframe to confirm nothing else was namespace-dependent
## Step 9: Prevention
1. When starting a v5 (or v6) script, always prefix built-in technical-analysis functions with `ta.` from the outset rather than porting v4 code directly.
2. Keep a quick reference of the v4→v5 namespace migrations (`ta.`, `math.`, `array.`, `str.`, `color.`, etc.) handy when upgrading old scripts.
3. Compile after writing each function block rather than the whole script at once — this catches namespace errors immediately instead of stacking multiple latent errors.
4. When copying snippets from older forum posts or tutorials, check the `//@version` they were written for before pasting into a newer-version script.
5. Use TradingView's built-in autocomplete in the Pine Editor — it only suggests valid namespaced functions for the declared version, which prevents this class of error.
Get this prompt + 9 more with Select$14 your first month, then $19/mo$14 first month, then $19/mo
View SelectBy purchasing this prompt, you agree to our terms of service
CLAUDE-5-SONNET
Debug broken TradingView Pine Script systematically. This prompt audits the Pine version, classifies compiler, runtime, logic, strategy, and repainting problems, identifies the root cause, and returns a complete corrected script with marked changes and a plain-English explanation. It works with indicators, strategies, libraries, and alerts while preserving the original logic wherever possible. Ideal for fixing AI-generated or legacy Pine Script.
...more
Added over 1 month ago
