The Pine Script™ Editor is where you will be working on your scripts. While you can use any text editor you want to write your Pine scripts, using our Editor has many advantages:
To open the Editor, click on the “Pine Script™ Editor” tab at the bottom of your TradingView chart. This will open up the Editor’s pane.
We will now create our first working Pine script, an implementation of the MACD indicator in Pine Script™:
1 2 3 4 5 6 7 8 9 10 | //@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) |
Your first Pine script is running on your chart, which should look like this:
Let’s look at our script’s code, line by line:
//@version=5
indicator("MACD #1")
fast = 12
fast
integer variable which will be the length of the fast EMA.slow = 26
slow
integer variable which will be the length of the slow EMA.fastMA = ta.ema(close, fast)
fastMA
, containing the result of the
EMA calculation (Exponential Moving Average) with a length equal
to fast
(12), on the close
series, i.e., the closing price of bars.slowMA = ta.ema(close, slow)
slowMA
, containing the result of the
EMA calculation with a length equal to slow
(26), from close
.macd = fastMA - slowMA
macd
as the difference between the two EMAs.signal = ta.ema(macd, 9)
signal
as a smoothed value of
macd
using the EMA algorithm (Exponential Moving Average) with
a length of 9.plot(macd, color = color.blue)
plot
function to output the variable macd
using a blue line.plot(signal, color = color.orange)
plot
function to output the variable signal
using an orange line.The first version of our script calculated MACD “manually”, but because Pine Script™ is designed to write indicators and strategies, built-in Pine Script™ functions exist for many common indicators, including one for… MACD: ta.macd().
This is the second version of our script:
1 2 3 4 5 6 7 | //@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) |
Note that we have:
Let’s repeat the same process as before to copy that code in a new indicator:
Your second Pine script is running on your chart. If you double-click on the indicator’s name on your chart, you will bring up the script’s “Settings/Inputs” tab, where you can now change the slow and fast lengths:
Let’s look at the lines that have changed in the second version of our script:
indicator("MACD #2")
#1
to #2
so the second version of our indicator displays a different name on the chart.fastInput = input(12, "Fast length")
12
will be the default value and the field’s label will be "Fast length"
.
If the value is changed in the “Inputs” tab, the fastInput
variable’s content will contain the new value and the script will re-execute on the chart with that new value.
Note that, as our Pine Script™ Style Guide recommends, we add Input
to the end of the variable’s name to remind us, later in the script,
that its value comes from a user input.slowInput = input(26, "Slow length")
[macdLine, signalLine, histLine] = ta.macd(close, fastInput, slowInput, 9)
=
sign.
Note that two of the values we pass to the function are the “input” variables containing the fast and slow lengths: fastInput
and slowInput
.Our second version performs the same calculations as our first, but we can change the two lengths used to calculate it. Our code is also simpler and shorter by three lines. We have improved our script.