# Rate of Change
# cabe1332
#The Rate Of Change (ROC) is an oscillator calculating the percentage change of the security price relative to the price a specified number (length) of periods before. The higher the ROC, the more overbought the security; when the ROC falls, a rally might occur.
# 10/20/2021 Rev.1
# TOC ROC with labels
# 10/23/2021 Rev.2
# added support and condition for bulls and bears labels
# added RSI and EMA stacked for better entry/exit labels
# added support for Profit Taking conditions with labels
## code start
declare lower;
input length = 7;
input colorNormLength = 7;
input price = close;
input bullthreshold = 2;
input bearthreshold = -2;
Assert(length > 0, "'length' must be positive: " + length);
plot ROC = if price[length] != 0 then (price / price[length] - 1) * 100 else 0;
ROC.DefineColor("Highest", Color.GREEN);
ROC.DefineColor("Lowest", Color.RED);
ROC.AssignNormGradientColor(colorNormLength, ROC.Color("Lowest"), ROC.Color("Highest"));
ROC.SetLineWeight(2);
plot zeroline = if !IsNaN(close) then 0 else Double.NaN;
zeroline.AssignValueColor(if ROC > 0 then Color.GREEN else Color.LIGHT_RED);
zeroline.SetPaintingStrategy(PaintingStrategy.dashES);
zeroline.SetLineWeight(1);
# RSI
def rsi = RSI()."RSI" is greater than or equal to 30 and RSI()."RSI" is less than or equal to 70 within 2 bar;
#EMAstacked#
def BULLISHstackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 50)."AvgExp" within 2 bars;
def BEARstackedUp = MovAvgExponential("length" = 8)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 50)."AvgExp" within 2 bars;
# ROC increasing or decreasing label
AddLabel(yes, if ROC > ROC[1] then " ROC Increasing " + Round(ROC, 2) + " " else " ROC Decreasing " + Round(ROC, 2) + " ", if ROC > ROC[1] then Color.GREEN else Color.RED);
# BULLISH OR BULLISH OR NEUTRAL
AddLabel(yes, if ROC >= bullthreshold then " BULLISH " else if ROC <= bearthreshold then " BEARISH " else " Wait & Watch ", if ROC >= bullthreshold then Color.GREEN else if ROC <= bearthreshold then Color.RED else Color.WHITE);
# GOING LONG OR SHORT BUY
def LONGBUY = ROC >= bullthreshold and rsi and BULLISHstackedUp;
def SHORTBUY = ROC <= bearthreshold and rsi and BEARstackedUp;
AddLabel(yes, if LONGBUY then " BUY LONG " else if SHORTBUY then " Sell SHORT " else "", if LONGBUY then Color.GREEN else if SHORTBUY then Color.RED else Color.WHITE);
# PERSONAL MESSAGE TO TAKE PROFITS
def TakeProfit = (ROC between 10 and 20) or (ROC between -10 and -20);
def SeriouslyTakeProfit = (ROC between 21 and 50) or (ROC between -21 and -50);
def FinTakeProfit = (ROC between 51 and 99) or (ROC between -51 and -99);
def DontBeLoser = (ROC >= 100) or (ROC <= -100);
AddLabel(yes, if TakeProfit then " Take Profit! " else if SeriouslyTakeProfit then " SERIOUSLY, Take Profit! " else if FinTakeProfit then " ****IN! Take Profit! " else if DontBeLoser then " DON'T BE A LOSER! Take Profit! " else "", if TakeProfit then Color.GREEN else if SeriouslyTakeProfit then Color.CYAN else if FinTakeProfit then Color.ORANGE else if DontBeLoser then Color.MAGENTA else Color.BLACK);
DefineGlobalColor("Bullish", Color.cyan);
DefineGlobalColor("Bearish", Color.pink);
AddCloud(ROC, zeroline , globalColor("Bullish"), globalColor("Bearish"));
## end code