#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#
declare lower;
input length = 20;
input momentum = 5;
assert(momentum > 0, "'momentum' must be positive: " + momentum);
def emaInc = ExpAverage(Max(close - close[momentum], 0), length);
def emaDec = ExpAverage(Max(close[momentum] - close, 0), length);
plot RMI = if emaDec == 0 then 0 else 100 - 100 / (1 + emaInc / emaDec);
plot OverBought = 70;
plot OverSold = 30;
RMI.SetDefaultColor(GetColor(1));
OverBought.SetDefaultColor(GetColor(8));
OverSold.SetDefaultColor(GetColor(8));
def condition1 = RMI crosses above OverSold;
def condition2 = RMI crosses below OverBought;
# Alerts
Alert(condition1, " ", Alert.Bar, Sound.Chimes);
Alert(condition2, " ", Alert.Bar, Sound.Bell);