Creator Message:
This is a simple moving average with a rolling length that resets whenever there is an outlier in the data. I have also included volume weighting.
The length represents the lookback period for the outlier detection and the "Outlier Detection" is the deviation level to trigger the detection. You can select from: price detection, volume detection, price or volume detection, price and volume detection.
I hope you can find this script useful. Its like a session weighted moving average but instead it retriggers the cumulative sum whenever there is an outlier.
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © peacefulLizard50262
#indicator("ODCMA", overlay = true)
# Converted by Sam4Cok@Samer800 - 01/2023
input src = close;#, "Source")
input smoothing = 0;#, "Smoothing", 0)
input weight = {default "Volume Weighting", "No Volume Weighting"};#, "Volume Weighting")
input swch = {"Price", "Volume", "Price or Volume", default "Price and Volume", "Pivot Point"};# "Outlier Detection"
input len = 30;#, "Outlier Length", 2)
input max = 3;#, "Outlier Detection Level", 0)
input other = no;#, "True Range Detection", "Enable to use true range instead of standard deviation")
input length = 100;#, "True Range Normalization Length")
input leftLenH = 10; # "Pivot High", 1, inline="Pivot High", group= "Pivot Points")
input rightLenH = 10; # "Pivot High", group= "Pivot Points")
input leftLenL = 10; # "Pivot Low", 1, inline="Pivot Low", group= "Pivot Points")
input rightLenL = 10; # "Pivot Low", group= "Pivot Points")
def na = Double.NaN;
script nz {
input data = close;
input repl = 0;
def ret_val = if data==0 then repl else data;
plot return = ret_val;
}
#filter(float src, int len = 1) =>
script filter {
input src = close;
input len = 0;
def filter;
filter = TotalSum((src + (src[1] * 2) + (src[2] * 2) + src[3]) / 6);
def filt = if len > 0 then (filter - filter[len]) / len else src;
plot return = filt;
}
#normalize(float src, int len) =>
script normalize {
input src = close;
input len = 100;
def out = (src - Lowest(src, len)) / (Highest(src, len) - Lowest(src, len));
plot return = out;
}
#outlier(series float source, int periods = 30, float max = 2, int length = 100, bool other = false) =>
script outlier {
input source = close;
input periods = 30;
input max = 3;
input length = 100;
input other = no;
def difHL = high - low;
def norm = normalize(difHL, length);
def difSrc = source - source[1];
def out = AbsValue(((difSrc) / Sqrt((Sum(Power(difSrc, 2), periods) -
Power(difSrc, 2)) / (periods - 2))));
def tr = if open > close then -norm else norm;
def outlier_tr = if tr > 0.5 then 1 else if tr < -0.5 then 1 else 0;
def outlier = out >= max;
def outl = if other then outlier_tr else outlier;
plot return = outl;
}
#outlier_volume(series float source, int periods = 30, float max = 2, int length = 100, bool other = false) =>
script outlier_volume {
input periods = 30;
input max = 3;
input length = 100;
input other = no;
def difSrc = volume - volume[1];
def volume_high = if open < close then volume else 0;
def volume_low = if open > close then -volume else 0;
def difHL = volume_high - volume_low;
def norm = normalize(difHL, length);
def out = AbsValue((difSrc / Sqrt((Sum(Power(difSrc, 2), periods) - Power(difSrc, 2)) / (periods - 2))));
def tr = if open > close then -norm else norm;
def outlier_tr = if tr > 0.5 then 1 else if tr < -0.5 then 1 else 0;
def outlier = out >= max;
def outl = if other then outlier_tr else outlier;
plot return = outl;
}
#odma(src = close, len = 20, max = 2, swch = "Price", length = 100, other = false, ext = na, smoothing = 0) =>
script odma {
input src = close;
input len = 20;
input max = 3;
input swch = "Price";
input length = 100;
input other = no;
input ext = Double.NaN;
input smoothing = 0;
def rolling_length;
def swma;
def outlier;
def outlier_vol;
def outlier_swch;
def v = volume;
outlier_vol = outlier(v, len, max, length, no);
outlier = outlier(src, len, max, length, other);
outlier_swch = if swch == "Price" then outlier else
if swch == "Volume" then outlier_vol else
if swch == "Price or Volume" then outlier or outlier_vol else
if swch == "Price and Volume" then outlier and outlier_vol else ext;
rolling_length = if outlier_swch then 1 else 1 + nz(rolling_length[1]);
swma = if outlier_swch then (src + src[1]) / 2 else src + nz(swma[1]);
def result = WMA(SimpleMovingAvg(swma / rolling_length, 2), 3);
def odma = filter(result, smoothing);
plot return = odma;
}
#odvwap(src = close, len = 200, max = 3, swch = "Volume", length = 100, other = false, ext = na, smoothing = 0) =>
script odvwap {
input src = close;
input len = 30;
input max = 3;
input swch = "Volume";
input length = 100;
input other = no;
input ext = Double.NaN;
input smoothing = 0;
def sumSrcVol;
def sumVol;
def outlier;
def outlier_vol;
def outlier_swch;
def v = volume;
def srcVol = src * v;
def srcVol1 = src[1] * v[1];
outlier_vol = outlier_volume(len, max, length, other);
outlier = outlier(src, len, max, length, other);
outlier_swch = if swch == "Price" then outlier else
if swch == "Volume" then outlier_vol else
if swch == "Price or Volume" then outlier or outlier_vol else
if swch == "Price and Volume" then outlier and outlier_vol else
if swch == "Pivot Point" then ext else nz(outlier_swch[1]);
sumSrcVol = if outlier_swch then (srcVol + srcVol1)/2 else srcVol + sumSrcVol[1];
sumVol = if outlier_swch then (v + nz(v[1],v)) / 2 else v + nz(sumVol[1],v);
def result = sumSrcVol / sumVol;
def odvwap = filter(result, smoothing);
plot retrun = odvwap;
}
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def ph = findpivots(high, 1, leftLenH, rightLenH);
def pl = findpivots(low, -1, leftLenL, rightLenL);
def pivot = !isNaN(ph) or !isNaN(pl);
def ma;
switch (weight) {
case "Volume Weighting":
ma = odvwap(src, len, max, swch, length, other, pivot, smoothing);
case "No Volume Weighting":
ma = odma(src, len, max, swch, length, other, pivot, smoothing);
}
def maValue = ma;
plot odvwap = maValue;
odvwap.SetDefaultColor(Color.MAGENTA);
# --- END CODE
Last edited by a moderator: