# bb_bands_cross_rev_00b_lower
#https://usethinkscript.com/threads/need-a-bollinger-band-study-and-or-scan.14603/
#Need a Bollinger Band study and/or scan
#ttsdmagic
#2/24
#I want to scan my watchlist (over 300 symbols) to get a count of how many symbols have candles that have crossed the upper (or lower) Bollinger band, and are now heading in the opposite direction. I would like this for the Daily time frame. I find that when that happens to a large number of symbols, it is very likely a "moving day" where trades are plentiful. Any help with this is appreciated.
# assign # for each step
# 2 = close > upper Bollinger band and close < close[1] (dropping)
# 1 = close crossed above upper Bollinger band and close > close[1] (rising)
# 0 = cancel ... #close crosses below upper or close crosses above lower
# -1 = close crossed below lower Bollinger band and close < close[1] (dropping)
# -2 = close < lower Bollinger band and close > close[1] (rising)
declare lower;
def bn = barnumber();
def na = double.nan;
def bbupper = BollingerBands().UpperBand;
def bblower = BollingerBands().LowerBand;
def bbmid = BollingerBands().MidLine;
def t = if bn == 1 then 0
else if close crosses below bbmid or close crosses above bbmid then 0
else if close > bbupper and close[1] < bbupper then 1
else if close < bblower and close[1] > bblower then -1
else if t[1] == 1 and close < close[1] then 2
else if t[1] == -1 and close > close[1] then -2
else t[1];
# wedges - crossing
def z1 = if t == 1 and t[1] != 1 then 1 else 0;
def z2 = if t == -1 and t[1] != -1 then 1 else 0;
# arrows - reversal
def z3 = if t == 2 and t[1] != 2 then 1 else 0;
def z4 = if t == -2 and t[1] != -2 then 1 else 0;
# reversal signals
plot z = if z3 or z4 then 1 else 0;
addlabel(0,
(if z1 then "xup"
else if z2 then "xdwn"
else if z3 then "REV dwn"
else if z4 then "REV up"
else "-")
, (if z1 then color.cyan
else if z2 then color.yellow
else if z3 then color.red
else if z4 then color.green
else color.gray)
);
#