Search code examples
pine-scriptpine-script-v5tradingview-apipine-script-v4

Pinescript - Series float used when fun_arg[] expected


I am trying to convert Williams Percent Range values to the positive domain using the attached scripts. It does need a bit of tweaking, however when attempting to pass a float array as a parameter to the custom function the following error is generated:

Cannot call 'array.size' with argument 'id'='duplicateArray'. An argument of 'series float' type was used but a 'fun_arg[]' is expected.

I understand what it means. However I do not know how to fix it.

Code:

//@version=5
indicator("Williams Percent Range Average", shorttitle="Williams %R", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

// Williams Settings

williamsGroup = "=== WILLIAMS ==="
lengthWilliams1 = input(title="Length", defval=13,group = williamsGroup)
lengthWilliams2 = input(title="Length", defval=21,group = williamsGroup)
lengthWilliams3 = input(title="Length", defval=34,group = williamsGroup)

lengthWilliamsAverage = math.round((lengthWilliams1 + lengthWilliams2 + lengthWilliams3) / 3)
srcWilliams = input(close, "Williams Source")

_pr(length) =>
    max = ta.highest(length)
    min = ta.lowest(length)
    100 * (srcWilliams - max) / (max - min)
percentR1 = _pr(lengthWilliams1)
percentR2 = _pr(lengthWilliams2)
percentR3 = _pr(lengthWilliams3)
percentR4 = _pr(lengthWilliamsAverage)

convertWilliams(williamsCalculations) =>
    multiplier = 0.0625
    duplicateArray = williamsCalculations
    size = array.size(duplicateArray)
    modifiedWilliamsValues = array.new_float(size) 
    for i = 0 to size - 1
        n = williamsCalculations[i]
        diff_n = n + 80
        n_multi = diff_n * multiplier 
        new_n = (n / (-4+n_multi)) + 20
        array.insert(modifiedWilliamsValues,i,new_n)
    modifiedWilliamsValues

modifiedR1 = convertWilliams(percentR1)

Additionally I have never had to return a value from a custom function before. Could anyone tell me how to return the modified array using an equivalent of a return statement as the other error I get is:

The end value of the "for" loop must be a number.

Any advice would be appreciated. Thanks


Solution

  • You call convertWilliams() like below:

    modifiedR1 = convertWilliams(percentR1)
    

    And percentR1 is defined as percentR1 = _pr(lengthWilliams1).

    _pr returns series float so percentR1 is series float.

    Then you pass this to convertWilliams() and in that function you call array.size() on it which is why you are getting both errors.