EliCobra

How functions work in pinescript

Education
INDEX:BTCUSD   Bitcoin

Functions in Pine Script are powerful tools that simplify and enhance the coding experience.
They allow you to create reusable blocks of code, making your scripts more organized and efficient.

Let's start by exploring a basic function:

ochl() => [open, close, high, low]

This function, named 'ochl', returns an array containing four values: open, close, high, and low.
The square brackets '' indicate the return values, and they must be placed at the end of the function.
To utilize the returned values, we can assign them to variables like this:

[o, c, h, l] = ochl()

Now, 'o' holds the 'open', 'c' holds the 'close', 'h' holds the 'high', and 'l' holds the 'low'.
Alternatively, we can use different variable names:

[x1, x2, x3, x4] = ochl()

Now 'x1' holds the 'open', 'x2' holds the 'close', 'x3' holds the 'high', and 'x4' holds the 'low'.

We can apply these variables to create specific rules or calculations in our script.

isOver = false

if o > o[1]
    isOver := true

In this example, we set 'isOver' to 'true' if the current 'open' is greater than the previous 'open'.
This demonstrates the basic usage of functions. Now, let's explore a more practical example.

newSMA(float src, int len) =>
    sma = ta.sma(src, len)
    sma

sma = newSMA(close, 14)

In this case, we have a function named 'newSMA' that takes two parameters: 'src' for the source and 'len' for the length.
We use 'float' and 'int' before the parameters to specify their types, improving code clarity and preventing errors.

Inside the function, we can perform any desired operations. It's worth noting that variables inside the function can have the same names as outside variables without causing conflicts.

In this example, we declare a variable 'sma' that calculates the simple moving average (SMA) using the 'ta.sma' function with the specified 'src' and 'len'.
Finally, we return the 'sma' value. If a function returns only one value, we don't need to use ''.

To use this function, we call it with different inputs:

sma1 = newSMA(close, 200)
sma2 = newSMA(low, 22)
sma3 = newSMA(high, 15)

Now 'sma1', 'sma2', and 'sma3' store the SMAs of 'close' with lengths 200, 22, and 15 respectively.

Let's delve a bit deeper now.

Consider the following code:

v = volume

getVol(float vol) =>
    x = vol > vol[1] ? +1 : -1
    x

getSum(int n1, int n2) =>
    bool id = na
    if getVol(v) == +1 and n1 == 0 and n2 == 5
        id := true
    if getVol(v) == -1 and n1 == 9 and n2 == 7
        id := false

    id

out1 = getSum(0, 5)
out2 = getSum(1, 5)

Here, we define two functions: 'getVol' and 'getSum'.

'getVol' takes 'vol' as the input (volume), and it returns +1 if the current volume is greater than the previous volume; otherwise, it returns -1.

'getSum' has two parameters, 'n1' and 'n2', which will be used later.

Within 'getSum', we declare a variable 'id' and set it to 'na' (Not Available) initially.

We then check conditions using 'getVol(v)', where 'v' is the volume. Depending on the result and the values of 'n1' and 'n2', we set 'id' to either 'true' or 'false'.

Consequently, 'out1' will be 'true' if the volume > volume and 'n1' is 0 and 'n2' is 5, otherwise it will be 'false'.
Similarly, 'out2' will always be 'false' as we pass 1 and 5, which do not match any of the conditions (0, 5 or 9, 7).

Functions can be very useful when running inside a security call:

[o1, c1, h1, l1] = request.security("", "480", ochl())

This line outputs the 'ochl' values on the 1-hour time frame. It allows us to obtain four values from a security call without manually writing each one.

I hope you now have a better understanding of how Pine Script functions work! If you encounter any issues, feel free to comment below, and I'll be glad to assist you!

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.