paamachat
paamachat

Reputation: 73

Pine Script - Extract the highest value from visible bars only; Study Error: Error on bar 0: invalid value of 'length' in'highest' function. Must be>0

I’m trying to dynamically determine the highest values of various visible bars and indicators as you scroll thru the chart.

The following code was used to identify the total number of visible bars on the chart. The code plotted the returned values allowing me to confirm they were correct, and that they adjusted dynamically as you scroll through the chart:

VisibleBars() =>
    bool result = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time

TotalBars() => 
    var int result = 0
    if VisibleBars()
        result += 1
    result

T = TotalBars()
plot(T,"Total")

Everything performs as expected so far. Here's the problem. I then try to determine the highest value of a particular series that I visible on the chart. Here's an example using closing price to illustrate the problem I'm facing.

var float HighClose = na
HighClose := ta.highest(close,T)
plot(HighClose,"highest close", color=color.white)

With this addition, the program still compiles but won't plot any data. Instead, it returns the error: "Study Error". Error on bar 0: invalid value of the 'length' in the "highest" function. It must be > 0."

Note that the plot clearly shows that T is > 0, but the error message says it is negative. If I go back to the code and replace T with the actual numerical value of T shown in the plot, then everything works as expected. So the problem seems to be that the ta.highest function is not interpreting the length T properly.

I also tried math.abs and int to force T to be positive and an integer, even though it already is. Same error. Any ideas?

Update: Based in part on comments below the code below solved the problem. Math.max was used to "remove the zero" from T which resolved the study error. This worked perfectly over small time periods, but for longer periods the buffer had to be enlarged by adding the 3rd line in the code below.

var float HighClose = na
T2 = math.max(1, T)                                        
T2_passed_length = bar_index == 0 ? 1000 : T2
HighestClose := ta.highest(close,T2_passed_length)  
plot(HighClose,"highest close", color=color.white)

Upvotes: 1

Views: 1540

Answers (3)

paamachat
paamachat

Reputation: 73

Based in part on comments provided, the code below solved the problem. Math.max was used to "remove the zero" from T which resolved the study error. This worked perfectly over small time periods, but for longer periods the buffer had to be enlarged by adding the 3rd line in the code below.

var float HighClose = na
T2 = math.max(1, T)                                        
T2_passed_length = bar_index == 0 ? 1000 : T2
HighestClose := ta.highest(close,T2_passed_length)  
plot(HighClose,"highest close", color=color.white)

Upvotes: 0

Gu5tavo71
Gu5tavo71

Reputation: 1214

For the highest value of close, try this:

HighClose := T == 0 ? close : T >= 1 ? math.max(close, HighClose[1]): HighClose[1]

Upvotes: 0

Gu5tavo71
Gu5tavo71

Reputation: 1214

Assign a value to the first candle in your range. When T = 0
Something like this:

var float HighClose = na
HighClose := T == 0 ? close : ta.highest(close,T)
plot(HighClose,"highest close", color=color.white)

Upvotes: 0

Related Questions