# 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);``````

#### JP382

##### New member
I too have been looking at the Stand Dev Strategy from Boiler Room and been tinkering with it to a degree. I started drawing up some code and came to here looking for info on extending lines and then I discovered this thread, so.....

It looks like cpl of you have been on the same page as I trying to find a way to display higher timeframe dev's on short term, as well as extend the lines forward and find a way to draw Priceline's automatically bc doing so manually pre-mkt is INCREDIBLY time consuming and can be unproductive if not a distraction from watching stocks/mkt

I want to display 15min Std Dev info on 1min chart with all the bells and whistles.

Does anyone (@geremyh @Tidan @BrianPaul) have a finished code that does this?

Last edited:

#### germanburrito

##### Active member
I too have been looking at the Stand Dev Strategy from Boiler Room and been tinkering with it to a degree. I started drawing up some code and came to here looking for info on extending lines and then I discovered this thread, so.....

It looks like cpl of you have been on the same page as I trying to find a way to display higher timeframe dev's on short term, as well as extend the lines forward and find a way to draw Priceline's automatically bc doing so manually pre-mkt is INCREDIBLY time consuming and can be unproductive if not a distraction from watching stocks/mkt

I want to display 15min Std Dev info on 1min chart with all the bells and whistles.

Does anyone (@geremyh @Tidan @BrianPaul) have a finished code that does this?
the reason why it might not work its because, the script uses inertiall, meaning it will take the average of the whole chart you are looking at and draw the regression line(middle) one thing you can do, is use a 180 day 5 minute chart if you trade on a 5 minute chart, it will take a while to populate but you can use the feature where the regression line aloows you to gather a certain amount of bars by choosing no on the full extension fature and taking the regression of the amount of bars you want, so for example if you are looking at a week regression channel of a week on a 5 week chart you would do something like a 390 period regression channel not ful extended because thats how many 5 minute bars there is in a week not accouting for after hours.

#### JP382

##### New member
the reason why it might not work its because, the script uses inertiall, meaning it will take the average of the whole chart you are looking at and draw the regression line(middle) one thing you can do, is use a 180 day 5 minute chart if you trade on a 5 minute chart, it will take a while to populate but you can use the feature where the regression line aloows you to gather a certain amount of bars by choosing no on the full extension fature and taking the regression of the amount of bars you want, so for example if you are looking at a week regression channel of a week on a 5 week chart you would do something like a 390 period regression channel not ful extended because thats how many 5 minute bars there is in a week not accouting for after hours.
Hmmmm.... I was under the impression from other MTF indicators, that display their info, I could specify time aggregation to display whatever Std Dev I want?

Im just frankensteining code from other indicators to "lay out" what I think I need, nothing has been tested to see how much of it I got wrong

#### Tidan

##### New member
@rad14733 I have tried numerous other ideas to get this to work but with no success. Do you by chance have any other suggestions I might try?

#### germanburrito

##### Active member
@rad14733 I have tried numerous other ideas to get this to work but with no success. Do you by chance have any other suggestions I might try?
you could do what im suggesting, this picture might help, you can just zoom in, this are all set to two std devs. and they will follow price as it moves so it might work better than drawing them. this is an 180 day 5 minute chart, if anything you might be able to have those lines going horizontal to clean the chart up.

this is what it looks like zoomed in, you can see that it bounced of the week and it traded along the two day dev, and it broke aobve the middle 180 day line.

Last edited:

#### JP382

##### New member
Pls don't chew me out... my brain is fried from working on my Standard Dev indicator... how do I add the names of each value?
Ex: for the median line I want to display: 0
+1 for the first line over 0 and -1 for below etc??

I know its something simple Ive searched through here but cant find it?

#### Tidan

##### New member
@germanburrito I tried what you mentioned using a 180 day/5 min but the STD lines didn't line up with a true 180/4 chart - it also was very slow loading so switching between numerous stocks was arduous. But thank you for your suggestion.

@JP382 I too have wanted to add a text label to my script generated price lines but to no avail. Maybe someone else can chime in on both the STD Dev challenge AND the price line label.

#### JP382

##### New member
@germanburrito I tried what you mentioned using a 180 day/5 min but the STD lines didn't line up with a true 180/4 chart - it also was very slow loading so switching between numerous stocks was arduous. But thank you for your suggestion.

@JP382 I too have wanted to add a text label to my script generated price lines but to no avail. Maybe someone else can chime in on both the STD Dev challenge AND the price line label.
Maybe someone will... Ive looked all over before asking here bc I know it's almost not permitted to directly ask for something here

I dont think there is way to have longer timeframe StdDev display on short TF chart (15M data on 1 min) I,like you, have tried w/help of someone outside of here but the data does not match factory results, in some cases its a few pennies other its well over 10cent diff which def matters and its not a clean line either

#### Rookie

##### New member
I have been searching through the forums (with little success) for a watchlist column script that will color the background color to show a stocks current location within the 1st, 2nd or 3rd positive or negative Std. Dev. Channels. Does anyone know if this already exists? Or, assuming it is not a difficult task for someone with the skill set, could someone possibly write the script for me/others? thanks in advance for any help with this!

#### Tidan

##### New member
Would fundamentalType/fundamental help me achieve my ambition of drawing std dev extension price lines from the 180d/4h chart? I stumbled upon Fundamental but can't seem to find an explanation of what it does that I can wrap my head around.

Last edited:

#### AnakManis

##### New member
VIP
I watched TBR YT video today and saw this (picture shown below), I tried to recreate the label, but it doesn't seem to work. So I looked on this website to find a solution and found this thread.

I think someone have asked this question before, but was not answered properly.

So here's the question, (referring to the picture below) based on the StandardDevChannel from TOS, how can I add a label that can show me the location of the closing price within the standard deviation?

Like on the chart off of QQQ, the closing price on the picture is \$336.74 and based on the StandardDevChannel, the closing price is at -1.1 deviation. So how do I put that as a label?

This is what I have so far,
Code:
``AddLabel (showSDCLabels, "Deviation: " + Round (-(regression-close)), color.ORANGE);``
I don't think it's right at all. I tried to learn the formula of the StandardDeviation and was dumbfounded.

Can anyone help? @BenTen ? Thanks in advance.

#### JP382

##### New member
I watched TBR YT video today and saw this (picture shown below), I tried to recreate the label, but it doesn't seem to work. So I looked on this website to find a solution and found this thread.

I think someone have asked this question before, but was not answered properly.

So here's the question, (referring to the picture below) based on the StandardDevChannel from TOS, how can I add a label that can show me the location of the closing price within the standard deviation?

Like on the chart off of QQQ, the closing price on the picture is \$336.74 and based on the StandardDevChannel, the closing price is at -1.1 deviation. So how do I put that as a label?

This is what I have so far,
Code:
``AddLabel (showSDCLabels, "Deviation: " + Round (-(regression-close)), color.ORANGE);``
I don't think it's right at all. I tried to learn the formula of the StandardDeviation and was dumbfounded.

Can anyone help? @BenTen ? Thanks in advance.

I think you can buy the script from TBR....

#### JP382

##### New member
Since Im here Ill ask, does anyone have any idea how to make the stand dev appear correctly on MTF? I have the MTF script someone helped me out with but it does not work, trying to show the 4hr devs on a 1 min chart. The 4hr SD's on a 1min chart DO NOT match up with 4hr chart.
I assume a lot more lines have to be written...

# Standard Devs
input agg = AggregationPeriod.FIFTEEN_MIN;
input price = close;
input deviations1 = 1.0;
input deviations2 = 2.0;
input deviations3 = 3.0;
input deviations4 = .5;
input deviations5 = 1.5;
input deviations6 = 2.5;
input fullRange = Yes;
input length = 21;

def regression;

def stdDeviation;

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

plot MiddleLine = InertiaAll(Close(period = agg),extendToRight = Yes);
MiddleLine.SetPaintingStrategy(PaintingStrategy.LINE);
plot UpperLine = regression + deviations1 * stdDeviation;
UpperLine.SetPaintingStrategy(PaintingStrategy.LINE);
plot LowerLine = regression - deviations1 * stdDeviation;
LowerLine.SetPaintingStrategy(PaintingStrategy.LINE);

plot UpperLine2 = regression + deviations2 * stdDeviation;
UpperLine2.SetPaintingStrategy(PaintingStrategy.LINE);
plot LowerLine2 = regression - deviations2 * stdDeviation;
LowerLine2.SetPaintingStrategy(PaintingStrategy.LINE);
plot LowerLine3 = regression - deviations3 * stdDeviation;
LowerLine3.SetPaintingStrategy(PaintingStrategy.LINE);
plot UpperLine3 = regression + deviations3 * stdDeviation;
UpperLine3.SetPaintingStrategy(PaintingStrategy.LINE);

plot UpperLine4 = regression + deviations4 * stdDeviation;
UpperLine4.SetStyle(Curve.SHORT_DASH);
plot LowerLine4 = regression - deviations4 * stdDeviation;
LowerLine4.SetStyle(Curve.SHORT_DASH);

plot UpperLine5 = regression + deviations5 * stdDeviation;
UpperLine5.SetStyle(Curve.SHORT_DASH);
plot LowerLine5 = regression - deviations5 * stdDeviation;
LowerLine5.SetStyle(Curve.SHORT_DASH);

plot LowerLine6 = regression - deviations6 * stdDeviation;
LowerLine6.SetStyle(Curve.SHORT_DASH);
plot UpperLine6 = regression + deviations6 * stdDeviation;
UpperLine6.SetStyle(Curve.SHORT_DASH);

# Colors

UpperLine.SetDefaultColor(GetColor(4));
MiddleLine.SetDefaultColor(GetColor(7));
LowerLine.SetDefaultColor(GetColor(4));

UpperLine2.SetDefaultColor(GetColor(1));
LowerLine2.SetDefaultColor(GetColor(1));

UpperLine3.SetDefaultColor(GetColor(3));
LowerLine3.SetDefaultColor(GetColor(3));

UpperLine4.SetDefaultColor(GetColor(4));
LowerLine4.SetDefaultColor(GetColor(4));

UpperLine5.SetDefaultColor(GetColor(1));
LowerLine5.SetDefaultColor(GetColor(1));

UpperLine6.SetDefaultColor(GetColor(3));
LowerLine6.SetDefaultColor(GetColor(3));

#### AnakManis

##### New member
VIP
I think you can buy the script from TBR....
Yeah, I can buy it. It's only \$40 something. I mean, I wouldn't mind spending \$200 as a VIP member, because I've appreciate the work that many coders have done here, and in return, I've learned quite a lot and have made some simple indicators myself, but what I'm saying is that the SD code is available here, meaning someone may have the knowledge to share the formula on how to get the equation. I can create the label, change the colors, etc... I just want to know the formula.

Yeah, I can simply buy the fish, but learning how to fish, in my opinion is the better way out.

Anyway, if no one cannot do it, that's fine. Thank you for the reply.

#### AnakManis

##### New member
VIP
This is what I have so far,
Code:
``AddLabel (showSDCLabels, "Deviation: " + Round (-(regression-close)), color.ORANGE);``
I don't think it's right at all. I tried to learn the formula of the StandardDeviation and was dumbfounded.
LMAO... After buying the indicator, my Label code was getting there. Stupid. Should've done it myself instead of spending \$50 on it. Sigh... If anyone wants a copy of my label code, I'll be charging it \$50 just to get my money back. LOL.

Here's a sample of my SD

Last edited:

#### JP382

##### New member
LMAO... After buying the indicator, my Label code was getting there. Stupid. Should've done it myself instead of spending \$50 on it. Sigh... If anyone wants a copy of my label code, I'll be charging it \$50 just to get my money back. LOL.

Here's a sample of my SD

You bought the Stand Dev indicator from TBR?!