Trading-View Basic


TradingView has designed its own scripting language called Pine Script. It allows users to create custom indicators and run them on our servers. Pine was designed as a lightweight language focused on the specific task of developing indicators and strategies. Most of TradingView’s built-in indicators have been coded in Pine. It is our explicit goal to keep Pine accessible and easy to understand for the broadest possible audience.

Pine is cloud-based and therefore different in nature to client-side programming languages. While we will not develop Pine into a full-fledged language with high-end coding capabilities for building very complex tools, constantly improving Pine is one of our highest priorities and we are happy to consider any requests for new features.

Because each script uses computational resources in the cloud, we must impose limits in order to share these resources fairly among our users. We strive to impose as few limits as possible while enforcing as many as needed. We must ensure the platform keeps running smoothly so nobody is negatively affected by scripts that consume a disproportionate amount of resources. The imposed limits apply to elements such as the amount of data from additional symbols, execution time, memory usage and script size. Additionally, we keep Pine syntax and semantics simple so it can handle common tasks efficiently.

We will continue to improve Pine’s documentation and support to ensure that anyone who wishes to learn and use Pine may do so, as this helps grow the Pine development community and provides TradingView users with more robust and helpful tools.

Quickstart guide

A script written in Pine is composed of functions and variables. Functions contain instructions that describe the required calculations. Variables save the values used or created during those calculations.

A script must contain a study or strategy annotation which defines the script’s name and other properties. The script’s body contains the functions and variables necessary to calculate results which will be rendered on a chart with a plot function, or some other function that plots the script’s output.

Introduction to Pine Script

Pine Script is the name of the programming language utilized by TradingView’s proprietary HTML5 charting software (which I highly recommend you try if you haven’t already — you’ll love it).

It is a cloud-based lightweight scripting language which is not very complicated or sophisticated but still allows you to do some powerful things with your charts, such as create or customize indicators and tools, backtest strategies and (my personal favorite) — create trading signal alerts.

Description: Complex PineScript ExampleExample of an Advanced TradingView Script

You do not need to have prior programming experience in order to use Pine Script, although it certainly helps. I have over 10 years’ experience in coding software and computer games from my past life before I became a trader, and I must admit, it has been a very useful skill to have.

As traders, we must do everything we can to maintain our edge in the markets. Using code and scripts for automation is one way of dramatically enhancing your edge.

With all the tools I have created over the years it feels like I have a second pair of eyes on the market at all times – which means there’s not as much pressure to keep my own on them all the time.

What Are the Advantages of Pine Script?

Before I begin my Pine Script lessons, here is a list of the benefits you will get from learning how to code:

  • Reduce your required screen time
  • Reduce anxiety over missing setups when you are busy
  • Systematize your setups into an objective rules-based process
  • Dramatically improve backtesting efficiency and speed
  • Test strategies over hundreds of trades in seconds with the click of a button
  • Enhance or customize existing indicators to suit your needs
  • Detect large spikes in volume or ATR (useful for stocks and crypto)
  • Create custom indicators and oscillators
  • Paint useful information like Stop Loss and Take Profit directly to your chart
  • Automate parts of your trading process or analysis
  • Create trade management tools (eg. Trailing stop loss calculation tools)
  • Turn existing indicators into alerts (eg. Whenever RSI goes overbought)
  • Gain a better understanding of the mechanics of price action
  • Gain a better understanding of how automated trading works
  • Find comfort in knowing that you have a robot on your side

If you are entirely new to programming and have never written a line of code in your life, then you may want to learn some of the absolute basics first.

I will do my best to explain the concepts to you, but if I miss something or what I say does not make sense, then try searching for some basic programming courses.

A great website to learn the fundamentals . I would recommend starting with the Python tutorial because Python is a popular programming language for traders and trading systems (and is very similar to Pine Script in its style).

W3Schools have a great free course on Python and other popular programming languages which you can find by clicking here.

You really have nothing to lose by learning to code. In the lessons to follow, I will teach you what I know about Pine Script.

How to add numerical inputs to a TradingView script?

TradingView scripts frequently have input options that affect the script’s behaviour, like the length of a moving average or an oscillator’s overbought and oversold levels. How can we add such numerical input options to a script?

# Creating numerical input options in TradingView

Input variables are a useful TradingView feature. These variables have a value that’s set manually by an input option. That way we don’t have to edit the script’s source code to change the value of a variable. Instead, we simply change its setting in the script’s ‘Inputs’ window.

Those manual input options are created with the input() function (TradingView, n.d.). This function also returns the setting’s current value (Pine Script Language Tutorial, n.d.). By assigning that value to a variable, the input’s current setting can be used in the script’s code.

Several kind of inputs are possible in TradingView, like true/false checkboxes and symbol search boxes. Numerical inputs are possible too. They allow us to, for instance, manually change the length of an EMA or the offset of a price band. For this, we set the input() function’s type argument to integer (for an input that accepts whole numbers) or float, which creates a floating-point input that allows decimal values (Pine Script Language Tutorial, n.d.). Let’s take a closer look at how we make such numerical inputs.

# Default syntax for creating a numerical input in TradingView

Creating a numerical input in TradingView basically looks like:

numericalVariable = input(title, type=integer/float,

     minval, maxval, step, defval, confirm)

The different arguments in the parentheses of the input() function work as follows (TradingView, n.d.):

titleOptional argument that specifies which text is placed before the input option (for instance, title=”EMA Length”).
typeRequired argument that, when set to integer or float, creates a numerical up/down option (see image below).
minvalOptional argument that sets the input’s minimum value (like minval=20). When a value lower than this is entered in the input option, the input resets to the minval value.
maxvalOptional argument that defines the maximum value of the input (such as maxval=1.5). When we enter a value greater than this, the numerical input automatically reduces to the maxval setting.
stepOptional argument that sets the step size for incrementing and decrementing the option in the script’s ‘Inputs’ window. The value for step determines how much the input’s value changes when we click on the small up/down arrow or use the keyboard’s arrow keys. When left out of input(), step defaults to 1.
defvalRequired argument that sets the option’s default value, and can either be an integer (like defval=2) or a decimal (defval=1.005). With defval the script can start calculating as soon as it’s added to the chart.
confirmOptional argument that, when set to true, asks the user to confirm the input’s value before the script is added to the chart. Only one input() statement needs to have confirm enabled before the ‘Inputs’ window is brought up. confirm defaults to false, and so the input options don’t require confirmation when this argument is missing.

The value of the input option is returned by the input() function. That integer or floating-point value always needs to be assigned to a variable (TradingView, n.d.).

The integer and float input types created by input() look like:

Now let’s look at a couple of programming examples that use these kind of inputs.

# Example: making an integer input in TradingView

Before discussing the code, let’s first look at what the example script does. It creates two options in the ‘Inputs’ tab:

When we add this script to the chart, it looks like:

If we change the ‘Offset’ integer setting to 7 and enable the ‘Blue Line?’ checkbox option, the indicator looks like:

# Adding integer inputs to a TradingView script

The code for the example indicator is the following:

study(title=”Offset plot”)

offsetPlot = input(title=”Offset”, type=integer,

     defval=5, minval=1, maxval=10)

blueLine = input(title=”Blue line?”, type=bool, defval=false)

plotColour = blueLine ? blue : red

plot(series=offset(close, offsetPlot), color=plotColour)

We start with the study() function to set the indicator’s properties. Then we create two inputs with input(). The first is an integer named “Offset” that has a default value (defval) of 5 and an allowed range of 1 (minval=1) to 10 (maxval=10). This input’s current value is stored in the offsetPlot variable. The second input, a true/false bool input titled “Blue line?”, is unchecked by default (defval=false) and has its current value put into the blueLine input variable.

Then we use the conditional ternary operator (?:) to give the plotColour variable a value. That operator evaluates a true/false condition and returns its second value when that condition is true and its third value when it’s false (Pine Script Language Tutorial, n.d.). In our example, the evaluated condition is the blueLine input variable. When that variable is true (meaning that its checkbox is enabled), then blue is returned by the conditional operator and assigned to the plotColour variable. And when the “Blue line?” input is unchecked, red is stored in plotColour.

We end the example with plot(), a function that displays a series of data on the chart (TradingView, n.d.). That function’s series argument uses the offset() function to shift the closing price a number of bars to the right (TradingView, n.d.). The amount of bars shifted is specified by the offsetPlot input variable, which we gave a default value of 5 earlier. The colour of this plotted line is set to the plotColour variable.

# Example: adding a floating-point input to a TradingView script

In a similar way we can add a floating-point input to a script. Before discussing the code, this is the input option it makes:

The example indicator added to a chart looks like the following:

The indicator script has the following code:

study(title=”Close + percentage”)

perc = input(title=”Percentage”, type=float,

     defval=1.10, minval=1, maxval=2, step=0.1)

plot(series=close * perc)

We start this brief example by setting the indicator’s chart name with the study() function. Then we use the input() function to create a numerical input option that accepts decimal values (type=float). This input is named “Percentage” and has a default value (defval) of 1.10. The values that it can have lay between 1 (minval=1) and 2 (maxval=2), while each click on the numerical up/down control changes its value with one-tenth (step=0.1).

The current value of that input is stored in the perc variable. That variable is used in the example’s last statement, where we multiply the closing price with value of the “Percentage” input. The result of that calculation is then plotted on the chart with the plot() function (TradingView, n.d.).