Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

What Pine Script is

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

Advanced · Coding strategies in Pine Script

What Pine Script is

Wildflower Trading School · Lesson 7.4.1.1

Wildflower

In Level 4 you tested strategies by hand, one candle at a time, and you met Pine Script briefly along the way. This course teaches you to write it yourself. By the end of this lesson, you'll know what Pine Script is and the three kinds of script it writes. You'll also see how a script runs on a chart, one candle at a time. You'll also know what a script can't do, which matters as much.

1 of 9

What you'll learn

  • Read a script as exact rules. Pine Script is TradingView's language for charts.
  • Tell the script kinds apart. An indicator draws. A strategy also places test trades.
  • Follow a script candle by candle. It runs once on each candle, oldest to newest.
  • Watch the live candle. Its result can change until the candle closes.
  • Spot look-ahead bias in code. Code can use prices it shouldn't have had yet.

Exercise

  1. Open any chart on TradingView.
  2. Hover over the name of a built-in you've added, and look for the curly brackets icon, {}, which opens its source code.
  3. Read the first two lines and find the version number and the indicator() line.

Next: The Pine Editor and your first script.

Quick check

  1. 1. What can a strategy script do that an indicator script cannot?

  2. 2. In what order does a script run on a chart?

  3. 3. Why does a 20-candle average have no value on the first 19 candles?

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 “The Pine Editor and your first script” is waiting next time.

Read the full notes

Pine Script is TradingView's own programming language: a script is a list of exact instructions that runs once on every candle, from the oldest to the newest, to draw indicators or place test trades.

  • is TradingView's own programming language for working with price charts. A script is a list of exact instructions the chart follows. A computer applies the same rules to every , as you saw in Hand testing versus computer testing. Other platforms have their own languages. More: What Pine Script is.
  • Three kinds of script: an calculates and draws; a strategy can also place test trades, which TradingView counts in the ; a library shares code between programmers.
  • A script runs once on every candle, from the oldest on the left to the newest on the right, and remembers earlier results. A 20-candle has no value until candle 20.
  • On a live chart, an indicator can run again with each price change on the candle still forming, so its result can change until the candle closes. That's where repainting comes from.
  • A script can't see future candles, but code can be written so it uses prices it shouldn't have had yet: in code. A test is only as honest as its rules. In this course, strategies place test trades only.

Worked example

Made-up numbers

A 3-candle average, one candle at a time

Made-up closes: 5,000 · 5,002 · 5,004 · 5,003 · 5,006

  1. Candle 3: (5,000 + 5,002 + 5,004) ÷ 3 = 5,002
  2. Candle 4: (5,002 + 5,004 + 5,003) ÷ 3 = 5,003
  3. Candle 5: (5,004 + 5,003 + 5,006) ÷ 3 = 5,004.33
  4. Candles 1 and 2: no value yet

Each answer uses only closes the script has already seen.

Your turn

Drill

Made-up closes · candle 6 closes at 5,008 · candles 4 and 5 closed at 5,003 and 5,006

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.