Playing around with this script. Some might find it useful
Code:
# Modified Zscore: 0.6745(xi – x̃) / MAD
# mcdon030
# date: 20211210
# User Selection for error/gaussian smoothing found in excel "erf"
# Orginial Script Author:
# Buy Sell Pressure
# Mobius
# Mobius at MyTrade
# V01.01.2011
declare lower;
script erf {
input z = 0;
def t = 1.0 / (1.0 + 0.5 * AbsValue(z));
# use Horner's method
def ans = 1 - t * Exp( -z * z - 1.26551223 +
t * ( 1.00002368 +
t * ( 0.37409196 +
t * ( 0.09678418 +
t * (-0.18628806 +
t * ( 0.27886807 +
t * (-1.13520398 +
t * ( 1.48851587 +
t * (-0.82215223 +
t * ( 0.17087277))))))))));
plot return = if z >= 0.0 then ans else -ans;
}
#Inputs
Input Length =15;
input DataType = {TrRange, TickTrRange, isVolume, default Shares,Price};
Input Price = close;#hint: Price only used when DataType "price" is selected for simple zscore
Input Error =Yes;#hint:No uses Price data and error doesn't matter
Input BullandBear = no;
Input stdevOne =2;
Input stdevtwo = 3.5;
def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def tcnt = tick_count ;
def tickS = If(!IsNaN(TickSize()), TickSize(), .01);
def bn = BarNumber();
def nan = Double.NaN;
def range = H - L;
def Data;
switch (DataType) {
case TrRange:
Data = (V / Max(TrueRange(H, C, L), tickS));
case TickTrRange:
Data = (V / tcnt / Max(TrueRange(H, C, L), tickS));
case isVolume:
Data = V;
case Shares:
Data = (V / tcnt);
case Price:
Data = nan;
}
def buyingx =( Data * (C - L) / (H - L));
def buyinga = if range == 0 && range[1]!=0 then buyingx[1] else buyinga[1];
def buying = if range == 0 then buyinga else buyingx;
def sellingx = ( Data * (H - C) / (H - L));
def sellinga = if range == 0 && range[1]!=0 then sellingx[1] else sellinga[1];
def selling = if range == 0 then sellinga else sellingx ;
def bullerror = erf(buying / (buying + selling ));
def bearerror = erf(selling / (buying + selling ));
def pData;
def bullData;
def bearData;
if DataType==DataType.Price{
pData= price;
bullData=nan;
bearData=nan;
} else if Error{
pData= bullerror -bearerror;
bullData=bullerror;
bearData=bearerror ;
} else{
pData= buying-selling;
bullData=buying;
bearData=selling;
}
#prime
def MedianData = Median(pData, length);
def absData =absvalue(pData-MedianData);
def MAD = Median(absdata,Length);
def Modifiedzscore =.6745*(pData-MedianData)/MAD;
#bull
def bullMedianData = Median(bullData, length);
def bullabsData =absvalue(bullData-MedianData);
def bullMAD = Median(bullabsData ,Length);
def bullModifiedzscore =.6745*(bullData-bullMedianData)/bullMAD;
#bear
def bearMedianData = Median(bearData, length);
def bearabsData =absvalue(bearData-MedianData);
def bearMAD = Median(bearabsData ,Length);
def bearModifiedzscore =.6745*(bearData-bearMedianData)/bearMAD;
# Plots
Plot Zscore = if !isnan(c) then Modifiedzscore else nan;
Zscore.assignValueColor( if Zscore>=stdevtwo then color.green else
if Zscore<=-stdevtwo then color.red else
color.gray);
Zscore.HideTitle();
Zscore.SetLineWeight(2);
Plot bullZscore = if !isnan(c) && BullandBear then bullModifiedzscore else nan;
bullzscore.assignValueColor( if bullzscore>=stdevtwo then color.green else
if bullzscore<=-stdevtwo then color.red else
color.gray);
bullZscore.HideTitle();
bullZscore.SetLineWeight(2);
Plot bearZscore = if !isnan(c) && BullandBear then bearModifiedzscore else nan;
bearZscore.assignValueColor( if bearZscore>=stdevtwo then color.green else
if bearZscore<=-stdevtwo then color.red else
color.gray);
bearZscore.HideTitle();
bearZscore.SetLineWeight(2);
plot pdevone = if !isNaN(c) then stdevOne else nan;
pdevone.SetDefaultColor(Color.light_Green);
pdevone.HideTitle();
plot ndevone = if !isNaN(c) then -stdevOne else nan;
ndevone.SetDefaultColor(Color.Light_Red);
ndevone.HideTitle();
plot pdevtwo = if !isNaN(c) then stdevtwo else nan;
pdevtwo .SetDefaultColor(Color.Green);
pdevtwo .HideTitle();
plot ndevtwo = if !isNaN(c) then -stdevtwo else nan;
ndevtwo.SetDefaultColor(Color.Red);
ndevtwo.HideTitle();