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?

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

Member
2019 Donor
It's a TOS study, just search for StandardDevChannel.

mjamesb80

New member
@Astro X Æ A-12
Here's one I use occasionally that combines the TOS Standard Deviation Channels 1 and 2 into a single script.
The "2" standard deviations will change color when in an uptrend or downtrend.
Note: these settings won't be the exact settings as Connor's (Boiler Room Trading)

Code:
``````input price = close;
input deviations1 = 1.0;
input deviations2 = 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);
}

plot UpperLine = regression + deviations1 * stdDeviation;
plot MiddleLine = regression;
plot LowerLine = regression - deviations1 * stdDeviation;
plot UpperLine2 = regression + deviations2 * stdDeviation;
plot LowerLine2 = regression - deviations2 * stdDeviation;

UpperLine.SetDefaultColor(GetColor(8));
MiddleLine.SetDefaultColor(GetColor(8));
LowerLine.SetDefaultColor(GetColor(8));
#UpperLine2.SetDefaultColor(GetColor(8));
#LowerLine2.SetDefaultColor(GetColor(8));

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

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

Last edited by a moderator:

paraklesis

New member
I'd like to add a color coded up/down trend to the StandardDevChannel upperline and lowerline: for instance, if a ticker is in an uptrend on whatever timeframe being used, the upper and lower lines change to a color which can be set, such as green for up trend, and red for down trend. Been trying to code it myself but I'm not sure familiar with thinkScript's system yet. With my luck it's probably something simple and I've just been over complicating it to this point. Any help is appreciated.

Astro X Æ A-12

New member
@mjamesb80 OMG thank u so much! Thats really all i need!!! U are the genius

New member

here in this video Connor does explain's if you need more in detail. @Astro X Æ A-12

Joseph Patrick 18

Active member
@mjamesb80 OMG thank u so much! Thats really all i need!!! U are the genius

Hey Astro I think I cracked exactly what you were looking for because I was also looking for same thing...I've included a pic below with what you were looking for to exactly the specs of Connors video...I am also attaching a link for my WORKSPACE to give back to the community because I am not that technically advanced (newbie) ...so good luck and I hope I helped you out the same way everyone here as helped me..Please read what I wrote in pic to understand layout..any questions let me know.

Here is the pic of one of the two flex grids (lmao I couldn't figure out how to put pic in from Imgur). READ WHAT I WROTE IN PICTURE..

Here is a photo of my WORKSPACE...2 SCREENS 2 MONITORS

And here is my THINKORSWIM "FINAL WORKSPACE" LINK...

http://tos.mx/zLjmW7R

GOOD LUCK

Joe

Just click on my WORKSPACE and then in THINKORSWIM save it with a name that you want then got to top right corner of TOS and click on SETUP (with little gear icon) then scroll down to the name that you just saved and then my setup will come up...if any of the charts are black just scroll to the left I have like 300 to the right in settings...hope it helps

Also I just put detailed instructions so that if there are people who are newbies like me it will help them out a little bit.

Last edited by a moderator:

JoeSD

New member
VIP
@Joseph Patrick 18 I saw your June 2020 post re. Boiler Room and the TOS chart set-up you put together. I would love to try and mimic that if possible. I tried to use the workspace link you provided but it was nothing like the one in the top photo you provided. I particularly like the Squeeze colored candles and your initial 7/7. Could you send me a link to your grid with studies in the TOP photo from your post ( the one with the light blue writing)? Thanks. Joe

Joseph Patrick 18

Active member
@Joseph Patrick 18 I saw your June 2020 post re. Boiler Room and the TOS chart set-up you put together. I would love to try and mimic that if possible. I tried to use the workspace link you provided but it was nothing like the one in the top photo you provided. I particularly like the Squeeze colored candles and your initial 7/7. Could you send me a link to your grid with studies in the TOP photo from your post ( the one with the light blue writing)? Thanks. Joe

Hey Joe...I think I did this right...sorry about that...just let me know...this is the flex grid with Standard deviation channel..Read the Blue writing up above it explains it. Top and bottom chart are set to 1 day you can change to 5 or whatever you want. Top chart leave extended hours checked bottom unchecked and hence the diff deviations.

The one with blue writing is actually the bottom (last pic) right side of chart one on top of each other.

http://tos.mx/vPiMnXd
---------------------------------------------------------------------

this is the last pic also but the chart on the left side of chart

http://tos.mx/0xW4bGs
---------------------------------------------------------------------

this is the second pic down

http://tos.mx/TBJvTHZ
----------------------------------------------------------------------

I also added a tick chart...just start studying them...instead of say on a minute chart it draws a candle every minute...the tick chart will show a candle every time (I set it to 333 but you could put say 2000) 333 trades are executed...

I put a 21 day moving average exponential in blue...when the candles break above and pretty much stay above that is a buy signal and sometimes a huge upswing...on the flip side if it goes below blue its a sell or short signal and again depending on market and stock it could be a long short just put the gear icon on say 5 days (highest you can go on tick) scroll back and look at past days...also time on bottom of chart is different than say a 5 min chart where it will show 9:45 9:50 9:55...tick chart say it does 333 ticks (executed trades)fast as market opens you might see time on bottom 9:30 9:32 9:33 but when it slows down say lunch time the time on bottom might be 1:05 1:09 1:13 1:14 so at 1:05 333 trades happened then a candlestick appeared...the next candlestick didn't appear till 1:09 because the trades on that stock slowed down and it took 4 minutes to execute 333 trades...different animal compared to time but you get a more in depth look at volume and volatility.

and a Hull Moving Avg I adjusted to 9 and it lines up with the candles. (so for example (you can change colors) when candle is going up green you will see the hull line cyan blue color and when the candle is red going down the Hull color is magenta..

and on the bottom is a volume zone oscillator (its A TOS study) if you click on study and open it up it explains the buy and sell signals for the wave, zero lines +60 +40 etc

Here it is:
http://tos.mx/bYaY4BM

Let me know if you got everything okay..hope I could help...if you have anything interesting you want to share feel free...I am always open to learning new things..

Thanks good luck

Joe

I checked all of them they worked for me...

JoeSD

New member
VIP
Joe, just saw your reply and thanks. . I will take a look and try and implement.

Stay well.

Joe

Joseph Patrick 18

Active member
Joe, just saw your reply and thanks. . I will take a look and try and implement.

Stay well.

Joe
Okay you also.

Sammy800

New member
VIP
Astro practically what do you think is better for accuracy, tick chart with 333 or 2000. And do you think it gives a better picture compared to the candlestick charts. Its just your practical opinion. If I want to day trade and at times swing trade would a tick chart be better?
Thanks

Ronathan Edwards

New member
I have the deviation channels on my charts... Love it... Is there a way to set up a chart alert when the price goes, let's say "under the 1st lower line" ?
When I set up a condition for a study in the condition wizard, when I look for standard deviation channel, it only has standard deviation, and the plot only says StdDev and will not let me chose a lower line (like in the scanner).

Well-known member
VIP
I have Chart Labels that display when my Acrylic_VWAP study SD lines are crossed so, yes, it can be done... SDB2-X in the image below...

Last edited:

geremyh

New member
i did search for code, and or examples of code i could put in, but no success. I would like for these lines to extend right all the time, even when i change time frames...i've watched hours of Python tutorials on youtube and i understand the basics of what i'm looking at generally, but enhancing code is still beyond my skillset. I found this script right here on this site and love it...but i need it to extend to the right, so i can guesstimate future Std positions. any help appreciated

Code:
``````input price = close;
input deviations1 = 1.0;
input deviations2 = 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);
}

plot UpperLine = regression + deviations1 * stdDeviation;
plot MiddleLine = regression;
plot LowerLine = regression - deviations1 * stdDeviation;
plot UpperLine2 = regression + deviations2 * stdDeviation;
plot LowerLine2 = regression - deviations2 * stdDeviation;

UpperLine.SetDefaultColor(GetColor(8));
MiddleLine.SetDefaultColor(GetColor(8));
LowerLine.SetDefaultColor(GetColor(8));
#UpperLine2.SetDefaultColor(GetColor(8));
#LowerLine2.SetDefaultColor(GetColor(8));

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

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

BenTen

Staff
VIP
@geremyh I don't think it's possible, but hopefully someone else can do it.

geremyh

New member
@geremyh I don't think it's possible, but hopefully someone else can do it.
I found this short script that does extend....i just don't know how to change all that needs to be changed to utilize it in my script request...a whole new function has to be defined i think using new attributes and i just don't have the know-how yet
Code:
``````input aP = {default week, month, quarter};
input length = 1;
input displace = 1;
input showOnlyLastPeriod = no;

def lastBar = IsNaN(close[-1]) and !IsNaN(close);
def beyondLastBar = IsNaN(close[-1]) and IsNaN(close);

#this extends it past last bar to the right

def PrevDayClose = if !beyondLastBar then Highest(close(period = aP), length)[1] else PrevDayClose[1];
def closeSource = if (lastBar and beyondLastBar) then closeSource[1] else PrevDayClose;
plot CloseLine = if showOnlyLastPeriod and !IsNaN(close(period = aP)[-1]) then Double.NaN else closeSource;
CloseLine.SetDefaultColor(color.white);
CloseLine.SetPaintingStrategy(PaintingStrategy.horizontal);

def PrevDayOpen = if !beyondLastBar then Highest(open(period = aP), length)[1] else PrevDayOpen[1];
def openSource = if (lastBar and beyondLastBar) then openSource[1] else PrevDayOpen;
plot OpenLine = if showOnlyLastPeriod and !IsNaN(open(period = aP)[-1]) then Double.NaN else openSource;
OpenLine.SetDefaultColor(getColor(1));
OpenLine.SetPaintingStrategy(PaintingStrategy.DASHES);``````

BenTen

Staff
VIP
@geremyh I think both are entirely different. Your script relies on the latest data while the other script projects data from the previous day. I hope that makes sense.

geremyh

New member
figured it out, works in any time frame.

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