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
- Open any chart on TradingView.
- Hover over the name of a built-in The average of the last few closing prices, worked out again on every candle and drawn as a line.More in the glossary you've added, and look for the curly brackets icon, {}, which opens its source code.
- Read the first two lines and find the version number and the indicator() line.
Quick check
1. What can a strategy script do that an indicator script cannot?
2. In what order does a script run on a chart?
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
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.
- TradingView’s own programming language, used to write the indicators and strategies that run on its charts.More in the glossary 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 One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary, 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 A line or set of bars worked out from a market’s prices or volume and drawn on or under the chart, such as a moving average.More in the glossary calculates and draws; a strategy can also place test trades, which TradingView counts in the TradingView’s panel, which its newer pages call the Strategy Report, that lists a strategy script’s test trades and adds up results such as total profit or loss and drawdown. The results are hypothetical.More in the glossary; 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 The average of the last few closing prices, worked out again on every candle and drawn as a line.More in the glossary 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: A backtest mistake: using a price the strategy couldn’t have known yet at the moment it made its decision.More in the glossary in code. A test is only as honest as its rules. In this course, strategies place test trades only.
Worked example
Made-up numbersA 3-candle average, one candle at a time
Made-up closes: 5,000 · 5,002 · 5,004 · 5,003 · 5,006
- Candle 3: (5,000 + 5,002 + 5,004) ÷ 3 = 5,002
- Candle 4: (5,002 + 5,004 + 5,003) ÷ 3 = 5,003
- Candle 5: (5,004 + 5,003 + 5,006) ÷ 3 = 5,004.33
- Candles 1 and 2: no value yet
Each answer uses only closes the script has already seen.
Your turn
DrillMade-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.