Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function returning 2 values to global variable

Tags:

pine-script

In light of this post I'd like to ask why the script hereunder works for [a,b] but doesn't work for [c,d].
Cannot find any documentation that explains why this doesn't work.

This example is only for 2 return values, but in reality I'm going to create a function with 6 or more variables to be returned in one go.
I'm trying to avoid having to enter 6 different lines, because I'll be entering this data every trading day (the function will be date-depentent and I already have code for that).
So I'd like to only have to enter 1 line per day to keep the source code clear and maintainable.

//@version=4
study("Functions test")

var int c = na
var int d = na

f(x) => [x,x+5]

[a,b] = f(20)
[c,d] := f(30)

plot(a)
plot(b)
plot(c)
plot(d)
like image 263
Bjorn Mistiaen Avatar asked Feb 24 '26 21:02

Bjorn Mistiaen


1 Answers

My understanding is that assigning with := is not allowed for tuple-like function returns. If you want to avoid entering multiple times the function input, in this case, 20 and 30, while keeping the variable definition as it is, you can still do something like:

//@version=4
study("Functions test")

var int c = na
var int d = na

f(x) => [x,x+5]

[a,b] = f(20)
[c1,d1] = f(30)

c := c1
d := d1

plot(a)
plot(b)
plot(c)
plot(d)

It does require several extra lines, and looks ugly, but at least you limit to one the number of times you have to type the input to the function as desired.

like image 61
Glauco Esturilio Avatar answered Feb 27 '26 11:02

Glauco Esturilio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!