Custom Standard Deviation Channel for ThinkorSwim

Astro X Æ A-12

New member
Hi, could someone help me to get something like this? it's so clear to see where we are in a trend (OB/OS). This guy mentioned that there were some Standard deviation channels there, but didn't tell us how he made this, any thoughts?

Thanks in advanced

Code:
# RegressionStdDeviation
# DREWGRIFFITH15 (C) 2015

declare upper;

input price = close;
input deviations = 1.50;
input fullRange = No;
input length = 200;

def regression;
def stdDeviation;
if (fullRange) {
    regression = InertiaAll(price);
    stdDeviation = StDevAll(price);
} else {
    regression = InertiaAll(price, length);
    stdDeviation = StDevAll(price, length);
}

plot UpperLine = regression + deviations * stdDeviation;
plot MiddleLine = regression;
MiddleLine.Hide();
plot LowerLine = regression - deviations * stdDeviation;

UpperLine.SetDefaultColor(Color.DARK_RED);
MiddleLine.SetDefaultColor(Color.DARK_GRAY);
LowerLine.SetDefaultColor(Color.DARK_RED);
UpperLine.SetLineWeight(3);
MiddleLine.SetLineWeight(1);
LowerLine.SetLineWeight(3);

Found it:

Code:
declare upper;

input FirstStdDev = 1;
input SecondStdDev = 2;
input Price = close;
input PlotCenterLines = yes;
input PlotUpperLines = yes;

def regression = InertiaAll(price);
def stdDeviation = StDevAll(price);

#
# plot all values
#
plot firstUpperLine = regression + stdDeviation;
plot firstLowerLine = regression - stdDeviation;
plot secondUpperLine = regression + 2 * stdDeviation;
plot secondLowerLine = regression - 2 * stdDeviation;
plot middleLine = regression;

#
# set properties
#
firstUpperLine.SetDefaultColor(Color.ORANGE);
firstLowerLine.SetDefaultColor(Color.ORANGE);
secondUpperLine.SetDefaultColor(Color.UPTICK);
secondLowerLine.SetDefaultColor(Color.UPTICK);
middleLine.SetDefaultColor(Color.WHITE);

secondUpperLine.SetHiding(!PlotUpperLines);
secondLowerLine.SetHiding(!PlotUpperLines);

#
# plot dotted lines
#
plot line1 = regression + 1.5 * stdDeviation;
plot line2 = regression + 0.5 * stdDeviation;
plot line3 = regression - 0.5 * stdDeviation;
plot line4 = regression - 1.5 * stdDeviation;

line1.SetDefaultColor(Color.WHITE);
line2.SetDefaultColor(Color.WHITE);
line3.SetDefaultColor(Color.WHITE);
line4.SetDefaultColor(Color.WHITE);

line1.SetHiding(!PlotCenterLines or !PlotUpperLines);
line2.SetHiding(!PlotCenterLines);
line3.SetHiding(!PlotCenterLines);
line4.SetHiding(!PlotCenterLines or !PlotUpperLines);

line1.SetStyle(Curve.SHORT_DASH);
line2.SetStyle(Curve.SHORT_DASH);
line3.SetStyle(Curve.SHORT_DASH);
line4.SetStyle(Curve.SHORT_DASH);

lou3XdR.png
 
figured it out, works in any time frame.
link to study http://tos.mx/tmFoDfa
Fxgi25W.png

Code:
input price = close;
input deviations1 = 1.0;
input deviations2 = 2.0;
input deviations3 = 3.0;
input fullRange = Yes;
input length = 21;

def regression;

def stdDeviation;

if (fullRange) {
    regression = InertiaAll(price, extendToRight = yes);
    stdDeviation = stdevAll(price, extendToRight = yes);
} else {
    regression = InertiaAll(price, length);
    stdDeviation = stdevAll(price, length);
}

plot UpperLine = regression +  deviations1 * stdDeviation;
UpperLine.SetPaintingStrategy(PaintingStrategy.DasHES);
plot MiddleLine = regression;   MiddleLine.SetPaintingStrategy(PaintingStrategy.DasHES);
plot LowerLine = regression - deviations1 * stdDeviation;
LowerLine.SetPaintingStrategy(PaintingStrategy.DasHES);
plot UpperLine2 = regression + deviations2 * stdDeviation;
UpperLine2.SetPaintingStrategy(PaintingStrategy.DasHES);
plot LowerLine2 = regression - deviations2 * stdDeviation;
LowerLine2.SetPaintingStrategy(PaintingStrategy.DasHES);
plot LowerLine3 = regression - deviations3 * stdDeviation;
LowerLine3.SetPaintingStrategy(PaintingStrategy.DasHES);
plot UpperLine3 = regression + deviations3 * stdDeviation;
UpperLine3.SetPaintingStrategy(PaintingStrategy.DasHES);

UpperLine.SetDefaultColor(GetColor(6));
MiddleLine.SetDefaultColor(GetColor(3));
LowerLine.SetDefaultColor(GetColor(6));

#UpperLine2.SetDefaultColor(GetColor(8));
#LowerLine2.SetDefaultColor(GetColor(8));

UpperLine2.DefineColor("Up", GetColor(4));
UpperLine2.DefineColor("Down", GetColor(4));
UpperLine2.AssignValueColor(if UpperLine2 > UpperLine2[1] then UpperLine2.color("Up") else UpperLine2.color("Down"));

LowerLine2.DefineColor("Up", GetColor(4));
LowerLine2.DefineColor("Down", GetColor(4));
LowerLine2.AssignValueColor(if LowerLine2 > LowerLine2[1] then LowerLine2.color("Up") else LowerLine2.color("Down"));

UpperLine3.DefineColor("Up", GetColor(5));
UpperLine3.DefineColor("Down", GetColor(5));
UpperLine3.AssignValueColor(if UpperLine3 > UpperLine3[1] then UpperLine3.color("Up") else UpperLine3.color("Down"));

LowerLine3.DefineColor("Up", GetColor(5));
LowerLine3.DefineColor("Down", GetColor(5));
LowerLine3.AssignValueColor(if LowerLine3 > LowerLine3[1] then LowerLine3.color("Up") else LowerLine3.color("Down"));
For some reason your study is not projecting forward like in the picture you provided
 

Daytrader

New member
I notice the Standard Deviation Channels plot differently based on if pre/post market data is included or not (Under "Chart Settings" >" Axis" then checking/unchecking "Show Extended-Hours Trading Session").

Does anyone know if there is a way to code these Channels, so that you can have both sets of channels (WITH and WITHOUT Pre/Post Market Data) without having to run two separate charts?

Thanks
 

rad14733

Well-known member
VIP
@Daytrader As @BenTen stated, it cannot be done - and here's why... The functions that do the calculations for the standard deviations, and virtually everything else, use the current charts extended hours settings from the Chart Settings Panel and there is no way to override them...
 

Daytrader

New member
Okay...thank you both for explaining it. Since ALL DATA is available with the Extended Hours settings turned "ON" (i.e. Pre-Market -Normal Market Hours - After-Hours):

I figured one could write code to create some of the channels that excludes pre or post market data from the calculations. However, it looks like this is not possible based on your opinions, so thanks for letting me know.
 
Last edited:

jleo1947

New member
I have search for scripts in Watchlist but couldn't find the piece of coded needed. I am trying to build in the standardDevChannel into a watchlist. it works for the "OverPrice" column in the code as being too high / over the set parameter's for the deviations I set. see the code below. I think its how I have the PLOT and the if statements or I want the and instead of the Else if.

thanks

Code:
def UpperLine =StandardDevChannel("deviations" = 1.3)."LowerLine" is greater than close;
def LowerLine = StandardDevChannel("deviations" = 1.3)."MiddleLine" is greater than close;
plot value = if UpperLine then 1.3 else if LowerLine then 1.3 else 0;
#def UpperLine = UpperLine1 > 1; #newCode
#def LowerLine = LowerLine1 < 1; #newCode
AddLabel(LowerLine, "UnderPriced", color.Black);
AddLabel(UpperLine, "OverPriced", color.black); #is working
AddLabel(!LowerLine and !UpperLine, " ", color.black);
AssignBackgroundColor(if LowerLine then color.RED else if UpperLine then color.GREEN else color.black);
 

XeoNoX

Well-known member
VIP
Hi, how do you code to get this label below? I have the channels on my charts but want to see the value in a label. Thanks

SDV.png
There are thousands of channels that you can be referring to. Therefore you can begin with posting the code of the study that you want labeled.
 

herns51

New member
There are thousands of channels that you can be referring to. Therefore you can begin with posting the code of the study that you want labeled.
The standard deviation channel is already on thinkorswim. I just want to be able to see what level the price is currently at on a label
 

XeoNoX

Well-known member
VIP
@herns51
Code:
input price = close;
input deviations = 2.0;
input fullRange = Yes;
input length = 21;

def regression;
def stdDeviation;
if (fullRange) {
    regression = InertiaAll(price);
    stdDeviation = stdevAll(price);
} else {
    regression = InertiaAll(price, length);
    stdDeviation = stdevAll(price, length);
}

def UpperLine = regression + deviations * stdDeviation;
def MiddleLine = regression;
def LowerLine = regression - deviations * stdDeviation;

AddLabel (yes, "Upder: " +  (UpperLine)  ,color.green);
AddLabel (yes, "Middle: " +  (MiddleLine) ,color.green );
AddLabel (yes, "Lower: " +  (LowerLine) ,color.green );
 

herns51

New member
@herns51
Code:
input price = close;
input deviations = 2.0;
input fullRange = Yes;
input length = 21;

def regression;
def stdDeviation;
if (fullRange) {
    regression = InertiaAll(price);
    stdDeviation = stdevAll(price);
} else {
    regression = InertiaAll(price, length);
    stdDeviation = stdevAll(price, length);
}

def UpperLine = regression + deviations * stdDeviation;
def MiddleLine = regression;
def LowerLine = regression - deviations * stdDeviation;

AddLabel (yes, "Upder: " +  (UpperLine)  ,color.green);
AddLabel (yes, "Middle: " +  (MiddleLine) ,color.green );
AddLabel (yes, "Lower: " +  (LowerLine) ,color.green );
Thanks for looking into this. What I was looking for was for the label to show what standard deviation the price is sitting at. If you look at the image I posted above you would see the label show "DEV 5.3", meaning the price is currently at the 5th deviation level.
 

XeoNoX

Well-known member
VIP
Thanks for looking into this. What I was looking for was for the label to show what standard deviation the price is sitting at. If you look at the image I posted above you would see the label show "DEV 5.3", meaning the price is currently at the 5th deviation level.

you stated
"If you look at the image I posted above you would see the label show "DEV 5.3", meaning the price is currently at the 5th deviation level."

to be honest all i see is a label and a bunch of lines that have nothing to relate to due to your partial screenshot and lack of explanation . Therefore that screenshot appears like just a random number other than the fact you stated afterwards that it was a study based off of the standard deviation channel.
 

herns51

New member
you stated
"If you look at the image I posted above you would see the label show "DEV 5.3", meaning the price is currently at the 5th deviation level."

to be honest all i see is a label and a bunch of lines that have nothing to relate to due to your partial screenshot and lack of explanation . Therefore that screenshot appears like just a random number other than the fact you stated afterwards that it was a study based off of the standard deviation channel.
My apologies for not being clear initially. The label simply shows what STD channel the price is at. Any ideas on how to code that? thanks
 

Calabasse

New member
VIP
Is there a way to show the 180 day standard deviation channels on an intraday chart? One of the youtubers (Connor) out there uses the long-term standard deviation channels as support/resistance areas, which makes sense to me statistically. I want to simplify the review of where the 2 and 3 standard deviations channels are for the 180d or 1Y charts simply by seeing them on my intraday charts. Seems like an easy thing to do for someone who understands Thinkscript--I only know enough to get by and tweak code written by others.
 
Last edited:

Similar threads

Top