Home > Back-end >  How to use default values for array parameters in pine-script functions?
How to use default values for array parameters in pine-script functions?

Time:04-09

Pine-script;

  • Libraries cannot use global variables in exported functions, so I cannot use a global array as a default value.
  • Functions do not accept function results as default values.

Is there a way to provide default array value?

For example:

// @version=5

library("mylibrary", overlay = true)

// This is OK
export calc(int a = 10, int[] b) => ...

// This is NOT OK
export calc(int a = 10, int[] b = array.from(1,2)) => ...

CodePudding user response:

You cannot provide default value as an array. And array argument cannot be omitted.

In the future there will be way to assign int[] b = na in the function signature. After that you will be able to check if passed array is 'na', and then reassign it.

It will be possible to do something like this:

// @version=5
library("mylibrary", overlay = true)

export calc(int[] b = na)  => 
    arr = na(b) ? array.from(1,2) : b
    array.size(b)
    
plot(calc())
  • Related