#### netarchitech

##### Well-known member

Code:

```
# filename: MR__EZ_PercentR_MAC_
# idea source: @HighBredCloud and https://usethinkscript.com/threads/moving-average-crossover-rsi-indicator-for-thinkorswim.185/
# Code: Thanson Stevens, @korygill and netarchitech
# V11.12.2019 - netarchitech added multiple-choice smoothing per HighBredCloud request
declare lower;
# PercentR_MAC
input length = 14;
input over_Bought = 80;
input over_Sold = 20;
input lowBand = 10; #Smoothing LowerBand
input data = close;
input lower = low; #Research Lower = Low?
def highest = Highest(high, length);
def divisor = highest - Lowest(low, length);
def PI = 3.14159265359;
def a1 = Exp(-PI * Sqrt(2) / lower);
def coeff2 = 2 * a1 * Cos(Sqrt(2) * PI / lower);
def coeff3 = - Power(a1, 2);
def coeff1 = 1 - coeff2 - coeff3;
def filt = coeff1 * (data + (data[1])) / 2 + coeff2 * (filt[1]) + coeff3 * (filt[2]);
input PercentRMALength5 = 5;
input PercentRMALength8 = 8;
#input PercentRMALength13 = 13;
input PercentRAverageType = AverageType.SIMPLE;
# plot and smooth PercentR
plot "%R" = if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor;
"%R".DefineColor("OverBought", GetColor(9)); #GetColor(9));
"%R".DefineColor("Normal", GetColor(7));
"%R".DefineColor("OverSold", GetColor(1)); #GetColor(1));
"%R".AssignValueColor(if "%R" > over_Bought then "%R".Color("OverBought") else if "%R" < over_Sold then "%R".Color("OverSold") else "%R".Color("Normal"));
"%R".SetLineWeight(3);
"%R".Hide();
# plot and smooth the PercentR Moving Averages
def PercentRMA5 = MovingAverage(PercentRAverageType, "%R", PercentRMALength5);
plot PercentRMovAvg5 = PercentRMA5;
PercentRMovAvg5.SetDefaultColor(Color.GREEN);
PercentRMovAvg5.SetLineWeight(3);
PercentRMovAvg5.hide();
def PercentRMA8 = MovingAverage(PercentRAverageType, "%R", PercentRMALength8);
plot PercentRMovAvg8 = PercentRMA8;
PercentRMovAvg8.SetDefaultColor(Color.RED);
PercentRMovAvg8.SetLineWeight(3);
PercentRMovAvg8.hide();
#def PercentRMA13 = MovingAverage(PercentRAverageType, "%R", PercentRMALength13);
#plot PercentRMovAvg13 = PercentRMA13;
#PercentRMovAvg13.SetDefaultColor(Color.ORANGE);
#PercentRMovAvg13.SetLineWeight(2);
#PercentRMovAvg13.hide();
input applySelectSmoothing = yes;
def smooth_it = EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, lowBand);
def smooth_MA5 = EhlersSuperSmootherFilter(MovingAverage(PercentRAverageType, "%R", PercentRMALength5));
def smooth_MA8 = EhlersSuperSmootherFilter(MovingAverage(PercentRAverageType, "%R", PercentRMALength8));
input SelectSmoothingType = {default "PercentR Only", "MAs Only", "PercentR & MAs", "No Smoothing"};
plot X;
X.AssignValueColor(if "%R" > over_Bought then "%R".Color("OverBought") else if "%R" < over_Sold then "%R".Color("OverSold") else "%R".Color("Normal"));
X.SetLineWeight(3);
plot Y;
Y.SetDefaultColor(Color.GREEN);
Y.SetLineWeight(2);
plot Z;
Z.SetDefaultColor(Color.RED);
Z.SetLineWeight(2);
switch (SelectSmoothingType) {
case "PercentR Only":
X = if applySelectSmoothing and "%R" then smooth_it else "%R";
Y = if applySelectSmoothing and PercentRMA5 then PercentRMA5 else PercentRMA5;
Z = if applySelectSmoothing and PercentRMA8 then PercentRMA8 else PercentRMA8;
case "MAs Only":
X = if applySelectSmoothing and "%R" then "%R" else "%R";
Y = if applySelectSmoothing and PercentRMA5 then smooth_MA5 else PercentRMA5;
Z = if applySelectSmoothing and PercentRMA8 then smooth_MA8 else PercentRMA8;
case "PercentR & MAs":
X = if applySelectSmoothing and "%R" then smooth_it else "%R";
Y = if applySelectSmoothing and PercentRMA5 then smooth_MA5 else PercentRMA5;
Z = if applySelectSmoothing and PercentRMA8 then smooth_MA8 else PercentRMA8;
case "No Smoothing":
X = if applySelectSmoothing and"%R" then "%R" else "%R";
Y = if applySelectSmoothing and PercentRMA5 then PercentRMA5 else PercentRMA5;
Z = if applySelectSmoothing and PercentRMA8 then PercentRMA8 else PercentRMA8;
}
plot OverBought = over_Bought;
OverBought.SetDefaultColor(Color.DARK_RED);
OverBought.HideTitle();
plot OverSold = over_Sold;
OverSold.SetDefaultColor(Color.DARK_GREEN);
OverSold.HideTitle();
plot fifty_line = 50;
fifty_line.SetDefaultColor(GetColor(8));
fifty_line.HideTitle();
fifty_line.SetStyle(Curve.SHORT_DASH);
# plot the Breakout Signals
input showBreakoutSignals = yes;
plot UpSignal = if "%R" crosses above OverSold then OverSold else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetLineWeight(3);
UpSignal.HideTitle();
plot DownSignal = if "%R" crosses below OverBought then OverBought else Double.NaN;
DownSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetLineWeight(3);
DownSignal.HideTitle();
AddCloud(0, over_Sold, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(over_Bought, 100, Color.DARK_RED, Color.DARK_RED);
# Gmode
def lengthRSI = 14;
def price = close;
input averageType = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageType, price - price[1], lengthRSI);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
RSI.SetDefaultColor(CreateColor(0, 102, 204));
RSI.SetLineWeight(3);
#RSI.HideTitle();
RSI.HideBubble();
RSI.Hide();
input n1 = 9; #Channel Length
input n2 = 26; #Average Length
input n3 = 13; #Short length
def multi = no; #Multi-exchange?
def src0 = hlc3;
def src1 = hlc3(period = AggregationPeriod.MONTH);
def src2 = hlc3(period = AggregationPeriod.MONTH);
def src3 = hlc3(period = AggregationPeriod.MONTH);
def src4 = hlc3(period = AggregationPeriod.MONTH);
script tci {
input src = hlc3;
input n1 = 9; #Channel Length
input n2 = 26; #Average Length
plot tci = MovAvgExponential((src - MovAvgExponential(src, n1)) / (0.025 * MovAvgExponential(AbsValue(src - MovAvgExponential(src, n1)), n1)), n2) + 50;
tci.Hide();
}
#mf(src) => rsi(sum(volume * (change(src) <= 0 ? 0 : src), n3), sum(volume * (change(src) >= 0 ? 0 : src), n3))
script mf {
input src = hlc3;
input n3 = 13; #Short length
def rsi = reference RSI();
#plot mf = rsi(Sum(volume * (if Average(src) <= 0 then 0 else src), n3), Sum(volume * (if Average(src) >= 0 then 0 else src), n3));
plot mf = RSI(n3, src);
mf.Hide();
}
script willy {
input src = hlc3;
input n2 = 26; #Average Length
plot willy = 60 * (src - Highest(src, n2)) / (Highest(src, n2) - Lowest(src, n2)) + 80;
willy.Hide();
}
#csi(src) => avg(rsi(src, n3),tsi(src0,n1,n2)*50+50)
script csi {
input src = hlc3;
input n1 = 9; #Channel Length
input n2 = 26; #Average Length
input n3 = 13; #Short length
def rsi = reference RSI();
plot csi = RSI(n3, src) + TrueStrengthIndex(n1, n2) / 2 * 50 + 50;
csi.Hide();
}
script godmode {
input src = hlc3;
def rsi = reference RSI();
plot godmode = (tci(src) + CSI(src) + mf(src) + willy(src)) / 4;
godmode.Hide();
}
script tradition {
input src = hlc3;
input n3 = 13; #Short length
def rsi = reference RSI();
plot tradition = (tci(src) + mf(src) + RSI(n3, src)) / 3;
tradition.Hide();
}
def wt1 = if multi then (godmode(src0) + godmode(src1) + godmode(src2) + godmode(src3) + godmode(src4) / 5) else tradition(src0);
def wt2 = SimpleMovingAvg(wt1, 6);
def extended = if wt2 < 20 then wt2 + 5 else if wt2 > 80 then wt2 - 5 else Double.NaN;
plot wta = wt1;
wta.SetDefaultColor(Color.GREEN);
wta.SetLineWeight(2);
wta.Hide();
plot wtb = wt2;
wtb.SetDefaultColor(Color.RED);
wtb.SetLineWeight(2);
wtb.Hide();
plot ext = extended; #Caution!
ext.SetPaintingStrategy(PaintingStrategy.POINTS);
ext.SetDefaultColor(Color.YELLOW);
ext.SetLineWeight(3);
```