Script structure and the version line
Lesson 1 · about 10 min
Pine Script is the small language TradingView runs on every bar of your chart. You do not install anything. The editor is a tab at the bottom of any chart, the script runs on TradingView's servers, and the output appears on the chart a second after you press "Add to chart". That fast loop is the whole reason to learn it: an idea becomes a plot you can look at, then an alert, then a backtest, without leaving the chart.
Where the editor lives
Open any chart and click "Pine Editor" in the bottom panel. You get a text area, an "Open" menu with your saved scripts, "Save", "Add to chart" and a console that shows compile errors. Scripts are saved to your TradingView account, not your computer, so they follow you between machines.
Every script starts from the same skeleton. Type this in and add it to the chart:
//@version=6
indicator("My first script", shorttitle="First", overlay=true)
plot(close)
A line drawn through every closing price appears on top of the candles. That is a complete, working Pine script.
The three parts of every script
1. The version annotation. //@version=6 looks like a comment, but the compiler reads it. It tells TradingView which version of the language you are writing. Pine has changed a lot between versions: function names, how na behaves, what a boolean can hold. Every script in this course uses version 6, which is the current one. If you copy an old script from the community and it fails to compile, the first thing to check is this line.
2. The declaration statement. Exactly one of indicator(), strategy() or library() must appear, and it is normally the first real line. It names the script and sets chart-level options like whether it draws on the price pane or in its own pane below. We cover the choice between indicator and strategy in the next lesson.
3. The body. Everything else: calculations, plots, alerts, orders. Statements run top to bottom, once per bar, and the script has no "main" function or loop of its own. TradingView provides the loop.
How a bar is processed
This execution model is the thing that trips people coming from other languages. Your script is not run once. It runs once for every historical bar on the chart, from the oldest to the newest, and then again on every price update of the live bar. On each run, close means the close of the bar being processed, close[1] means the bar before it, and plot() draws one point.
A 5,000-bar chart therefore runs your script 5,000 times before you see anything, and each run only knows about its own bar and the bars before it. It cannot see the future, and it cannot see the "whole array" of closes the way a Python script would. That constraint is what makes Pine both simple and occasionally frustrating.
Key idea: A Pine script is a function of one bar. TradingView calls it once per bar from left to right;
closeis this bar's close andclose[1]is the previous one.
Syntax rules you will meet in the first hour
- Statements end at the end of the line. There are no semicolons.
- Comments begin with
//. - Blocks (the body of
if, functions, loops) are defined by indentation of four spaces, like Python. Mixed tabs and spaces cause "Mismatched input" errors. - Names are case sensitive:
Closeis notclose. - Long function calls can be split over lines by indenting the continuation with a number of spaces that is not a multiple of four (five is the convention), so the compiler does not read it as a new block.
//@version=6
indicator("Long call example", shorttitle="Long",
overlay=true, max_labels_count=100)
plot(close, "Close", color=color.gray)
Named arguments
Most built-in functions take a mix of positional and named arguments. plot(close) passes the series positionally. plot(close, "Close", color=color.gray) passes the title positionally too, then names color. Once you start naming arguments you must name everything after them. Naming makes scripts readable and lets you skip optional arguments you do not care about, so the course names almost everything except the first argument.
Reading the reference
Hover over any built-in in the editor and a tooltip shows its signature. The full reference manual is one click away from the editor's "help" icon, and it is the single most useful document for Pine: every function, its arguments, their types and a short example. When a lesson here mentions a function you have not seen, look it up there before searching the web, because community answers are often for old versions.
Try it: Change
overlay=truetooverlay=false, add to chart again, and see the line move to its own pane. Then delete the version line and read the error you get. Put it back. Finally, changeclosetovolumeand notice why overlay matters for a series on a different scale.
Recap
- The editor is a panel under any chart; scripts are saved to your account and run on TradingView's servers.
- Every script has a version annotation, one declaration statement and a body.
- The body runs once per bar, oldest to newest, then on every update of the live bar.
- Indentation defines blocks, names are case sensitive, and continuation lines are indented by five spaces.
- The reference manual, reachable from the editor, is the primary source; community answers are often for older versions.
See it drawn
Original diagrams for the ideas on this page. Illustrative, not real market data.