Two condition true in specific period

118 Views Asked by At

I want to write an indicator on tradingview solve one pine script problem. I have three indicators (for example MACD, EMA14 and volume) and I want to check these indicators for specific period. If in two periods ta.crossover(macdLine, macdSignal) and close>ema14 and volume>volumeavg14 has to be true. How can I make it. Could you explain to me?

Conditions can be true different bars and different order. Important thing is all conditions have to be true in specific period.

1

There are 1 best solutions below

0
vitruvius On BEST ANSWER

You can use the ta.barssince() function to detect how long it has been since a condition was true. This way, you can define your periods.

Below example shows a three bar period (marked by Xs) when the price crosses over EMA14.

You would do the same for your other conditions and finally and them together.

//@version=5
indicator("My script", overlay=true)

ema_14 = ta.ema(close, 14)
ema_cross = ta.crossover(close, ema_14)
bs_ema_cross = ta.barssince(ema_cross)

ema_cond = ema_cross or ((bs_ema_cross > 0) and (bs_ema_cross < 3))

plot(ema_14)
plotshape(ema_cond)

enter image description here