#alert_ema_rsi
#https://usethinkscript.com/threads/ema-rsi-alert.17996/
#EMA - RSI Alert
# alerts would be as follows:
# On the 5 min chart i use the following ema's
# 9 13 34 50 72 89
#The long alert is
#If the 34 is above the 50 and
#50 is above the 72 and the
#72 is above the 89 and
#The RSI is below 60 and
#The RSI is above 30
#Send Long alert if the last price is = to the 89 ema
#The short alert is as follows:
#If the 89 is above the 72 and
#The 72 is above the 50 and
#The 50 is above the 34 and
#Rsi is below 60
#Send short alert is last price is = to the 89 ema
def na = Double.NaN;
def bn = BarNumber();
def data = close;
# EMA 9 13 34 50 72 89
input avg1_type = AverageType.EXPONENTIAL;
#input avg1_type = AverageType.SIMPLE;
input avg1_length = 9;
def avg1 = MovingAverage(avg1_type, data, avg1_length );
input avg2_type = AverageType.EXPONENTIAL;
input avg2_length = 13;
def avg2 = MovingAverage(avg2_type, data, avg2_length );
input avg3_type = AverageType.EXPONENTIAL;
input avg3_length = 34;
def avg3 = MovingAverage(avg3_type, data, avg3_length );
input avg4_type = AverageType.EXPONENTIAL;
input avg4_length = 50;
def avg4 = MovingAverage(avg4_type, data, avg4_length );
input avg5_type = AverageType.EXPONENTIAL;
input avg5_length = 72;
def avg5 = MovingAverage(avg5_type, data, avg5_length );
input avg6_type = AverageType.EXPONENTIAL;
input avg6_length = 89;
def avg6 = MovingAverage(avg6_type, data, avg6_length );
input show_average_lines = yes;
plot zavg1 = if show_average_lines and 0 then avg1 else na;
plot zavg2 = if show_average_lines and 0 then avg2 else na;
plot zavg3 = if show_average_lines then avg3 else na;
plot zavg4 = if show_average_lines then avg4 else na;
plot zavg5 = if show_average_lines then avg5 else na;
plot zavg6 = if show_average_lines then avg6 else na;
zavg1.SetDefaultColor(Color.gray);
zavg1.SetLineWeight(1);
zavg1.HideBubble();
zavg2.SetDefaultColor(Color.gray);
zavg2.SetLineWeight(1);
zavg2.HideBubble();
zavg3.SetDefaultColor(Color.gray);
zavg3.SetLineWeight(1);
zavg3.HideBubble();
zavg4.SetDefaultColor(Color.gray);
zavg4.SetLineWeight(1);
zavg4.HideBubble();
zavg5.SetDefaultColor(Color.gray);
zavg5.SetLineWeight(1);
zavg5.HideBubble();
zavg6.SetDefaultColor(Color.cyan);
zavg6.SetLineWeight(1);
zavg6.HideBubble();
#----------------------------
# rsi
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 RSI1 = 50 * (ChgRatio + 1);
def OverSold = rsi_over_Sold;
def OverBought = rsi_over_Bought;
def UpSignal = if RSI1 crosses above OverSold then OverSold else Double.NaN;
def DownSignal = if RSI1 crosses below OverBought then OverBought else Double.NaN;
#RSI.DefineColor("OverBought", GetColor(5));
#RSI.DefineColor("Normal", GetColor(7));
#RSI.DefineColor("OverSold", GetColor(1));
#RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then #RSI.color("OverSold") else RSI.color("Normal"));
#OverSold.SetDefaultColor(GetColor(8));
#OverBought.SetDefaultColor(GetColor(8));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#
#----------------------------
# long
# If the 34 is above the 50 and
# 50 is above the 72 and the
# 72 is above the 89 and
# The RSI is below 60 and
# The RSI is above 30
# last price is = to the 89 ema
def long1 =
avg3 > avg4
and avg4 > avg5
and avg5 > avg6
and rsi1 < 60
and rsi1 > 30
and (low < avg6 and high > avg6);
plot zup = if long1 then low*0.999 else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(3);
zup.hidebubble();
#addverticalline(long1, "long", color.green);
addchartbubble(0, low*0.997,
(avg3 > avg4) + "\n" +
(avg4 > avg5) + "\n" +
(avg5 > avg6) + "\n" +
(rsi1 < 60) + "\n" +
(rsi1 > 30) + "\n" +
#and data crosses avg6;
(low < avg6 and high > avg6)
, color.yellow, no);
#----------------------------
# short
# If the 34 is below the 50 and
# 50 is below the 72 and the
# 72 is below the 89 and
# The RSI is below 60 and
# The RSI is above 30
# last price is = to the 89 ema
def short1 =
avg3 < avg4
and avg4 < avg5
and avg5 < avg6
and rsi1 < 60
and rsi1 > 30
and (low < avg6 and high > avg6);
plot zdwn = if short1 then high*1.001 else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_down);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(3);
zdwn.hidebubble();
#
alert(long1, "up" ,alert.BAR, sound.DING);
alert(short1, "down" ,alert.BAR, sound.bell);
#