# avg_bullbear_scan_00
#https://usethinkscript.com/threads/bullish-and-bearish-scanner.14164/
#Bullish and Bearish Scanner
# Lds3435 1/22
#Trying to create a scanner with this condition:
#Bullish:
#low is above the 21 day EMA for a least 10 days
#21 day EMA is above the 50 day SMA for at least 5 days
#50 day SMA is in an uptrend
#close up for the day
#Bearish:
#21 day EMA crosses under 50 SMA
#Close 10% below recent high and below the 50 day SMA
#------------
def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
input duration1_bars = 10;
input duration2_bars = 5;
def price = close;
input ma1_len = 21;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 50;
input ma2_type = AverageType.simple;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
#-----------------------
# bull
#low is above the 21 day EMA for a least 10 days
#21 day EMA is above the 50 day SMA for at least 5 days
#50 day SMA is in an uptrend
#close up for the day
def bull1 = sum((low > ma1), duration1_bars) == duration1_bars;
def bull2 = sum((ma1 > ma2), duration2_bars) == duration2_bars;
def bull3 = ma2 > ma2[1];
def bull4 = close > close[1];
def bull = bull1 and bull2 and bull3 and bull4;
#Bearish:
#21 day EMA crosses under 50 SMA
#Close 10% below recent high and below the 50 day SMA
input per = 0.5;
def bear1 = ma1 crosses below ma2;
def bear2 = (close < ((1 - (per/100))*high[1]) and close < ma2);
def bear = bear1 and bear2;
# ---------------------
input show_vertical_lines = yes;
addverticalline(show_vertical_lines and bull, "-", color.green);
addverticalline(show_vertical_lines and bear, "-", color.red);
#------------------
input show_average_lines = yes;
plot z1 = if show_average_lines then ma1 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = if show_average_lines then ma2 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();
#