Modified Zscore: 0.6745(xi – x̃) / MAD For ThinkOrSwim

mcdon030

Member
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();
 
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();

Why are you using .6745 ratio? MAD isn't normalized, so you can't just plug in a z-score without scaling.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
428 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top