Back to TradingView PineScript for CFD Traders
Part 2

Functions: writing reusable blocks

Functions in Pine take series and return series. They are how you stop copy-pasting the same code into every script.

Intermediate9 min readBeginner → AdvancedLesson 08 / 17
Try it in TradingView
A documented function, called twice

percentChange() is defined once and used for two different lookbacks. Hover the call in the editor to see the doc string.

//@version=5
indicator("Lesson 8 - Functions", overlay=false)

//@function        Percent change of a series over a lookback window.
//@param src       The series to measure.
//@param lookback  Number of bars back.
//@returns         Percent change as a float series.
percentChange(src, lookback) =>
    (src - src[lookback]) / src[lookback] * 100

// Same function, two lookbacks.
pct20 = percentChange(close, 20)
pct50 = percentChange(close, 50)

plot(pct20, "20-bar % change", color=color.blue, linewidth=2)
plot(pct50, "50-bar % change", color=color.orange, linewidth=2)

hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)
bgcolor(pct20 > 0 ? color.new(color.green, 90) : color.new(color.red, 90))
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

The function shape

A Pine function takes typed inputs and returns a value. The simplest:

percentChange(src, lookback) =>
    (src - src[lookback]) / src[lookback] * 100

This is a function literal — a one-liner with =>. Use it: plot(percentChange(close, 20)).

Multi-line functions

Use the block form for anything non-trivial:

atrPercent(length) =>
    atr = ta.atr(length)
    atr / close * 100

plot(atrPercent(14))

Returning multiple values with tuples

Pine v5 functions can return tuples:

macdSignal(src, fast, slow, signalLen) =>
    fast_ma = ta.ema(src, fast)
    slow_ma = ta.ema(src, slow)
    macd = fast_ma - slow_ma
    sig = ta.ema(macd, signalLen)
    [macd, sig]

[m, s] = macdSignal(close, 12, 26, 9)
plot(m)
plot(s)

Doc strings

Pine supports doc comments above functions:

//@function        Returns the percent change of a series over a lookback window.
//@param src       The series to measure
//@param lookback  Number of bars back
//@returns         The percent change as a float series
percentChange(src, lookback) =>
    (src - src[lookback]) / src[lookback] * 100

Hovering the function call in the editor shows the docs.

Where to put functions

Top of the script, before any code that calls them. Functions can call other functions. There is no "header file" — Pine is a single-file language.

What this unlocks

Once you write functions for your common building blocks (custom MAs, signal generators, sizing logic), every new strategy becomes "compose the functions, plot the signals" instead of writing the same math from scratch.

What you just did

Lesson 08 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.