This document aims to provide information that will be useful for newcomers to the Pine Script programming language. Pine Script is the programming language used on the TradingView charting platform.
There are many resources to learn Pine. These are the most important. While you will not begin by reading the Reference Manual, it’s important to know where it is. The Quickstart Guide is a good place to start and branch out to key areas of the User Manual.
pine introduction tradingview
will also turn up a few videos.Pine is a specialized language used to write scripts that can take two very different forms: studies (a.k.a. indicators, as we will name them) or backtesting strategies. Indicators are used to show graphic information on a chart or in an indicator Pane. If you wish to write a MACD indicator in Pine, you do that by creating a script using the study()
declaration statement at the beginning of the script. Strategies use the strategy()
declaration statement and can display visual information on charts or in panes in the same way an indicator would, but they also contain additional Pine statements to simulate trades in order to run backtests.
If you want to design a trading system that trades on MACD setups, you may write a strategy to test it, and then convert it to an indicator to generate alerts in order to discretionary trade on them, or send them to a third-party execution bot for relaying orders to markets. Be sure to look at the PineCoders Backtesting and Trading Engine if this is your objective.
Because it is specialized, Pine is very powerful. You can write two lines of Pine to do what could take hundreds in other languages. The same specialization that makes Pine powerful also implies a high abstraction level; until you understand a few key concepts about Pine and its runtime environment, it will be difficult to make sense of Pine code.
There are 4 supported versions of Pine, from 1 to 4. A compiler directive used in the script’s first line will tell you what version of Pine the script is written for (when no such directive is present, v1 is the default used). It is important to know which version the code you will be writing or studying is, as keywords and programming techniques can vary quite a bit between versions.
Pine scripts run directly in TradingView chart Editor:
Step 1: Go to https://www.tradingview.com/
Step 2: Select “Chart” button
Step 3: Open the Pine Editor pane by clicking on the “Pine Editor” tab at the bottom of the chart.
Pine indicator code executes once for each bar of the dataset, starting from the beginning of the chart’s history. When the realtime bar is reached, indicator scripts execute every time price changes, while strategy scripts may be configured to behave like indicators, or to run only at the close of the realtime bar (the default). See here for more information.
The main data type used in Pine scripts is called a series. It is a continuous list of values that stretches back in time from the current bar and where one value exists for each bar. While this structure may remind many of an array, a Pine series is totally different and thinking in terms of arrays will be detrimental to understanding this key Pine concept. You can read about series here and get more information on how to use them here.
— quoted from the Pine v4 documentation
If you are new to programming, then you have a double learning curve to go through: learn to program and learn Pine. You will need to do your homework and spend the countless hours required to become able to convert your trading ideas into working code.
Either way, the most productive way to learn is always to start playing with real code early. Start with the examples in the next section. Make slight changes to the code and see what impact they have, and you’ll be on your way.
If you already have programming experience, learning Pine is mostly about becoming proficient in manipulating series, and then understanding the abstractions, the runtime environment and the typing and runtime limitations, as the language itself is straightforward.
Whether you are new to programming or a veteran, the PineCoders Pine Coding Conventions will provide useful coding guidelines you can choose to adopt.
Here are a few methods for troubleshooting pine-related issues:
site:tradingview.com intitle:indicatorname
for better results.plot(var)
, or plotchar(var, "var description", "")
to show the value in the indicator values and in the Data Window (look it up; it’s real useful) without plotting anything in your indicator’s space. PineCoders has a section on debugging techniques.The TradingView platform does not run indicators written for other platforms. They will require conversion in Pine before they can run on TradingView charts.