Author Message:
Enhanced Moving Average Calculation with Stepped Moving Average and the Advantages over Regular RSI
Technical analysis plays a crucial role in understanding and predicting market trends. One popular indicator used by traders and analysts is the Relative Strength Index (RSI). However, an enhanced approach called Stepped Moving Average, in combination with the Slow RSI (I added RSX) function, offers several advantages over regular RSI calculations.
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator("Step RSI [Loxx]")
# converted and modified by Sam4Cok@Samer800 - 05/2023
declare lower;
input RsiMethod = {default "Slow", "RSX"};# "RSI Type"
input Source = close; # "Source"
input rsiPeriod = 14; # "RSI Period"
input StepSize = 5; # "Step Size"
input colorBars = yes; # "Color bars?"
input showBackground = yes; # "Show Highlights?"
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def slow = RsiMethod==RsiMethod."Slow";
#stepMACalc(float rsi, simple int size)=>
script stepMACalc {
input rsi = close;
input size = 5;
def smin;
def smax;
def smax_ = rsi + 2.0 * size;
def smin_ = rsi - 2.0 * size;
def trend;
def result;
if trend[1] <= 0 and rsi > smax[1] {
trend = 1;
} else
if trend[1] >= 0 and rsi < smin[1] {
trend = -1;
} else {
trend = trend[1];
}
if trend > 0 {
smin = if smin_ < smin[1] then smin[1] else smin_;
smax = smax_;
result = smin + size;
} else {
smax = if smax_ > smax[1] then smax[1] else smax_;
smin = smin_;
result = smax - size;
}
plot out = result;
}
#slowRSI(float src, simple int per)=>
script slowRSI {
input src = close;
input per = 14;
def alpha = (1 / per);
def up = fold k = 0 to per with p do
if (src[k] - GetValue(src, k + 1)) > 0
then p + (src[k] - GetValue(src, k + 1)) else p;
def dn = fold i = 0 to per with q do
if (src[i] - GetValue(src, i + 1)) > 0
then q else q - (src[i] - GetValue(src, i + 1));
def rma2 = WildersAverage(100 * up / (up + dn), per);
def rsival = if (up + dn) == 0 then 50 else rma2;
plot out = rsival;
}
script nz {
input data = close;
def ret_val = if !IsNaN(data) then data else 0;
plot return = ret_val;
}
#// @function method for returning 1 of 7 different RSI calculation outputs.
#rsx(float src, int len)=>
script rsx {
input src = close;
input len = 14;
def src_out = 100 * src;
def mom0 = src_out - src_out[1];
def moa0 = AbsValue(mom0);
def Kg = 3 / (len + 2.0);
def Hg = 1 - Kg;
def f28;
def f30;
f28 = Kg * mom0 + Hg * nz(f28[1]);
f30 = Hg * nz(f30[1]) + Kg * f28;
def mom1 = f28 * 1.5 - f30 * 0.5;
def f38;
def f40;
f38 = Hg * nz(f38[1]) + Kg * mom1;
f40 = Kg * f38 + Hg * nz(f40[1]);
def mom2 = f38 * 1.5 - f40 * 0.5;
def f48;
def f50;
f48 = Hg * nz(f48[1]) + Kg * mom2;
f50 = Kg * f48 + Hg * nz(f50[1]);
def mom_out = f48 * 1.5 - f50 * 0.5;
def f58;
def f60;
f58 = Hg * nz(f58[1]) + Kg * moa0;
f60 = Kg * f58 + Hg * nz(f60[1]);
def moa1 = f58 * 1.5 - f60 * 0.5;
def f68;
def f70;
f68 = Hg * nz(f68[1]) + Kg * moa1;
f70 = Kg * f68 + Hg * nz(f70[1]);
def moa2 = f68 * 1.5 - f70 * 0.5;
def f78;
def f80;
f78 = Hg * nz(f78[1]) + Kg * moa2;
f80 = Kg * f78 + Hg * nz(f80[1]);
def moa_out = f78 * 1.5 - f80 * 0.5;
def rsiout = Max(Min((mom_out / moa_out + 1.0) * 50.0, 100.00), 0.00);
plot out = rsiout;
}
def rsxout = rsx(Source, rsiPeriod);
def slowout = slowRSI(Source, rsiPeriod);
def rsiout = if slow then slowout else rsxout;
def fasttrend = stepMACalc(rsiout, StepSize);
def colorout = rsiout > fasttrend;
plot StepRSI = rsiout;
plot FastTrnd = fasttrend;
StepRSI.SetLineWeight(2);
StepRSI.AssignValueColor(if colorout then Color.GREEN else Color.RED);
FastTrnd.SetDefaultColor(Color.WHITE);
AssignPriceColor(if !colorbars then Color.CURRENT else if colorout then Color.GREEN else Color.RED);
AddCloud(if !showBackground then na else if colorout then pos else neg, if colorout then neg else pos, Color.DARK_GREEN, Color.DARK_RED);
#-- END of CODE