What we are building
A two-line indicator that overlays a 20-period simple moving average on the chart. Once this works, you have proven the full toolchain — editor, save, add to chart, tune parameters.
The code
//@version=5
indicator("My first SMA", overlay=true)
length = input.int(20, "Length", minval=1)
plot(ta.sma(close, length))
Line by line
//@version=5 — the version directive. Without this Pine defaults to v5 but the directive makes intent explicit.
indicator("My first SMA", overlay=true) — declares this is an indicator (not a strategy) and asks TradingView to overlay it on the price chart rather than put it in a separate pane.
length = input.int(20, "Length", minval=1) — adds an integer input named "Length" with default 20 and minimum 1. Inputs become parameters in the indicator's settings dialog.
plot(ta.sma(close, length)) — computes the simple moving average using the built-in ta.sma function and plots it.
Running it
Click Save (give it any name), then Add to chart. A blue line should appear over your candles. Open the indicator settings (gear icon next to the indicator name) and change Length to 50, then 200 — the line should respond.
Adding a second line
Let's add a 50-period SMA in a different colour. The full script:
//@version=5
indicator("SMA 20 + 50", overlay=true)
fastLen = input.int(20, "Fast Length", minval=1)
slowLen = input.int(50, "Slow Length", minval=1)
plot(ta.sma(close, fastLen), color=color.blue, linewidth=2)
plot(ta.sma(close, slowLen), color=color.orange, linewidth=2)
What to notice
Both lines are series-aware — Pine does not loop over bars. The chart updates instantly when you change a parameter. If the chart freezes or shows a red error message at the top of the editor, your syntax has a problem; the error is shown above the offending line.
Common first errors
Forgetting the //@version=5 line. Using input without specifying a type (input.int instead). Mixing overlay and non-overlay indicators — if your plot shows up in a separate pane when you wanted overlay, you forgot overlay=true.