ZSCORE Indicator by TheraTrend for ThinkorSwim

Camelotnite

Camelotnite

New member
VIP
This is a mean reversion indicator to forecast possible price reversal.



Code:
#=====================================
#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the code with a link back to thetatrend.com

declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score
plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
Zscore.assignValueColor(color.cyan);
Zscore.assignValueColor(if Zscore > 0 then color.cyan else color.red);

plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);
avgZscore.setLineWeight(2);
avgZscore.assignValueColor(color.green);
#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);

#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
two.assignValueColor(color.YELLOW);
plot one = 1;
one.setPaintingStrategy(paintingStrategy.DASHES);
one.setLineWeight(1);
one.assignValueColor(color.GRAY);
plot negone = -1;
negone.setPaintingStrategy(paintingStrategy.DASHES);
negone.setLineWeight(1);
negone.assignValueColor(color.GRAY);
plot negtwo = -2;
negtwo.assignValueColor(color.LIGHT_RED);
zero.setDefaultColor(color.DARK_GRAY);
#=====================================
 
Last edited by a moderator:
D

diazlaz

Well-known member
2019 Donor
VIP
this is a very cool - I think there are multiple uses, I've made some changes to make turn the zscore into a trend indicator, I think it can be use as trend, reversal and maybe even as a exit/stop indicator, please play with it and test it. report back any findings or observations.



toggle color candles on or off to control the candle color as an input.

Code:
#=====================================
#Computes and plots the Zscore
#Provided courtesy of ThetaTrend.com
#Feel free to share the code with a link back to thetatrend.com
#
# Change Log
# 2019.10.01 - diazlaz - added color candles for trend detection
#                      - added threshold and 2 candle confirmation
#                        for trend and reducing intraday noise at
#                        lower timeframes.
#

declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;
input zTresPos = 0.50;
input zTresNeg = -0.50;
input colorBars = yes;

#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score
plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
Zscore.assignValueColor(color.cyan);
Zscore.assignValueColor(if Zscore > 0 then color.cyan else color.red);

plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);
avgZscore.setLineWeight(2);
avgZscore.assignValueColor(color.green);
#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);

#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
two.assignValueColor(color.YELLOW);
plot one = 1;
one.setPaintingStrategy(paintingStrategy.DASHES);
one.setLineWeight(1);
one.assignValueColor(color.GRAY);
plot negone = -1;
negone.setPaintingStrategy(paintingStrategy.DASHES);
negone.setLineWeight(1);
negone.assignValueColor(color.GRAY);
plot negtwo = -2;
negtwo.assignValueColor(color.LIGHT_RED);
zero.setDefaultColor(color.DARK_GRAY);

#Color Candles
def state = if Zscore > zTresPos and Zscore[1] > zTresPos then 100 else 
   if Zscore < zTresNeg and Zscore[1] < zTresNeg then -100 else state[1];

AssignPriceColor(
   if colorBars then
    if state == 100 then COLOR.GREEN
        else COLOR.RED
   else
        Color.CURRENT);

#End of ZSCORE Indicator by TheraTrend for ThinkorSwim
 

Similar threads

Top