imlepid
imlepid

Reputation: 21

Iterator in loop is considered a series form?

There seems to be a bug in pine script that has broken an indicator I wrote. Previously (before ~Oct2020), I had an indicator that did some calculation inside a for loop using the ema function. Now, Pine Script throws an error when I try and add my script:

Add to Chart operation failed, reason: line 21: Cannot call 'ema' with arguments (series[float], series[integer]); available overloads: ema(series[float], integer) => series[float]

Here's a sample of what I'm trying to run:

//@version=4
study("My Script")
emastep = 5
emashort = 5
thisema = 0.0
emanum = 10
lastema = ema(close, emashort)
for i = 1 to emanum
    thisema := ema(close, round(i*emastep))
plot(thisema)

The issue seems to be the "i" in the ema(close, round(i*emastep)) part. What's interesting is a sample script from the pine script manual (https://www.tradingview.com/pine-script-docs/en/v4/language/Expressions_declarations_and_statements.html#for-statement) also throws the same error now. The following sample from the above link doesn't work:

//@version=4
study("RMA in for loop")
sum = 0.0
for i = 1 to 2
    sum := sum + rma(close, i)
plot(sum)

Any thoughts on how to get around this? i in a for loop shouldn't be a series form, right?

Upvotes: 0

Views: 1798

Answers (2)

imlepid
imlepid

Reputation: 21

The bug that threw the error is, as I suspected, related to the i iterator in the for loop. If I create my own iterator, and use that, everything works ok. This is my solution I came up with:

//@version=4
study("My Script")
emastep = 5
emashort = 5
thisema = 0.0
emanum = 10
lastema = ema(close, emashort)
iter = 1
for i = 1 to emanum
    thisema := ema(close, round(iter*emastep))
    iter := iter + 1 
plot(thisema)

Hope this helps others.

Upvotes: 2

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

Several pine functions support dynamic length arguments.
However, rma() is not one of them.

This function has never accepted a dynamic argument length, also not in v3 of Pine.
Just because it doesn't throw an error in v3 doesn't mean the calculation is correct.
This example calculates the rma in v3, once in a loop and once with a fixed value, but both should result in an rma(close, 15).

//@version=3
study("RMA fixed length vs variable length")

sum = 0.0
i = 0

for i = 1 to 15
    sum := rma(close, i)

plot(rma(close, 15), color=green)
plot(sum, color=red)

Both should return the rma with a length of 15, but the results are very different (=incorrect), because of the use of the unsupported variable length input.

rma variable vs fixed length

The reference manual for rma() shows an example of how to calculate rma in a function.
You can use that in your code as a workaround.

//@version=4
study("RMA in for loop")

// Built-in RMA
plot(rma(close, 15), color=color.green)

// The same on pine
pine_rma(src, length) =>
    alpha = 1/length
    sum = 0.0
    sum := na(sum[1]) ? sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])

plot(pine_rma(close, 15), color=color.red)


// Your code with built-in rma(), this will NOT work
// sum = 0.0
// for i = 1 to 2
//     sum := sum + rma(close, i)

// plot(sum, color.blue)


// Your code with pine_rma(), this will work
sum = 0.0
for i = 1 to 2
    sum := sum + pine_rma(close, i)

plot(sum, color.blue)

Notice how the red and the green line in this code are identical (overlapping).
This proves that the result of pine_rma() is identical to the built-in rma(), but with the extra advantage of accepting mutable variables as input, which results in the blue line, as you intended.

Working

Upvotes: 0

Related Questions