How can I get a value since a condition happen? (Pinescript)

Currently, I'm having trouble with getting a value since a condition happened. Here is the Image

And below is the simplified code (in Pinescript, version 5)

Cond_1 = ta.crossover(ema_1, ema_2)
Cond_2 = ta.crossunder(ema_1, ema_2)
       //I want to get the highest value since Cond_1, so I added ta.barssince() and ta.highest()
Bars_count = ta.barssince(Cond_1)
Highest = ta.highest(Bars_count)
Plot(Highest)

But the problem is it couldn't be compiled. It sent me this:

Study error. Invalid value of the 'length' argument (NaN) in the "Highest" function. It must be > 0

I tried to put fixnan() before the "highest" function or even add "+1" to "Bars_count" but they didn't help

Cond_1 = ta.crossover(ema_1, ema_2)
Cond_2 = ta.crossunder(ema_1, ema_2)
Bars_count = ta.barssince(Cond_1) + 1
       //(This is to make sure the results are always > 0)
Highest = fixnan(ta.highest(Bars_count))
Plot(Highest)

Is there any solution for this? Thank you all <3

Upvotes: 1

Views: 1778

Answers (1)

Rohit Agarwal
Rohit Agarwal

Reputation: 1368

You can use the nz function to set Bars_count to 0. And if Bars_count<=0 then set it to 1.

//@version=5
indicator(title="Test", overlay=true,max_bars_back=1500)
ema_1=ta.ema(close,50)
ema_2=ta.ema(close,200)
plot(ema_1)
plot(ema_2)
Cond_1 = ta.crossover(ema_1, ema_2)
Cond_2 = ta.crossunder(ema_1, ema_2)
       //I want to get the highest value since Cond_1, so I added ta.barssince() and ta.highest()
Bars_count = ta.barssince(Cond_1)
Bars_count:=nz(Bars_count)
if Bars_count<=0
    Bars_count:=1
Highest = ta.highest(Bars_count)
plot(Highest)

enter image description here

Upvotes: 2

Related Questions