#// Indicator for TOS
#// © Amphibiantrading
#indicator("Webby's Quick & Grateful Dead RS", overlay = true, shorttitle = 'QGDRS')
# Converted by Sam4Cok@Samer800 - 12/2024
declare lower;
input index = "SPX"; # 'Index to Compare'
input ma1Type = AverageType.SIMPLE; #('SMA',
input ma1Len = 21; #, 'Quick Moving Average', mi
input ma2Type = AverageType.SIMPLE; #('SMA', ' ', options = ['EMA', 'SMA'], inline = '3', group = g2)
input ma2Len = 50; #, 'Slow Moving Average', minval = 3, maxval = 200, step = 1, group = g2, inline = '3')
input ma3Type = AverageType.SIMPLE; #('SMA', ' ', options = ['EMA', 'SMA'], inline = '2', group = g3)
input ma3Len = 8; #, 'Quick Moving Average', minval = 3, maxval = 50, step = 1, group = g3, inline = '2')
input ma4Type = AverageType.SIMPLE; #('SMA', ' ', options = ['EMA', 'SMA'], inline = '3', group = g3)
input ma4Len = 21; #, 'Slow Moving Average', minval = 3, maxval = 200, step = 1, group = g3, inline = '3')
input ma5Type = AverageType.SIMPLE; #('SMA', ' ', options = ['EMA', 'SMA'], inline = '2', group = g4)
input ma5Len = 12; #, 'Quick Moving Average', minval = 3, maxval = 50, step = 1, group = g4, inline = '2')
input ma6Type = AverageType.SIMPLE; #('SMA', ' ', options = ['EMA', 'SMA'], inline = '3', group = g4)
input ma6Len = 24; #, 'Slow Moving Average', minval = 3, maxval = 200, step = 1, group = g4, inline = '3')
input multi = 35; #, 'Vertical offset of RS Line', minval=1, maxval=500, step=1, group = g5)
def na = Double.NaN;
def cap = GetAggregationPeriod();
def isdaily = cap >= AggregationPeriod.DAY and cap < AggregationPeriod.WEEK;
def isweekly = cap >= AggregationPeriod.WEEK and cap < AggregationPeriod.MONTH;
def ismonthly = cap >= AggregationPeriod.MONTH;
def spx = if close(Symbol = index) then close(Symbol = index) else spx[1];
def rs = close / spx * 100;
def ma1 = MovingAverage(ma1Type, rs, ma1Len);
def ma2 = MovingAverage(ma2Type, rs, ma2Len);
def ma3 = MovingAverage(ma3Type, rs, ma3Len);
def ma4 = MovingAverage(ma4Type, rs, ma4Len);
def ma5 = MovingAverage(ma5Type, rs, ma5Len);
def ma6 = MovingAverage(ma6Type, rs, ma6Len);
#//variables
def qsLev;
def qsBool;
#// conditions
def quickBreak; def quickSand; def gdb; def reset;
if isdaily {
quickBreak = (rs < ma1) and (rs[1] >= ma1[1]);
quickSand = rs < ma1 and rs < qsLev[1] and !qsBool[1];
gdb = (rs < ma2) and (rs[1] >= ma2[1]);
reset = (rs > ma1) and (rs[1] <= ma1[1]);
} else if isweekly {
quickBreak = (rs < ma3) and (rs[1] >= ma3[1]);
quickSand = rs < ma3 and rs < qsLev[1] and !qsBool[1];
gdb = (rs < ma4) and (rs[1] >= ma4[1]);
reset = (rs > ma3) and (rs[1] <= ma3[1]);
} else {
quickBreak = (rs < ma5) and (rs[1] >= ma5[1]);;
quickSand = rs < ma5 and rs < qsLev[1] and !qsBool[1];
gdb = (rs < ma6) and (rs[1] >= ma6[1]);
reset = (rs > ma5) and (rs[1] <= ma5[1]);
}
if quickBreak {
qsLev = rs;
qsBool = qsBool[1];
} else if quickSand {
qsLev = qsLev[1];
qsBool = yes;
} else if reset {
qsLev = 0;
qsBool = no;
} else {
qsLev = qsLev[1];
qsBool = qsBool[1];
}
plot rsLine = rs * multi; #, 'RS', lineCol, rsWidth)
plot maD1 = if isdaily then ma1 * multi else na; #, 'Fast MA', ma1Color, maWidth)
plot maD2 = if isdaily then ma2 * multi else na; #, 'Slow MA', ma2Color, maWidth)
plot maW3 = if isweekly then ma3 * multi else na; #, 'Fast MA', ma3Color, maWWidth)
plot maW4 = if isweekly then ma4 * multi else na; #, 'Slow MA', ma4Color, maWWidth)
plot maM5 = if ismonthly then ma5 * multi else na; #, 'Fast MA', ma5Color, maMWidth)
plot maM6 = if ismonthly then ma6 * multi else na; #, 'Slow MA', ma6Color, maMWidth)
rsLine.SetLineWeight(2);
rsLine.SetDefaultColor(Color.CYAN);
maD1.SetDefaultColor(Color.RED);
maD2.SetDefaultColor(Color.YELLOW);
maW3.SetDefaultColor(Color.RED);
maW4.SetDefaultColor(Color.YELLOW);
maM5.SetDefaultColor(Color.RED);
maM6.SetDefaultColor(Color.YELLOW);
#-- Crosses
plot qBreak = if quickBreak then rsLine else na;
plot qSand = if quickSand then rsLine else na;
plot gBreak = if gdb then rsLine else na;
qBreak.SetLineWeight(3);
qSand.SetLineWeight(3);
gBreak.SetLineWeight(3);
qBreak.SetPaintingStrategy(PaintingStrategy.SQUARES);
qSand.SetPaintingStrategy(PaintingStrategy.SQUARES);
gBreak.SetPaintingStrategy(PaintingStrategy.SQUARES);
qBreak.SetDefaultColor(Color.MAGENTA);
qSand.SetDefaultColor(Color.ORANGE);
gBreak.SetDefaultColor(Color.LIGHT_RED);
#-- END of CODE