2
votes

I'm trying to send a variable into the argument "length" to my Aroon indicator.

If I do it using the internal pine function "highestbars", I get this error :

"Cannot call highestbars with arguments (series[float], series[float]); available overloads: highestbars(series[float], integer) => series[integer]; highestbars(integer) => series[integer];"

My work around so far is to recreate the internal function I need as a normal function into my script.

Ex. If I need to send a variable length to my sma, I add that function to my script and use that function instead.

my_sma(price, length) =>
    sum = price
    for i = 1 to length-1
        sum := sum + price[i]
    sum / length

The problem is that I can't find the code for Highestbar and I tried to do it my self but I never get the same result as pine. Thanks you very much for your time.

Coding it my self Looking if someone already done that Went on google Went on PineScript v4 reference

I tried a lot with for but I failed... so the closest result I have so far is with :

plot(highestbars(high,5)) //Expected result

myhigh =0
myhigh := (high <= high[1]) ? myhigh[1]-1 : (high<high[1]) ? 0 : 0
plot(myhigh,color=color.red) //This result != pinescript function :(

I expect that function to output the same result as the Pine Script internal function highestbars

3

3 Answers

2
votes
//@version=4
//@author=LucF, for PineCoders

// f_highestbars [PineCoders]
//  v1.0, 2019.08.06 02:19 — Luc

// This script implements the built function highestbars() but accepts a mutable series float length.

study("f_highestbars [PineCoders]")
src = input(high)
// These are for testing purposes.
len1 = input(30.1)
len2 = input(60.9)
cond = close>open
len = 0.0
// Create a mutable series float to be used as length for testing.
len := cond ? len1 : len2

f_highestbars(_src, _len) =>
    _index = 0
    _highest = 0.0
    for _i = 0 to _len-1
        if _src[_i]>=_highest
            _highest := _src[_i]
            _index := _i
    -_index

// Calculate value of built-in for both test lengths and choose the one matching condition.
v11 = highestbars(src, int(len1))
v12 = highestbars(src, int(len2))
v1 = cond ? v11 : v12
// User-defined equivalent of built-in function.
v2 = f_highestbars(src, len)
plot(v1, "v1: highestbars")
plot(v2, "v2. f_highestbars", color=color.orange)
// Plot red background if built-in and user-defined functions results do not match.
bgcolor(v1!=v2? color.red:na, transp=0)
2
votes

Thanks again LucF, you really helped me a lot.

Btw I asked in a comment about the lowestbar function. It is not as pretty as what you did but I found a way to make it work and here is the answer.

f_lowestbars(_src, _len) =>
_index = 0
_lowest = 0.0
_lowest := low
for _i = 0 to _len-1
    if _src[_i]<=_lowest
        _lowest := _src[_i]
        _index := _i
-_index
1
votes

Don't sweat the down-voting; that's just the condescending SO crowd interfering with useful answers. They have a nice culture of rigor, but the crowd sometimes overdoes it. Had to change first mod's edit as he deleted the //@version=4 directive with all other comments in script header because they originally contained pinecoders.com. Guess he just assumed it was something commercial and didn't check, so no worries.

So... for lowestbars you need to initialize your _lowest var with something big enough that anything is going to be smaller on first test.

f_lowestbars(_src, _len) =>
    _index = 0
    _lowest = 10e20
    for _i = 0 to _len-1
        if _src[_i]<=_lowest
            _lowest := _src[_i]
            _index := _i
    -_index