#price_below_ema_day_close
#https://usethinkscript.com/threads/drop-below-9-ema-and-20-ema-on-the-daily-candle.16786/
#Drop Below 9 ema and 20 ema on the Daily Candle
#pgoebel6 10/11
# scan the market at close for stocks that,
# open above the 9 and 20 ema and closed below both of them
# also the 9 ema > 20 ema
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
def na = double.nan;
def bn = barnumber();
def price = close;
#--------------------
# avg 1 & 2
input ma1_len = 9;
def len1 = if ma1_len == 0 then 1 else ma1_len;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, len1);
input ma2_len = 20;
def len2 = if ma2_len == 0 then 1 else ma2_len;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = Movingaverage(ma2_type, price, len2);
def xup = ma1 crosses above ma2;
def xdwn = ma1 crosses below ma2;
def up = ma1 > ma2;
def dwn = ma1 < ma2;
# open above the 9 and 20 ema and closed below both of them
def sell_rule1 = (open > ma1 and open > ma2 and close < ma1 and close < ma2);
# also the 9 ema > 20 ema
def sell_rule2 = (ma1 > ma2);
def sell = (sell_rule1 and sell_rule2);
def buy_rule1 = (open < ma1 and open < ma2 and close > ma1 and close > ma2);
def buy_rule2 = (ma1 < ma2);
def buy = (buy_rule1 and buy_rule2);
addchartbubble(1, low*0.995,
buy + "\n" +
sell
, (if buy then color.green else if sell then color.red else color.gray), no);
# AssignBackgroundColor
input show_lines = yes;
plot z1 = if show_lines and len1 > 1 then ma1 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = if show_lines and len2 > 1 then ma2 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();
#