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
- On TradingView, open the Pine Editor, create a new indicator, and type the three-line script.
- Save it as My first line and add it to a 5-minute MES1! chart.
- Then change 20 to 50, save again, and watch the line change.
Quick check
1. You save a script and nothing changes on the chart. What should you check first?
2. What does overlay = true do?
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
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 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.
- Our first script: //@version=6, then indicator("My first line", overlay = true), then plot(ta.sma(close, 20)). It draws a 20-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 A moving average where every close in the window counts the same: add them up and divide by how many there are.More in the glossary. 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 numbersYour first script, line by line
Three lines that draw a 20-candle simple moving average
- //@version=6: the language version
- indicator("My first line", overlay = true): an indicator, drawn on the price chart
- 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
DrillMade-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.