Woutorius
Woutorius

Reputation: 1

Pinescript tradingview indicator on previous candle value

I am writing a code that is triggered on the current candle value, but uses the previous candle's as well. I was successful however this code looks probably kind of funny :)

Is there an easier way of using the previous candle value, i.o. repeating [..]?

Thanks a lot.

signal6a =( signal6[12] or signal6[11] or signal6[10] or signal6[9] or signal6[8] or signal6[7] or signal6[6] or signal6[5] or signal6[4] or signal6[3] or signal6[2] or signal6[1] or signal6 or signal6[13] or signal6[14] or signal6[15] or signal6[16] or signal6[17] or signal6[18] or signal6[19] or signal6[20] or signal6[21] or signal6[22] or signal6[23] or signal6[24] or signal6[25] or signal6[26] or signal6[27] or signal6[28] or signal6[29] or signal6[30] or signal6[31] or signal6[32] or signal6[33] or signal6[34] or signal6[35] or signal6[36] or signal6[37] or signal6[38] or signal6[39] or signal6[40]) and high >ta.ema(high, 8) 

Upvotes: 0

Views: 1034

Answers (2)

vitruvius
vitruvius

Reputation: 21342

You can use the math.sum() function for this purpose.

If you want to check if signal6 was true for 40 bars consecutievly, you would do it like below:

is_signal6a = math.sum(signal6 ? 1 : 0, 40) == 40

It will lookback the last 40 candles and return the number of times signal6 is true. If that rolling sum is equal to 40, you know that signal6 was true for the last 40 candles.

If you want to check if signal6 was true for at least one bar (this is your case), you would do it like below:

is_signal6a = math.sum(signal6 ? 1 : 0, 40) > 0

Upvotes: 2

mr_statler
mr_statler

Reputation: 2171

I would personally would use a custom function to check if signal6 is true:

checkForTrue(source, len) =>
    p = false
    for i=0 to len
        p := source[i]
        if p
            break
    p       

Once you have this function, your code can be written as:

signal6a = checkForTrue(signal6, 40) and high > ta.ema(high, 8)

Upvotes: 0

Related Questions