First indicator

The Pine Script™ Editor is your primary workspace for script development. You could write your Pine scripts in any text editor, but our Editor comes with multiple benefits:

  • It offers syntax highlighting adhering to Pine Script™ rules.

  • Hovering over built-in and library functions prompts syntax reminders.

  • It offers an auto-complete feature that you can trigger with ctrl + space / cmd + space.

  • It enables quick iteration by instantly executing a newly saved script version loaded on the chart.

  • Although it may not be as feature-packed as some top editors, it provides essential functionality such as search & replace, multi-cursor editing, and versioning.

To launch the Editor, click on the “Pine Script™ Editor” tab located at the bottom of your TradingView chart. This action will open the Editor's pane.

First Indicator: Version One

Let's create our first Pine script—a Pine Script™ rendition of the MACD indicator. Start by selecting "New indicator". Copy the following script into the editor:

//@version=5
indicator("MACD #1")
fast = 12
slow = 26
fastMA = ta.ema(close, fast)
slowMA = ta.ema(close, slow)
macd = fastMA - slowMA
signal = ta.ema(macd, 9)
plot(macd, color = color.blue)
plot(signal, color = color.orange)
indicator.signal(true)
  • Replace all existing code in the editor with this script.

  • Click “Save” and assign a name to your script. The script will be saved under your account in Bulltrading’s cloud, and only you can access it.

  • Click “Compile” in the Editor’s menu bar. The MACD indicator will now appear in behavior.

Your first Pine script is now running on your chart, and it should look like the following image:

Now, let's break down the script:

  • Line 1: //@version=5 - A compiler directive that signals the use of Pine Script™ version 5.

  • Line 2: indicator("MACD #1") - Defines the script's name as it will appear on the chart.

  • Lines 3 and 4: fast = 12 and slow = 26 - Define two integer variables representing the lengths of the fast and slow EMAs.

  • Lines 5 and 6: fastMA = ta.ema(close, fast) and slowMA = ta.ema(close, slow) - Define variables containing the EMAs calculated on the closing prices.

  • Line 7: macd = fastMA - slowMA - Defines the MACD as the difference between the fast and slow EMAs.

  • Line 8: signal = ta.ema(macd, 9) - Defines the signal line as the 9-period EMA of the MACD.

  • Lines 9 and 10: plot(macd, color = color.blue) and plot(signal, color = color.orange) - These lines plot the MACD and signal lines on the chart in blue and orange, respectively.

  • Lines 11 : indicator.signal(true) - These lines return a positive signal on all candles.

First Indicator: Version Two

Our first version calculated the MACD "manually." However, Pine Script™ includes built-in functions for many common indicators, including the MACD: ta.macd(). Let's use this function in the second version of our script:

//@version=5
indicator("MACD #2")
fastInput = input(12, "Fast length")
slowInput = input(26, "Slow length")
[macdLine, signalLine, histLine] = ta.macd(close, fastInput, slowInput, 9)
plot(macdLine, color = color.blue)
plot(signalLine, color = color.orange)
indicator.signal(true)

Notably, this version:

  • Adds inputs so we can change the lengths of the moving averages.

  • Uses the ta.macd() Pine Script™ built-in to calculate our MACD, reducing three lines of code and enhancing readability.

The modified lines in the second version of our script are:

  • Line 2: indicator("MACD #2") - We change #1 to #2 to display a different name for the second version of our indicator on the chart.

  • Lines 3 and 4: fastInput = input(12, "Fast length") and slowInput = input(26, "Slow length") - We replace the constant values with input() functions, allowing us to change the values via the script's "Settings/Inputs" tab.

  • Line 5: [macdLine, signalLine, histLine] = ta.macd(close, fastInput, slowInput, 9) - We now use the ta.macd() built-in function to perform all calculations in one line. It requires four parameters and returns three values that we assign to the macdLine, signalLine, and histLine variables.

  • Lines 6 and 7: The variable names for plotting have changed, but they function as before.

Our second version performs the same calculations as our first, but now we can change the two lengths used in the calculations. The code is also simpler and shorter by three lines—an improvement for our script.

Last updated