#scan_excludenear_earnings_lower
#https://usethinkscript.com/threads/scan-for-sma-and-rsi-exclude-those-reporting-earnings.19510/
#scan for sma and rsi & exclude those reporting earnings
#bcbbab Aug 25, 2024
#Just looking everywhere and Can't seem to find these. I want a TOS scan that will scan stocks that meet these requirements;
# 1) stock price close above 200 day sma
# 2) yesterday price was under 200 day sma
# 3) price above 10 day ema
# 4) RSI less than 70
# 5) does not have earnings before or after 2 days
declare lower;
# time = day
def na = Double.NaN;
def bn = BarNumber();
def data = close;
# rule 1, 2
# 1) stock price close above 200 day sma
# 2) yesterday price was under 200 day sma
input avg1_type = AverageType.SIMPLE;
input avg1_length = 200;
def avg1 = MovingAverage(avg1_type, data, avg1_length );
def r1 = close > avg1;
def r2 = close[1] < avg1[1];
# rule 3
# 3) price above 10 day ema
input avg2_type = AverageType.EXPONENTIAL;
input avg2_length = 10;
def avg2 = MovingAverage(avg2_type, data, avg2_length );
def r3 = close > avg2;
# rule 4
# 4) RSI less than 70
input rsi_length = 14;
input rsi_over_Bought = 70;
input rsi_over_Sold = 30;
input rsi_price = close;
input rsi_averageType = AverageType.WILDERS;
def NetChgAvg = MovingAverage(rsi_averageType, rsi_price - rsi_price[1], rsi_length);
def TotChgAvg = MovingAverage(rsi_averageType, AbsValue(rsi_price - rsi_price[1]), rsi_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def r4 = rsi < rsi_over_Bought;
#rule 5
# 5) does not have earnings before or after 2 days
input earnings_days = 2;
#ref
#https://usethinkscript.com/threads/getting-next-earnings-date-in-watchlist-column.19557/
#in a column, can look at 30 future days to check for earnings
#days to prev earnings
def PE = GetEventOffset(Events.EARNINGS, -1);
#days to next earnings
def NE = GetEventOffset(Events.EARNINGS, 0);
def r5 = pe > earnings_days and absvalue(ne) > earnings_days ;
plot scan = r1 and r2 and r3 and r4 and r5;
#----------------------------------
# remove code after this line when using as a scan
def f = 0.15;
def y = 1.4;
def iscls = isnan(close);
plot z1 = if iscls then na else (r1*f) - (1*f*y);
plot z2 = if iscls then na else (r2*f) - (2*f*y);
plot z3 = if iscls then na else (r3*f) - (3*f*y);
plot z4 = if iscls then na else (r4*f) - (4*f*y);
plot z5 = if iscls then na else (r5*f) - (5*f*y);
plot w = if iscls then na else 1.2;
w.setdefaultcolor(color.black);
def t = 5;
def x = !isnan(close[t+1]) and isnan(close[t]);
addchartbubble(x, 0,
"close > 200SMA\n" +
"yesterday close < 200SMA\n" +
"close > 10EMA\n" +
"RSI < 70\n" +
"no earnings within 2 days"
, color.yellow, no);
input show_avg1_line = no;
input show_avg2_line = no;
plot zavg1 = if show_avg1_line then avg1 else na;
plot zavg2 = if show_avg2_line then avg2 else na;
zavg1.SetDefaultColor(Color.CYAN);
zavg1.SetLineWeight(1);
zavg1.HideBubble();
zavg2.SetDefaultColor(Color.YELLOW);
zavg2.SetLineWeight(1);
zavg2.HideBubble();
#