UK 100
Education

PineScript v6: Conditional Expressions from Libraries

49

I thought it appropriate to make some quick notes on calling conditional expressions from PineScript v6 libraries, seeing as I have recently updated all of my libraries to v6 and most of my function exports output booleans or values that are ultimately derived from other functions that output booleans.

When calling functions in v6 that output booleans or values derived from other functions that output booleans, it is best practice to first declare the function return globally before you use said output as input for anything else.

For example, instead of calling my swing low and uptrend functions (which both return booleans) as part of a broader conditional expression:

Pine Script®
//@version=6 indicator('Example Conditional Expression 1') import theEccentricTrader/PubLibSwing/3 as sw import theEccentricTrader/PubLibTrend/2 as tr uptrend = sw.sl() and tr.ut() plotshape(uptrend)


I would first declare the function returns as global variables and then call the broader conditional expression using said variables:

Pine Script®
//@version=6 indicator('Example Conditional Expression 2') import theEccentricTrader/PubLibSwing/3 as sw import theEccentricTrader/PubLibTrend/2 as tr sl = sw.sl() ut = tr.ut() uptrend = sl and ut plotshape(uptrend)


This demonstrates different behaviour from v5, where you could combine functions that output booleans in conditional expressions without error or warning.

The same also applies to functions that output values derived from other functions that output booleans. In the example below, my swing low price and bar index functions output float and integer values, respectively, but these values are derived from the swing low function, which is a function that returns a boolean. So these return values should also be first declared globally for later use, just like the swing low and uptrend functions:

Pine Script®
//@version=6 indicator('Example Conditional Expression 3', overlay = true) import theEccentricTrader/PubLibSwing/3 as sw import theEccentricTrader/PubLibTrend/2 as tr sl = sw.sl() ut = tr.ut() slp_0 = sw.slp(0) slpbi_0 = sw.slpbi(0) slp_1 = sw.slp(1) slpbi_1 = sw.slpbi(1) if sl and ut line.new(slpbi_1, slp_1, slpbi_0, slp_0, color = color.green)
Note
It seems that the change in behaviour is, in-fact, applicable to all conditional expressions that use more than one user-defined function (UDF). If, for example, you create a new function in an indicator and want to use that function inside another conditional expression later on in the same script, you will still need to first declare the function return as a variable globally. Which is, again, completely different behaviour from v5. And, arguably, quite counter-intuitive all things considered.

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.