Search code examples
pine-scripttradingview-apipine-script-v4

How can I fix this Unassigned argument length error on this code?


I'm trying to write a Exponentially Weighted Standard Deviation code but got stuck in the middle.

//@version=4
study("Exponentially Weighted Standard Deviation", shorttitle="EWSD", overlay=false)

// Input for the length of the EMA
length = input(32, title="Length", minval=1)

// Smoothing factor (typically a value between 0 and 1)
lambda = 2 / (length + 1)

// Calculate Exponentially Weighted Moving Average (EMA)
emaValue = ema(close, length)

// Function to calculate the exponentially weighted standard deviation
ew_stddev(src, length) =>
    summ = 0.0
    for i = 0 to length - 1
        summ := summ + (1 - lambda) * pow(lambda, i) * (src[i] - emaValue)
    summ := sqrt(summ / sum(1 - pow(lambda, 2)))

// Calculate the exponentially weighted standard deviation
ew_stddevValue = ew_stddev(close, length)

plot(ew_stddevValue, color=color.blue, title="EW Standard Deviation")

I still don't understand why the error pops up... Can somebody help please?

ps. Did I write the code right for the Exponentially Weighted Standard Deviation?

I tried to compile the code but still the error pops up and I don't know why or how to solve this...


Solution

  • sum() expects two arguments. You are missing the length as the error message tells you.

    The sum function returns the sliding sum of last y values of x.

    sum(source, length) → series[float]
    

    ARGUMENTS
    source (series[float]) Series of values to process.
    length (series[integer]) Number of bars (length). Can be series[integer].