# RSI_Trend_Clouds
# Derived from TOS standard RSI Study
# Created by rad14733
# v1.0 : 2021-10-29 : Initial Release
# v1.1 : 2022-04-04 : Minor variable name changes for clarity
# v1.2 : 2022-04-13 : Modified to allow user-selectable cloud colors using Globals.
# v1.3 : 2022-06-10 : Added trend dot feature to midline.
# v1.4 : 2022-10-10 : Added independent average type for avgRSI.
# v1.5 : 2023-02-14 : Added ability to enable/disable clouds
# v1.6 : 2023-12-04 : Added Trend label
# v1.7 : 2024-12-17 : Corrected cloud logic for proper color painting
declare lower;
input length = 14;
input avgAvgLength = 14;
input price = close;
input overSold = 30;
input overBought = 70;
input avgType = AverageType.WILDERS;
input avgAvgType = AverageType.WILDERS;
input showTrueRsiLabel = no;
input showAvgRsiLabel = no;
input showClouds = yes;
def rsi = RSI(length, overBought, overSold, price, avgType, no);
plot rsiLine = rsi;
rsiLine.SetPaintingStrategy(PaintingStrategy.LINE);
rsiLine.SetDefaultColor(Color.CYAN);
rsiLine.SetLineWeight(2);
plot midline = 50;
midline.SetDefaultColor(Color.YELLOW);
midLine.SetStyle(Curve.FIRM);
midLine.SetPaintingSTrategy(PaintingStrategy.LINE_VS_POINTS);
midLine.SetLineWeight(1);
plot overSoldLevel = overSold;
overSoldLevel.SetDefaultColor(Color.YELLOW);
overSoldLevel.SetPaintingStrategy(PaintingStrategy.LINE);
overSoldLevel.SetStyle(Curve.SHORT_DASH);
overSoldLevel.SetLineWeight(1);
plot overBoughLevel = overBought;
overBoughLevel.SetDefaultColor(Color.YELLOW);
overBoughLevel.SetPaintingStrategy(PaintingStrategy.LINE);
overBoughLevel.SetStyle(Curve.SHORT_DASH);
overBoughLevel.SetLineWeight(1);
# Alert Code
#input useAlerts = no;
#Alert(useAlerts and rsiLine crosses above midline, "RSI_HistoAlert xUp", Alert.BAR, Sound.Chimes);
#Alert(useAlerts and rsiLine crosses below midline, "RSI_HistoAlert xDown", Alert.BAR, Sound.Chimes);
plot avgRsi = MovingAverage(avgAvgType, rsi, avgAvgLength);
#avgRsi.SetDefaultColor(Color.WHITE);
avgRsi.AssignValueColor(if avgRsi > avgRsi[1] then Color.UPTICK else if avgRsi < avgRsi[1] then Color.DOWNTICK else Color.CURRENT);
avgRsi.SetLineWeight(2);
DefineGlobalColor("UpTrend", Color.UPTICK);
DefineGlobalColor("DnTrend", Color.DOWNTICK);
#midline.AssignValueColor(if rsi > avgRsi then GlobalColor("UpTrend") else GlobalColor("DnTrend"));
midline.AssignValueColor(if avgRsi > avgRsi[1] then GlobalColor("UpTrend") else if avgRsi < avgRsi[1] then GlobalColor("DnTrend") else Color.CURRENT);
AddCloud(rsi, avgRsi, if showClouds then GlobalColor("UpTrend") else Color.CURRENT, if showClouds then GlobalColor("DnTrend") else Color.CURRENT);
AddLabel(showTrueRsiLabel, "RSI: " + Round(rsi, 0), if rsi >= 70 then Color.ORANGE else if rsi <= 30 then Color.CYAN else Color.WHITE);
AddLabel(showAvgRsiLabel, "Avg RSI: " + Round(avgRsi, 0), if avgRsi >= 70 then Color.ORANGE else if avgRsi <= 30 then Color.CYAN else Color.WHITE);
AddCloud(overBought, Double.POSITIVE_INFINITY, if showClouds then Color.WHITE else Color.CURRENT, if showClouds then Color.DOWNTICK else Color.CURRENT);
AddCloud(overSold, Double.NEGATIVE_INFINITY, if showClouds then Color.UPTICK else Color.CURRENT, if showClouds then Color.WHITE else Color.CURRENT);
AddLabel(yes, " Trend ", if rsi > avgRsi then Color.GREEN else Color.RED);
# END - RSI_Trend_Clouds