[Solution] Highest/Lowest/Falling/Rising functions with series-integer in PineScript

 

[NOTE: at  09/09/2020 the functionality became available & built-in for PineScript for several functions]

______

[Original Article]

Probably, you have already met the problem, when you tried to use highest()  or lowest() functions in TradingView PineScript using the dynamic/calculated variable, i.e.:

length = close > open ? 2 : 3
my_value = highest(close, length )

So, you will get the error:

Cannot call `highest` with arguments (series, series); available overloads: highest(series, integer) => series; highest(integer) => series;
Cannot call `lowest` with arguments (series, series); available overloads: lowest(series, integer) => series; lowest(integer) => series;
Cannot call `rising` with arguments (series, series); available overloads: lowest(series, integer) => series; lowest(integer) => series;
Cannot call `falling` with arguments (series, series); available overloads: lowest(series, integer) => series; lowest(integer) => series;

As always (this is generic rule in most platforms), the platform developers just ignore users’ requests for years, even for simple things (So, don’t hope your request-topic, opened years ago, will be ever fixed in near future). Instead, you should find a custom solution. So, we had to create a custom solution for that, and finally did:

highest / lowest:

highest_(values, length) =>
h_val = values[0]
h_indx = 0
if length >= 1
for i = 0 to length-1
if ( not na(values[i]) and values[i] > h_val )
h_indx := i
h_val := values[i]
h_val

lowest_(values, length) =>
l_val = values[0]
l_indx = 0
if length >= 1
for i = 0 to length-1
if ( not na(values[i]) and values[i] < l_val )
l_indx := i
l_val := values[i]
l_val

You can use now:

hVal = highest_(high, length)

 

falling / rising:

rising_(values, length) =>
current_ = values[0]
ok_ = false
if length>0 and length<=bar_index
ok_ := true
for i = 1 to length
if ( not na(values[i]) and current_ <= values[i] )
ok_ := false
break
ok_


falling_(values, length) =>
current_ = values[0]
ok_ = false
if length>0 and length<=bar_index
ok_ := true
for i = 1 to length
if ( not na(values[i]) and current_ >= values[i] )
ok_ := false
break
ok_

You can use:

myVar = rising_(close, length)

 

3 thoughts on “[Solution] Highest/Lowest/Falling/Rising functions with series-integer in PineScript

  1. Really helpful! I was wondering if you could also do something like this for the rising() and falling() methods, which suffer the same limitations in pine?

    1. Was added `rising/falling` too

  2. Thank you for making this! Very helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *