What is Pine Script?
Pine Script is a language built for one job: turning price and volume data into calculations and drawings on a TradingView chart. TradingView describes it as lightweight by design, meant for developing indicators and strategies that can then be backtested, and says its goal is to keep it accessible to the broadest possible audience. It is not a general-purpose language like Python or JavaScript: it is cloud-based, and scripts run on TradingView’s servers, not on your computer.
You write it in the Pine Editor, a tab at the bottom of the chart. Saving a script stores it on TradingView’s servers under your account, and adding it to a chart runs it there. The current version at the time of writing is version 6, and a script states its version on its first line so the compiler knows which rules to apply.
- Pine Script
- TradingView’s programming language for writing indicators, strategies and libraries. Scripts run on TradingView’s servers and display their results on charts.
- Pine Editor
- The code editor built into TradingView, opened from a tab at the bottom of the chart. It is where scripts are written, saved to your account and added to the chart.
Indicator, strategy or library: what kinds of Pine scripts are there?
Every script declares what it is, with exactly one declaration statement. There are three kinds, summarized in the drawing below and in the list that follows.
The difference between the first two matters to every user. An indicator shows you information and leaves the decision to you. A strategy also places hypothetical orders according to its rules and reports how they would have done on past data, which is useful and easy to misread, as the section on limits explains. The best TradingView indicators covers the built-in indicators worth knowing.
- Indicator, declared with indicator(): performs calculations and shows them as plots, drawings or alerts. It is the most common kind of script; the built-in RSI is an example.
- Strategy, declared with strategy(): simulates orders and trades on the chart’s data so that a trading system can be backtested and forward tested. It adds a strategy report with the hypothetical results.
- Library, declared with library(): a collection of reusable functions that other scripts import. It is a tool for programmers, not something you add to a chart for signals.
- Indicator (Pine Script)
- A script declared with indicator() that performs calculations on chart data and displays them as plots, drawings or alerts. It does not simulate trades.
- Strategy (Pine Script)
- A script declared with strategy() that simulates orders and trades on historical and live bars and reports the hypothetical results. It is used for backtesting and forward testing.
- Library (Pine Script)
- A script declared with library() that exports reusable functions and other code for indicators, strategies and other libraries to import.
What does a Pine Script look like? A four-line example
The script in the drawing at the top of the page is complete: paste it into the Pine Editor, save it and click Add to chart, and a 20-bar simple moving average appears on the price chart. The list below goes through it line by line.
Two ideas in it carry over to every script. First, the script does not loop through the chart itself: TradingView runs the whole script once on every bar, from the oldest to the newest, and close means the closing price of the bar being processed. Second, functions with a prefix such as ta. come ready-made, so one call replaces the arithmetic. What the average is used for is covered in moving averages.
- Line 1, //@version=6, is a compiler annotation. It tells TradingView that the script is written in version 6 of the language.
- Line 2, indicator("Simple MA", overlay = true), is the declaration. The script is an indicator named Simple MA, and overlay = true draws it on the price chart instead of in a separate pane below.
- Line 3, length = input.int(20, "Length", minval = 1), is an input. It creates a whole-number setting labeled Length, with a default of 20 and a minimum of 1, and stores the chosen value under the name length.
- Line 4, plot(ta.sma(close, length), color = color.blue), is the calculation and the output. ta.sma() computes the simple moving average of the closing price over length bars, and plot() draws it as a blue line.
How can a non-programmer read a script’s inputs?
You can learn a lot about a script without reading a line of code. Double-click its name on the chart, or hover over the name and click the Settings button, and the settings window opens. Its Inputs tab lists every setting the author chose to make editable; the Style tab controls colors and line styles; the Visibility tab sets the timeframes on which the script appears.
Each field in that window comes from one input function in the code. In the example above, input.int(20, "Length", minval = 1) is why the Inputs tab shows a field called Length filled with 20. Change it to 50 and the script runs again on the whole chart with the new value. Settle these choices before you create alerts, as the best TradingView settings suggests. What to look for:
- Lengths and periods: how many bars each calculation looks back.
- Sources: which price the script reads, usually the close.
- Timeframes: whether it pulls data from a higher timeframe, which matters for repainting.
- Toggles: which parts of the display, or which signals, can be switched off.
- The description on the script’s page: what it does and how to use it, which is all you have when the code is hidden.
- Script inputs
- The settings a script’s author makes editable, shown on the Inputs tab of the script’s settings window. Each is created by an input function in the code and has a default value the user can change.
Open-source, protected and invite-only scripts: what is the difference?
When an author publishes a script, they choose who can see the code and who can use it. TradingView has three visibility types, shown in the drawing below.
Invite-only scripts are the only published scripts for which an author may charge; open-source and protected scripts are free to use by definition, and TradingView states that it takes no part in those sales. UpTickFlow, the indicator this site publishes, is an invite-only script; it is described on the indicator page. The steps for adding one are in how to add an invite-only script.
Hidden code is not a warning sign in itself, but it changes what you can check. With an open-source script you can read exactly what it calculates. With a closed one you rely on the description, the inputs and your own testing, so test before you trust it.
- Open-source: anyone can read the code and add the script to a chart. TradingView says about half of its more than 150,000 community scripts are open-source.
- Protected: the code is hidden from everyone but the author, yet anyone can add the script and use it freely. Only authors on paid plans can publish this way.
- Invite-only: the code is hidden, and only users the author has granted access to can add the script. Publishing this way requires a Premium or higher-tier plan.
- Open-source script
- A published TradingView script whose Pine Script code anyone can read. It can be studied, checked and, within TradingView’s reuse rules, built upon.
- Protected script
- A published script whose code is hidden from everyone except its author, but which any user can add to a chart and use for free.
- Invite-only script
- A published script whose code is hidden and which only users approved by the author can add to a chart. It is the only kind of script an author may charge for.
Can a Pine script send alerts?
Yes, in two steps. The script’s code defines alert events: alertcondition() in indicators, alert() in indicators and strategies, and order fills in strategies. No code can switch an alert on by itself, though; you create the alert from the chart, choosing one of those events as the condition. From then on it runs on TradingView’s servers, whether or not you are logged in.
Two details catch people out. Alerts trigger only on the realtime bar, never on history. And when you create one, TradingView saves a copy of the script and its inputs with the chart’s symbol and timeframe, so later changes to the settings do not reach a running alert: you delete it and create a new one. How to set alerts on TradingView walks through the dialog and the trigger options.
What are the limits of Pine Script?
Pine Script is deliberately narrow, and knowing its edges prevents most misunderstandings.
- It runs on TradingView’s servers, on the bars of the chart. Because every script uses shared computing resources, TradingView limits execution time, memory, script size and the data a script can request. A script can use at most 64 plot counts, for example, and the number of historical bars it sees depends on the plan, from 5,000 up to 40,000.
- It behaves differently on the open bar. On historical bars an indicator runs once per bar, on final prices. On the realtime bar it runs again on every price update, so its values can change until the bar closes, as the drawing below shows.
- That difference is the root of repainting. TradingView’s documentation defines repainting as script behavior that makes historical and realtime calculations or plots behave differently, and estimates that more than 95% of indicators show some form of it, much of it harmless. Repainting on TradingView explains which forms mislead and how to test for them.
- A strategy’s results are a simulation. Its orders are hypothetical: they are filled by TradingView’s emulator from the chart’s own data, not in a market. A good-looking report is a starting point for testing, not proof; see how to backtest a trading strategy and backtest overfitting.
- It does not trade for you. A script calculates, draws and raises alert events; what you do with them is your decision.
- Historical bar
- A closed bar on the chart, whose prices are final. An indicator runs once on each historical bar.
- Realtime bar
- The bar that is still forming at the right edge of the chart. A script runs again on each price update of this bar, so its results can change until the bar closes.
Do you need to learn Pine Script?
Most traders never write a line of it. The built-in indicators and the community scripts cover the common tools, and reading inputs is enough to use them well. Learning the basics pays off in two situations: when you want to check what an open-source script really does, and when you want to test a rule of your own instead of taking someone’s word for it.
If you do start, TradingView’s own advice is to read good code first: the built-in indicators, whose source opens from the curly-braces icon next to their name, and the open-source scripts picked by its editors. Then change one thing at a time and watch the chart. And practice on a paper trading account before any rule you code touches real money.