Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

The Pine Editor and your first script

Lesson 2 of 16 in Coding strategies in Pine ScriptModule 1: Pine Script basics

Advanced · Coding strategies in Pine Script

The Pine Editor and your first script

Wildflower Trading School · Lesson 7.4.1.2

Wildflower

Last time you saw what a Pine Script looks like. Now you're going to write one. By the end of this lesson, you'll know how to open the Pine Editor on TradingView and start a new script. Then you'll save it and add it to your chart. You'll also know how to read a script someone else wrote, including TradingView's own.

1 of 7

What you'll learn

  • Open the Pine Editor. Choose Create new, then Indicator, to get a starter script.
  • Save and add it to a chart. Save it with a name, then click Add to chart.
  • Write your first script. Three lines draw a 20-candle simple moving average.
  • Fix errors fast. The editor points at the broken line. Check brackets first.
  • Read built-in code. The curly brackets icon opens it. Make a working copy to edit.

Exercise

  1. On TradingView, open the Pine Editor, create a new indicator, and type the three-line script.
  2. Save it as My first line and add it to a 5-minute MES1! chart.
  3. Then change 20 to 50, save again, and watch the line change.

Next: Variables, numbers and true or false.

Quick check

  1. 1. You save a script and nothing changes on the chart. What should you check first?

  2. 2. What does overlay = true do?

  3. 3. A built-in indicator's code opens read-only. How do you get a copy you can change?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Variables, numbers and true or false” is waiting next time.

Read the full notes

To write your first Pine Script, open the Pine Editor on a TradingView chart, choose Create new and Indicator, type the script, save it with a name and click Add to chart.

  • On TradingView, open the Pine Editor: a Pine button on the right-hand toolbar on newer layouts, or a tab under the chart on older ones. Click the arrow beside the script's name, then Create new and Indicator. A short starter script appears.
  • Type your script, then save with Ctrl+S (or Cmd+S on a Mac), or click Save, and give it a name. Scripts are saved to your TradingView account. Click Add to chart to run it. Its name appears at the top left of the chart, like any .
  • Our first script: //@version=6, then indicator("My first line", overlay = true), then plot(ta.sma(close, 20)). It draws a 20- . See What Pine Script is.
  • If you mistype, the script doesn't run: the editor shows an error message pointing at the line, and the chart keeps the last version that worked. Check brackets first.
  • To read a built-in indicator's code, hover over its name on the chart and click the curly brackets icon. It opens read-only. A line using input() puts a setting in the indicator's settings box; Create a working copy makes your own editable copy. Other platforms have their own editors and languages.

Worked example

Made-up numbers

Your first script, line by line

Three lines that draw a 20-candle simple moving average

  1. //@version=6: the language version
  2. indicator("My first line", overlay = true): an indicator, drawn on the price chart
  3. plot(ta.sma(close, 20)): average the last 20 closes and draw the line

Saved to your account, added to the chart, and worked out on every candle from left to right.

Your turn

Drill

Made-up example · plot(ta.sma(close, 20)) · on a 5-minute chart

Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.