Back to TradingView PineScript for CFD Traders
Part 1

Your first indicator: plotting a moving average

A complete walkthrough: write, save, add to chart, and tune a simple moving average from scratch.

Beginner8 min readBeginner → AdvancedLesson 04 / 17
Try it in TradingView
Two SMAs plus a trend background

Save → Add to chart. Then open the settings gear and drag Fast/Slow Length — the lines and the background shading respond live.

//@version=5
indicator("Lesson 4 - SMA 20 + 50", overlay=true)

fastLen = input.int(20, "Fast Length", minval=1)
slowLen = input.int(50, "Slow Length", minval=1)

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

plot(fast, "Fast SMA", color=color.blue, linewidth=2)
plot(slow, "Slow SMA", color=color.orange, linewidth=2)

// Shade the background green while the fast MA is above the slow one.
bgcolor(fast > slow ? color.new(color.green, 90) : na)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

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.

What you just did

Lesson 04 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.