Is there a way to count non-consecutive and consecutive candles or to solve my problem in TradingView?

Viewed 40

My stratedy is based on conditions:

From the last 4 candles, if:

3 out of 4 candles are with the trend, take the trade. BUT: if the last candle is against the trend, don't take the trade.

It sounds confusing but I attached the picture, so is stupid simple, however I have NO IDEA how to do this on pine, I am new at this and a little help would be appreciated. Thanks.

2 Answers

Like this

ThreeValidInLastFour = ta.sum(condition ? 1 : 0, 4) >= 3

A little bit update to Daveatt's answer, because you say you won't take the trade if the last candle is not in the trend:

ThreeValidInLastFour = ta.sum(condition ? 1 : 0, 4) >= 3 and condition

Related