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
 

BrianPaul

New member
I'm looking for some help creating an auto price level study. For some context I use Connor's trading strategy from Boiler Room Trading. I set price levels based on the 1, 2, and 3 (with mid points) Standard Deviation lines. Below is the main study I use. Now what I normally do premarket is go into the 1day/1year, 4hr/180day, and 30min/10day time frames and set "price levels" based on where the standard deviation lines are in the premarket. Since these are somewhat higher time frames these levels don't seem to change much intraday, but the 30min time frame levels do change if the stock is really moving that day.

Two things I'm looking for...

1. Is there a way to just run something to auto set these standard deviation price levels (as $ price level lines) on the stock I'm looking at, on these 3 time frames so i don't have set them manually in the premarket?
2. If the item above is possible, can we have them update as the standard deviation lines move throughout the day?

To summarize, i just want to be able to see the standard deviation price levels on the three different time frames above while viewing a 1 min or 5 minute chart. From what I've heard around the forums, i know there are some limitations to showing studies on one time frame for another time frame. For some added context, I'm mainly trying to save time with my premarket review process in plotting these levels, and sadly for the time being i only trade with a laptop with one screen, so looking at multiple time frames while going back and forth through multiple windows is just plain annoying.

Im sure there are others that use this strategy as its pretty amazing how much standard deviation levels really opened my eyes to where stocks go. Any advice if you use this strategy is greatly appreciated where you found ways to save time.

Thanks

Code:
declare upper;

input FirstStdDev = 1;
input SecondStdDev = 2;
input ThirdStdDev = 3;

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 thirdUpperLine = regression + 3 * stdDeviation;
plot thirdLowerLine = regression - 3 * stdDeviation;
plot middleLine = regression;

#
# set properties
#
firstUpperLine.SetDefaultColor(Color.ORANGE);
firstLowerLine.SetDefaultColor(Color.ORANGE);
secondUpperLine.SetDefaultColor(Color.BLUE);
secondLowerLine.SetDefaultColor(Color.BLUE);
thirdUpperLine.SetDefaultColor(Color.GREEN);
thirdLowerLine.SetDefaultColor(Color.GREEN);
middleLine.SetDefaultColor(Color.RED);

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;
plot line5 = regression - 2.5 * stdDeviation;
plot line6 = regression + 2.5 * stdDeviation;


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

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

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

Juniorr

New member
Hi Brian, i am familiar with the studies from this group. Does this not happen on the scanner portion of the study? There is two parts to the Standard Deviation they use right? One on the chart and one they have as a watchlist?
 

BrianPaul

New member
Hello BrianPaul,

The use of all data that is loaded, skews the charts, I doubt that a stock will crash 6 levels

Yes, i agree. I more just plot the closest levels on the 1 day, 4hr and 30 min charts for intraday trades. Just didn't want to make a long post so figured it was a mute point. But could be wrong.
 

BrianPaul

New member
Hi Brian, i am familiar with the studies from this group. Does this not happen on the scanner portion of the study? There is two parts to the Standard Deviation they use right? One on the chart and one they have as a watchlist?

I am not a member of Boiler Room so didn't get the SD code or Scanner from him, just learned from his videos, so not sure if he has other tools available to save time. Just found code here for the SD 1-3 with midpoints in the forum and made small personal tweaks. I just trade my favorite stocks, like BA and NIO and SPY. If you've watched Connors video, you seem him price level out the SD levels, and I'm just trying to get rid of that manual process of setting the levels since i look at 3 different time frames and it takes about 3 mins or so per ticker.

Here is the strategy for anyone unfamiliar.

 

Tidan

New member
How might I retrieve the current Standard Deviation value(for any given deviation line) from a 4hr/180 day chart while I'm on a 1m/1d chart? E.g. the value the bubble at the side of the chart displays.
 
Last edited:

rad14733

Well-known member
VIP
@Tidan Simply use the code in the AddLabel() call above in a StandardDeviation() call... I'm going to leave it to you to head over into the Thinkscript Learning Center and research the StandardDeviation(price, length) function... No need in my heading over there and handing you code you can get for yourself... I'm here to help members learn, not to hand out code upon request, so you've been pointed and nudged in the right direction... If you get stumped, then is the time to ask for more help... These are basic function calls, not entire scripts...
 

Tidan

New member
@Tidan Simply use the code in the AddLabel() call above in a StandardDeviation() call... I'm going to leave it to you to head over into the Thinkscript Learning Center and research the StandardDeviation(price, length) function... No need in my heading over there and handing you code you can get for yourself... I'm here to help members learn, not to hand out code upon request, so you've been pointed and nudged in the right direction... If you get stumped, then is the time to ask for more help... These are basic function calls, not entire scripts...
Understood, and thank you again.
I have the script written up but have yet to figure out how to retrieve the data I'm after, thus the reason I broke down and asked the question here.

Perhaps it would help if I posted the code I'm working on.
The AddLabel function indeed shows the price of a given Deviation line, but I can't seem to get it to display the value of said deviation line as seen on the 180d/4h time frame WHILE I'm on a different time frame.


Code:
input price = close;
input deviation = 1.0;
input fullRange = Yes;
input length = 21;
input yesterday_hi_lo = Yes;

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

plot Line1Upper = regression + deviation * stdDeviation;
plot Line1UpperMiddle = regression + (deviation * .5) * stdDeviation;
plot Line2Upper = regression + (deviation * 2) * stdDeviation;
plot Line2UpperMiddle = regression + ((deviation * 2) * .75) * stdDeviation;
plot Line3Upper = regression + (deviation * 3) * stdDeviation;
plot Line3UpperMiddle = regression + ((deviation * 3) * .835) * stdDeviation;

plot MiddleLine = regression;

plot Line1Lower = regression - deviation * stdDeviation;
plot Line1LowerMiddle = regression - (deviation * .5) * stdDeviation;
plot Line2Lower = regression - (deviation * 2) * stdDeviation;
plot Line2LowerMiddle = regression - ((deviation * 2) * .75) * stdDeviation;
plot Line3Lower = regression - (deviation * 3) * stdDeviation;
plot Line3LowerMiddle = regression - ((deviation * 3) * .835) * stdDeviation;

Line1Upper.SetDefaultColor(GetColor(8));
#Line1Upper.HideBubble();
Line1UpperMiddle.SetDefaultColor(Color.GRAY);
Line1UpperMiddle.SetStyle(Curve.SHORT_DASH);
#Line1UpperMiddle.HideBubble();
Line2Upper.SetDefaultColor(Color.DARK_ORANGE);
#Line2Upper.HideBubble();
Line2UpperMiddle.SetDefaultColor(Color.GRAY);
Line2UpperMiddle.SetStyle(Curve.SHORT_DASH);
#Line2UpperMiddle.HideBubble();
Line3Upper.SetDefaultColor(Color.RED);
#Line3Upper.HideBubble();
Line3UpperMiddle.SetDefaultColor(Color.GRAY);
Line3UpperMiddle.SetStyle(Curve.SHORT_DASH);
#Line3UpperMiddle.HideBubble();

MiddleLine.SetDefaultColor(Color.WHITE);
#MiddleLine.HideBubble();

Line1Lower.SetDefaultColor(GetColor(8));
#Line1Lower.HideBubble();
Line1LowerMiddle.SetDefaultColor(Color.GRAY);
Line1LowerMiddle.SetStyle(Curve.SHORT_DASH);
#Line1LowerMiddle.HideBubble();
Line2Lower.SetDefaultColor(Color.DARK_ORANGE);
#Line2Lower.HideBubble();
Line2LowerMiddle.SetDefaultColor(Color.GRAY);
Line2LowerMiddle.SetStyle(Curve.SHORT_DASH);
#Line2LowerMiddle.HideBubble();
Line3Lower.SetDefaultColor(Color.RED);
#Line3Lower.HideBubble();
Line3LowerMiddle.SetDefaultColor(Color.GRAY);
Line3LowerMiddle.SetStyle(Curve.SHORT_DASH);
#Line3LowerMiddle.HideBubble();


#testing extension of std lines:
#def ext_1 = GetHighestValueArea((regression + deviation * stdDeviation));
#def ext_1 = regression + deviation * stdDeviation;
#def ext_1 = close(period = AggregationPeriod.hour);
#plot ext_line = close(period = AggregationPeriod.HOUR, ext_1);
#plot ext_line = getMaxValueOffset(middleLine,1);
#plot ext_line = absvalue(close - MiddleLine);
#def GetP = GetAggregationPeriod();
#plot ext_line = high(period = GetP, deviation);
#ext_line.SetDefaultColor(Color.CYAN);
#ext_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def ext_1 = getValue(Line1Upper, close(period = AggregationPeriod.FOUR_HOURS));
AddLabel(yes, ext_1, Color.WHITE);

#the rest of the code is unrelated and has been snipped out....
 
Last edited:

rad14733

Well-known member
VIP
@Tidan You're close... What you need to do is create another logic structure like you have for your chart for regression and another LineUpper but using close(period = AggregationPeriod.FOUR_HOURS)... You can't mix the two aggregation periods or you won't get the results you want, which is how you presently have it coded... Maybe call them regression4H and LineUpper4H... That should be enough to get you where you want to be...
 

Tidan

New member
@Tidan You're close... What you need to do is create another logic structure like you have for your chart for regression and another LineUpper but using close(period = AggregationPeriod.FOUR_HOURS)... You can't mix the two aggregation periods or you won't get the results you want, which is how you presently have it coded... Maybe call them regression4H and LineUpper4H... That should be enough to get you where you want to be...
Thank you Rad.
If I create a separate study called 'std dev extensions' and do something like the following, would this be correct?
Code:
def price = close(period = AggregationPeriod.FOUR_HOURS);
input deviation = 1.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);
}
# etc...

And if instead of 'addLabel', I wanted to have a straight horizontal price line extending to the right which will reflect the current price of a given Deviation line, is that pretty straight forward, i.e. a 'plot' containing Line1Upper for example?
E.g.:
Code:
#...
Def Line1Upper = regression + deviation * stdDeviation;
Plot Line1UpperExt = getValue(Line1Upper, close);

#etc...
 
Last edited:

rad14733

Well-known member
VIP
@Tidan Yes, that should work... You could also have both the current and FOUR_HOUR in a single Study once you get the logic the way you want it... Just use different variable names like I described above... For the horizontal line you want to only use the current bars price... There are many example of how to do this here in the forums... Some paint across the entire chart while others only paint to the right...
 

Tidan

New member
@Tidan Yes, that should work... You could also have both the current and FOUR_HOUR in a single Study once you get the logic the way you want it... Just use different variable names like I described above... For the horizontal line you want to only use the current bars price... There are many example of how to do this here in the forums... Some paint across the entire chart while others only paint to the right...
Thank you..
I edited my previous comment as you posted this. Does this plot code appear accurate?
Code:
#...
Def Line1Upper = regression + deviation * stdDeviation;
Plot Line1UpperExt = getValue(Line1Upper, close);
#etc...
 
Last edited:

Tidan

New member
Unfortunately it didn't work.

Here is what I was hoping for:
On the 180d/4h chart(the purple lines are the extensions I am trying to re-create):
180d.png


And how I want them to appear on the 1d/5m chart(Note how they maintain their value from the 180d/4h chart):
1d.png


But this is how they are displaying with the new script:
1dFail.png



Is there something I've done incorrectly here?
Code:
# Draw 180d standard deviation lines horizontaly on smaller timeframe charts.

def price = close(period = AggregationPeriod.FOUR_HOURS);
input deviation = 1.0;
input fullRange = Yes;
input length = 21;
#input yesterday_hi_lo = Yes;

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

plot Line1Upper = getValue(regression + deviation * stdDeviation, close(period = AggregationPeriod.FOUR_HOURS));
plot Line1UpperMiddle = regression + (deviation * .5) * stdDeviation;
plot Line2Upper = regression + (deviation * 2) * stdDeviation;
plot Line2UpperMiddle = regression + ((deviation * 2) * .75) * stdDeviation;
plot Line3Upper = regression + (deviation * 3) * stdDeviation;
plot Line3UpperMiddle = regression + ((deviation * 3) * .835) * stdDeviation;

plot MiddleLine = regression;

plot Line1Lower = regression - deviation * stdDeviation;
plot Line1LowerMiddle = regression - (deviation * .5) * stdDeviation;
plot Line2Lower = regression - (deviation * 2) * stdDeviation;
plot Line2LowerMiddle = regression - ((deviation * 2) * .75) * stdDeviation;
plot Line3Lower = regression - (deviation * 3) * stdDeviation;
plot Line3LowerMiddle = regression - ((deviation * 3) * .835) * stdDeviation;

Line1Upper.SetDefaultColor(color.cyan); #GetColor(8)
Line1Upper.SetStyle(Curve.MEDIUM_DASH);
Line1Upper.HideBubble();
Line1UpperMiddle.SetDefaultColor(Color.GRAY);
Line1UpperMiddle.SetStyle(Curve.MEDIUM_DASH);
Line1UpperMiddle.SetStyle(Curve.MEDIUM_DASH);
Line1UpperMiddle.HideBubble();
Line2Upper.SetDefaultColor(Color.DARK_ORANGE);
Line2Upper.SetStyle(Curve.MEDIUM_DASH);
Line2Upper.HideBubble();
Line2UpperMiddle.SetDefaultColor(Color.GRAY);
Line2UpperMiddle.SetStyle(Curve.MEDIUM_DASH);
Line2UpperMiddle.HideBubble();
Line3Upper.SetDefaultColor(Color.RED);
Line3Upper.SetStyle(Curve.MEDIUM_DASH);
Line3Upper.HideBubble();
Line3UpperMiddle.SetDefaultColor(Color.GRAY);
Line3UpperMiddle.SetStyle(Curve.MEDIUM_DASH);
Line3UpperMiddle.HideBubble();

MiddleLine.SetDefaultColor(Color.WHITE);
MiddleLine.SetStyle(Curve.MEDIUM_DASH);
MiddleLine.HideBubble();

Line1Lower.SetDefaultColor(GetColor(8));
Line1Lower.SetStyle(Curve.MEDIUM_DASH);
Line1Lower.HideBubble();
Line1LowerMiddle.SetDefaultColor(Color.GRAY);
Line1LowerMiddle.SetStyle(Curve.MEDIUM_DASH);
Line1LowerMiddle.HideBubble();
Line2Lower.SetDefaultColor(Color.DARK_ORANGE);
Line2Lower.SetStyle(Curve.MEDIUM_DASH);
Line2Lower.HideBubble();
Line2LowerMiddle.SetDefaultColor(Color.GRAY);
Line2LowerMiddle.SetStyle(Curve.MEDIUM_DASH);
Line2LowerMiddle.HideBubble();
Line3Lower.SetDefaultColor(Color.RED);
Line3Lower.SetStyle(Curve.MEDIUM_DASH);
Line3Lower.HideBubble();
Line3LowerMiddle.SetDefaultColor(Color.GRAY);
Line3LowerMiddle.SetStyle(Curve.MEDIUM_DASH);
Line3LowerMiddle.HideBubble();
 
Last edited:

rad14733

Well-known member
VIP
@Tidan You'll most likely want to use the value of yesterdays last aggregation to carry across todays chart to eliminate variation throughout the day... I don't have time to scrounge around for the code to accomplish that right now but that's what you would parse for and use for the entirety of today... That would be similar to plotting yesterdays high and low on todays chart, except it would be the last 4H aggregation...
 

Tidan

New member
@Tidan You'll most likely want to use the value of yesterdays last aggregation to carry across todays chart to eliminate variation throughout the day... I don't have time to scrounge around for the code to accomplish that right now but that's what you would parse for and use for the entirety of today... That would be similar to plotting yesterdays high and low on todays chart, except it would be the last 4H aggregation...

Something like this?
Code:
def price = close(period = AggregationPeriod.FOUR_HOURS)[1];

Update: I tried that and it didn't work either. Continued to draw jagged lines which did not line up with the 180d/4h marks.
Any other suggestions?
 
Last edited:

Similar threads

Top