StructureX

Home › Guides › Platforms

Pine Script for beginners: build your first indicator in 3 lines

Pine Script is the small coding language behind TradingView indicators. You don't need to be a programmer. 3 short lines put your own indicator on a chart.

Beginner guide · 8-minute read · Updated 2026-10-21

What Pine Script is

Pine Script is TradingView's own coding language. It's made for one job: drawing things on price charts and testing ideas on them.

Every indicator you add from the Indicators menu is a Pine Script program. When you write your own, it works the same way — it shows up on your chart, it has settings, and it can fire alerts.

The current versions are version 5 and version 6. New scripts should use version 6. Older scripts still run.

You need a computer for this

You write Pine Script in the Pine Editor, and the Pine Editor is only on the computer — in your web browser or TradingView's desktop app. The phone app has no Pine Editor.

The good news: once you save a script on a computer, it's saved to your account. You can then add it to charts in the phone app too.

Step 1: Open the Pine Editor

  1. Go to tradingview.com on a computer and open any chart.
  2. Find Pine Editor. On newer layouts it opens from the panel on the right side of the screen. On older layouts it's a tab along the bottom of the chart.
  3. The editor opens next to the chart, so you can see both.

TradingView moves buttons from time to time. If you can't see it, look for "Pine Editor" in the chart's panels or menus.

Step 2: Make a new indicator

  1. In the Pine Editor, open its menu (often the script name at the top of the editor) and choose to create a new Indicator.
  2. The editor fills in a small starter script. You can keep it or clear it.

A new script always starts with a line that says which version it uses, then a line that names the indicator.

Step 3: Write three lines

Here's a whole, working indicator. It draws a 20-candle moving average — the average closing price of the last 20 candles — on top of your chart. Type each line on its own line in the editor:

//@version=6
indicator("My first average", overlay=true)
plot(ta.sma(close, 20), color=color.orange)

What each line does, in plain words:

Step 4: Save and add to chart

  1. Click Save and give the script a name.
  2. Click Add to chart.
  3. An orange line appears on your chart.

If there's a typo, the editor shows a red error message at the bottom, with the line number. Check commas, brackets and quote marks — those cause most beginner errors.

Your saved scripts live under My scripts in the Indicators menu. That's how you add them to other charts, including in the phone app.

Step 5: Change the color or the number

Try small changes, one at a time:

Each time you save, the chart updates. This "change one thing, look, repeat" loop is how most people learn Pine Script.

Step 6: Turn the number into a setting

Instead of typing a new number into the code each time, you can make it a setting with an input.

Replace line 3 with these two lines:

length = input.int(20, "Length")
plot(ta.sma(close, length), color=color.orange)

Save it. Now open the indicator's settings on the chart (the gear icon next to its name). Under Inputs there's a "Length" box. You can change it there, without touching the code.

Step 7: Add an alert condition

Pine Script can tell TradingView when something happens, so you can get an alert. Add this line at the bottom:

alertcondition(ta.crossover(close, ta.sma(close, length)), "Close above average", "Price closed above the average")

In plain words: "when the closing price crosses above the average, this condition is true." The two pieces of text are the alert's title and its message.

Then:

  1. Save the script.
  2. Open TradingView's Alerts panel and create a new alert.
  3. For the condition, choose your indicator, then "Close above average".
  4. Pick how you want to be told — for example, a pop-up or a phone notification.

An alert is just a tap on the shoulder. It tells you to look. It doesn't decide anything for you.

An indicator is not a trading plan. A line crossing another line doesn't tell you where your stop goes, how big to trade or whether the bigger chart agrees. Code can show you information. You still make the decision.

Test before you trust it

Before using any script — yours or someone else's — test it on past charts. TradingView's bar replay lets you step through old price moves and see how the indicator behaves, one candle at a time. Log what you see. See how to backtest a trading strategy.

Learning from real code

Small scripts teach the basics. Reading a bigger, finished indicator teaches how the pieces fit. TradingView's own Pine Script user manual is the place to look up any function.

If you want a full indicator to study, our course includes the complete source code of the StructureX chart tool — the same code behind the free version — to learn from and customize: change its settings, add alerts, or build on it. It's source code, not a ready-made trading bot.

Common beginner mistakes

Keep a copy. Before you edit a script that works, save a copy with a new name. Then you can always go back.

Questions

Is Pine Script hard to learn?

It's one of the easier coding languages to start with, because it does one job: charts. A working indicator can be three lines long.

Can I use Pine Script on the TradingView mobile app?

Not to write code — the phone app has no Pine Editor. Write and save scripts on a computer, then add them to charts in the phone app from My scripts.

What is the latest version of Pine Script?

Version 6 is the current version. Version 5 scripts still run, and the Pine Editor can help convert older scripts.

Can Pine Script place trades automatically?

Pine Script draws indicators, tests strategies on past data and fires alerts. Placing real orders needs a broker connection or other tools, and any automated idea should be tested carefully first.

Want the next step? We'll walk you through it.

Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.

Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytime

Or see the whole method, step by step:

See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)

One chart idea a day, in plain English. Follow along:

Follow on XFollow on TradingView