问题
I'd like to check if ANY candle in the past 'n' candles meet a certain condition.
E.g. let's check if any close in the latest 20 candles was higher than 'x':
x = 2
n = 20
condition = [ANY of n] > x
回答1:
See barssince() and an example showing how to use it here.
You can also count the number of occurrences of a condition in the last n bars using:
sum(cond ? 1 : 0, len)
Disclosure: the link in this answer points to a PineCoders FAQ entry.
I am a member of the PineCoders community and I most probably wrote that FAQ entry. PineCoders is a TradingView-supported group of volunteer Pine coders and PineCoders' website is strictly educational. Neither TradingView nor PineCoders benefits financially from sending traffic to pinecoders.com, and the site contains no affiliate/referral links.
来源:https://stackoverflow.com/questions/65621050/how-to-detect-if-in-any-candle-in-the-past-n-candles-meet-a-certain-requiremen