Create PineScript Indicators & Strategies (TradingView)

Custom PineScript (TradingView) IndicatorsHow to create Indicators & Automated Strategies using TradingView PineScript

PineScript is a simple and free-to-use programming language, specially designed for traders, which is incorporated into the TradingView platform.

A few words about TradingView

TradingView is an online platform that is basically free and provides all the charting tools and indicators you need to create trading ideas and share them with the community. The TradingView platform incorporates its own scripting language called PineScript (similar to Python) which allows the creation of custom indicators. All you need to use TradingView and PineScript is a free account:

» Visit TradingView and open a free account

Introduction to the PineScript

PineScript is a cloud-based language, and that means you can create and use indicators in your desktop computer, or your mobile device, without the need of installing any software.

Key Features of Pine scripts

  • A script can be a STUDY or a STRATEGY (review script structures below)
  • At the beginning of each script, there is an annotation defining the version of PineScript used, along with the name and the basic properties of the script
  • Each script’s main body contains FUNCTIONS and VARIABLES. Functions correspond to instructions that lead to calculations. Variables save the values of those calculations in the cloud.
  • PineScript contains built-in functions that you can use in your scripts
  • At the end of each script, you can define which variables will be plotted, and where they will be plotted

Example of a simple Pine script

Before moving further, here is a simple Pine script that contains the annotation (Lines 1,2), the functions and variables (Lines 3,4), and the instruction to plot on-screen (Line 5).

You can use this script on your Pine Editor by simply removing the numbers (1,2,3 etc.)

1 //@version=4 {A directive that tells the compiler to use version 4 of PineScript}

2 study("TEST") {States that the structure of the script is a Study (not a Strategy) by giving the name TEST}

3 FunctionA…. {includes the basic calculations of the script}

4 TEST_1 = FunctionA {Links the variable TEST to the calculations of FunctionA}

5 plot(TEST_1, {that will give the instruction to plot the variable TEST_1 on the chart, in green color}



The Two (2) Available Types of Script Structures

As mentioned in the beginning, a script can be a Study or a Strategy. Both structures can plot calculations and generate alert events. The difference is that strategies are designed for trading automation. In other words, strategies can be used for the automated placement and modification of trading orders, without any human intervention.

(1) Study Structures

A study must contain at least one function which produces some output on the chart (e.g., plot, plotshape, barcolor, etc.).

Example of a PineScript Study structure

This is a simple MACD study on MACD standard settings (12,6,9):

  • As we can see Lines 1,2 describe the version of PineScript and the name of the Study
  • Lines 3,4,5 describe the Functions of the script
  • Lines 6,7 describe the Variables, and links these variables to the previous Functions
  • Lines 8,9 plot the Variables on screen

1 //@version=4

2 study("MACD")

3 fast = 12, slow = 26

4 fastMA = ema(close, fast)

5 slowMA = ema(close, slow)

6 macd = fastMA - slowMA

7 signal = sma(macd, 9)

8 plot(macd,

9 plot(signal,

Image: Visualizing the MACD study on TradingView

Visualizing the MACD study on TradingView

(2) Strategy Structures

A strategy is a script that is able to automatically execute trades (send, modify and cancel trade orders). Furthermore, strategies can backtest any trading idea based on historical data. Once a strategy is backtested on historical data, you can even see hypothetical order fills.

In the following example, there is a simple strategy that buys/sells an Index. You can use this script on your Pine Editor by simply removing the numbers (1,2,3 etc.)

Example of a simple PineScript strategy

1 //@version=4

2 strategy("test")

3 if bar_index > 5000

4 strategy.entry("buy", strategy.long, 10, when=strategy.position_size <= 0)

5 strategy.entry("sell", strategy.short, 10, when=strategy.position_size > 0)

6 plot(strategy.equity)

Image: Plotting the Strategy on TradingView

Plotting the Strategy on TradingView


The Quant Platform for Advanced Automated Forex Trading...


PineScript Functions & Examples

PineScript contains a plethora of built-in functions, so you don’t have to create everything from scratch.

TIP: How to Uncover all Available Functions, within the Editor

If you are using the Pine editor and you can’t find a certain function you can always get instant help and uncover all available functions by using the following combination of keys.

Type a part of the syntax and then press:

For Windows PCs… CTRL + spacebar

For an Apple Mac… CMD + spacebar

Using ‘Functions’ you can research particular markets and even create comparisons between correlated asset classes. For example, let’s suppose you want to create an indicator that plots the price of MicroStrategy in order to compare it with the Bitcoin price. For that instance, we are going to create a variable called MSTR and use the simple built-in function called ‘Security’. The function calls the MSTR symbol of MicroStrategy and uses daily (D) closing prices.

□ Example: Indicator showing MicroStrategy price on Data Window

1 //@version=4

2 study("price of MicroStrategy")

3 MSTR_price = security("MSTR", "D", close)

4 plot(MSTR_price)

Next, you want to add an EMA to the comparison between Bitcoin and MicroStrategy. Let’s suppose you want to add a 50-period EMA.

You have to add two lines. One line to state the new variable

■ MSTREMA = ema(MSTR_price, 50)

And another line at the end, in order to plot the EMA in orange color.

■ plot(MSTREMA,

□ Example: Indicator showing MicroStrategy price including a 50-period EMA.

1 //@version=4

2 study("price of MicroStrategy")

3 MSTR_price = security("MSTR", "D", close)

4 MSTREMA = ema(MSTR_price, 50)

5 plot(MSTR_price)

6 plot(MSTREMA,

Image: MicroStrategy Price & EMA(50) in the Data Window

MicroStrategy Price & EMA(50) in the Data Window

Next, you want to move the MicroStrategy chart and its EMA to the main chart of Bitcoin in order to optimize your comparison. This can be done by adding overlay=true into the study annotation.

■ study("price of MicroStrategy ", overlay=true)

The problem is that MicroStrategy's price is only a fraction of the Bitcoin price, and consequently, the charts are not comparable.

We can easily solve this problem by multiplying the price of MicroStrategy by 100 (X100). This can be done by creating a new variable called MSTR_x100

■ MSTR_x100 = MSTR_price*100

Example: Showing MicroStrategy price and a 50-period EMA within the main Bitcoin chart

1 //@version=4

2 study("price of MicroStrategy", overlay=true)

3 MSTR_price = security("MSTR", "D", close)

4 MSTR_x100 = MSTR_price*100

5 MSTREMA = ema(MSTR_x100, 50)

6 plot(MSTR_x100)

7 plot(MSTREMA,

Image: MicroStrategy Price & EMA(50) in the Main Bitcoin Chart

MicroStrategy Price & EMA(50) in the Main Bitcoin Chart



Creating Visual Alerts

Both types of scripts (studies and strategies) can generate visual alerts.

Example of Creating an Alert based on RSI(14)

In the following example, we create two RSI visual alerts.

  • If RSI crosses over 30, we get a green arrow
  • If RSI crosses under 70, we get a red arrow

Here is the script, if you want to use it on your Pine Editor don't forget to remove the numbers (1,2,3 etc.) at the beginning of each line.

1 //@version=4

2 study("RSI-ALERTS")

3 r = rsi(close, 14)

4 RSIUp = crossover(r, 30)

5 RSIDn = crossunder(r, 70)

6 plot(r,

7 hline(80, color=color.gray, linestyle=hline.style_dashed)

8 hline(20, color=color.gray, linestyle=hline.style_dashed)

9 plotchar(RSIUp, "BUY",  "▲", location.bottom,, size = size.tiny)

10 plotchar(RSIDn, "SELL", "▼",,,  size = size.tiny)

11 alertcondition(RSIUp, "Long Alert", "BUY")

12 alertcondition(RSIDn, "Short Alert", "SELL")

Final Thoughts -Why PineScript?

The TradingView platform and the programming language PineScript are free, here are some good reasons for using them:

  • Everything is cloud-based, which means you can access your account via any device (desktop or mobile) without any installations
  • TradingView offers a great variety of markets and financially-traded assets, including stocks, indices, Forex, bonds, commodities, and cryptocurrencies
  • You gain full access to a great variety of charting tools, chart types, and timeframes
  • You can create custom indicators or fully-automated trading strategies via PineScript
  • PineScript is very easy to learn and offers an extensive built-in library
  • TradingView users can publish their ideas and their indicators to the TradingView library
  • TradingView users can even sell their indicators and their strategies to the community (social trading features)

» Visit TradingView and open a free account | » PineScript Coding Tutorial


Create PineScript Indicators & Strategies (TradingView)

Giorgos Protonotarios, financial analyst, (c)




Pin It