Rob H
Rob H

Reputation: 3

If statement converting 'simple int' to 'series int'

In a trading view pine script I am trying to call ta.rma with a dynamic length but everything I try gives me this error in the console:

Cannot call 'ta.rma' with argument 'length'='length'. An argument of 'series int' type was used but a 'simple int' is expected

Using a simple assignment works (ie no error from the ta.rma function):

// This works
length = 20

But if I use an 'if' statement, length is converted to 'series int' and I have no idea why or how to fix it:

length = if syminfo.ticker == 'SPY'
    10
else
    20

rma call:

ta.rma(high - low, length)

I'm using a //@version=5 script

Upvotes: 0

Views: 3444

Answers (1)

vitruvius
vitruvius

Reputation: 21342

As the error message tells you, the length argument expects a simple int.

simple is a data type such that their values are known on the first bar and they never change during the execution. You can read this to learn more about type systems.

In your case, you are using a conditional operator to assign a value to length. This makes it not a simple because its value could change depending on your condition. Hence the error message.

len1 = input.int(14)
len2 = 14
len3 = math.round(bar_index / 2)

rma = ta.rma(close, 14)    // Fine
rma1 = ta.rma(close, len1) // Fine
rma2 = ta.rma(close, len2) // Fine
rma3 = ta.rma(close, len3) // Error as len3 will be different on each bar

Upvotes: 1

Related Questions