Pine Script: All three indicators have to confirm trend
My strategies is based on 3 different indicators. As a condition all of them have to be false at least on one candle before the main indicator becomes true. In my example candle 4 does not get a trigger, as indicator 2 was not false on candle 3. Candle 10 is true for the main indicator, as all indicators were false either on candle 8 or 9.
bar | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
indicator 1 | true | true | false | true | true | false | true | true | false | true | ... | ... |
indicator 2 | false | true | true | true | false | false | true | true | false | true | ... | ... |
indicator 3 | false | true | false | true | false | false | true | false | true | true | ... | ... |
melting indicators | false | true | false | false (not all were false on #3) | false | false | true | false | true | true | ... | ... |
You would help me a lot!
Solution 1:
If I understand correctly, you need the melting indicator to be true
when indicators 1-2-3 are true
, but only if indicators 1-2-3 have been false
at any point in the previous 2 bars.
This pseudo-code would do that.
//@version=5
indicator("My Script", overlay=true)
var bool a = na // indicator 1
var bool b = na // indicator 2
var bool c = na // indicator 3
var bool m = na // melting indicator
m := a and b and c and (not a[1] or not a[2]) and (not b[1] or not b[2]) and (not c[1] or not c[2])
plot(na)
Or you could do it with a function, which improves the code readability.
//@version=5
indicator("My Script", overlay=true)
var bool a = na // indicator 1
var bool b = na // indicator 2
var bool c = na // indicator 3
var bool m = na // melting indicator
f_check(bool _x) => _x and (not _x[1] or not _x[2])
m := f_check(a) and f_check(b) and f_check(c)
plot(na)
Edit 1 in response to this comment :
Example with undefined number of candles
//@version=5
indicator("My Script", overlay=true)
var bool a = na // indicator 1
var bool b = na // indicator 2
var bool c = na // indicator 3
var bool m = na // melting indicator
var bool a_triggered = false
var bool b_triggered = false
var bool c_triggered = false
// once triggered, keep triggered until reset
a_triggered := not a or a_triggered
b_triggered := not b or b_triggered
c_triggered := not c or c_triggered
// melting indicator will ony be able to be true when conditions are met for a, b, c AND if all triggers have fired since last reset
m := a and b and c and a_triggered and b_triggered and c_triggered
// melting indicator is true?
if m
// reset triggers
a_triggered := false
b_triggered := false
c_triggered := false
plot(na)