TradingView's Linear Regression indicator For ThinkOrSwim

JonPM

New member
Does anyone have or know of a TOS script that is similar to TradingView's built-in Linear Regression script (blue/red channel in image below)?

RaOMcO3D
 

BenTen

Administrative
Staff member
Staff
VIP
Linear Regression channel is available as a built-in drawing tool in your ThinkorSwim. However, it will not include the blue/green zones like TradingView.

JYkyGXZ.png
 

diazlaz

Well-known member
2019 Donor
VIP
Hi @JonPM,

here you go:

5PhMgRI.png


aoy9vl1.png


Ruby:
#tradingview like Linear Channel w/ fills
#@JonPM Community Request
#
#VERSION
# 2020.09.08 V1.0 @diazlaz - Linear Regression Channel with Cloud and variable
#                            timeframe.
#

input price = close;
input deviations = .8;  #set your deviation units here.
input length = 100; #set your channel lookback period here.

def regression = InertiaAll(price, length);
def stdDeviation = stdevAll(price, length);

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

AddCloud (MiddleLine, UpperLine, COLOR.GREEN, COLOR.GREEN);
AddCloud (MiddleLine, LowerLine, COLOR.RED, COLOR.RED);

UpperLine.SetDefaultColor(COLOR.GREEN);
MiddleLine.SetDefaultColor(GetColor(8));
LowerLine.SetDefaultColor(COLOR.RED);

#END of tradingview like Linear Channel w/ fills
 
Last edited by a moderator:

markos

Well-known member
VIP
Hey @diazlaz Have you worked w "Opacity" in TOS? Just wondering if we can make those SD clouds more transparent. I've seen it work but can't find the example to borrow from. Thanks, Markos
 

Ficus

New member
I was able to add opacity. It helps a lot to see the chart behind it, but the opacity adjustment doesn't work for me, seems locked at 50%. Thought someone else might appreciate it.

Code:
#tradingview like Linear Channel w/ fills
#@JonPM Community Request
#
#VERSION
# 2020.09.08 V1.0 @diazlaz - Linear Regression Channel with Cloud and variable
#                            timeframe.
#

input price = close;
input deviations = .8;  #set your deviation units here.
input length = 100; #set your channel lookback period here.
input opacity = 50;

def regression = InertiaAll(price, length);
def stdDeviation = stdevAll(price, length);

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

AddCloud(MiddleLine,  UpperLine,  Color.GREEN,  Color.GREEN, opacity);
AddCloud(MiddleLine,  LowerLine,  Color.RED,  Color.RED, opacity);

UpperLine.SetDefaultColor(Color.GREEN);
MiddleLine.SetDefaultColor(GetColor(8));
LowerLine.SetDefaultColor(Color.RED);

#END of tradingview like Linear Channel w/ fills
 

markos

Well-known member
VIP
W0DhIUB.png


http://tos.mx/N6lW6NE

@diazlaz @Ficus I wanted to have an LRC that was opaque. This was done w help from TOS Support.

Color Opacity can be changed to what ever one wishes via the UI under Global Colors>More...>HSV color panel.

Code:
# LRC no Lines Global Color Adj
# With help from coder in TOS chat support 9-18-2020
# Originally: tradingview like Linear Channel w/ fills
# @JonPM Community Request in useThinkscript
# VERSION
# 2020.09.08 V1.0 @diazlaz - Linear Regression Channel with Cloud and variable
#                            timeframe.
# 2020.09.18 V1.1 @markos - Color Opacity can now be changed via Globals in UI.

input price = close;
input deviations = 1.0;  #set your deviation units here.
input length = 80; #set your channel lookback period here.

def regression = InertiaAll(price, length);
def stdDeviation = StDevAll(price, length);
def UpCloud = regression + deviations * stdDeviation;
def DnCloud = regression - deviations * stdDeviation;
def MiddleLine = regression;

DefineGlobalColor("Bullish", Color.light_green);
DefineGlobalColor("Bearish", Color.light_RED);

AddCloud(middleline,  upcloud,  GlobalColor("Bullish"),  GlobalColor("Bullish"));
AddCloud(middleline,  dncloud,  GlobalColor("Bearish"),  GlobalColor("Bearish"));

# End LRC wo Lines
 
Last edited:

wtf_dude

Active member
How do you guys trade using this?
You don't really trade based on this alone. The green and red zones give you an idea of possible support and resistance as well as the overall trend direction. Just notice how the price seems to bump up and down near the top, bottom and middle.
 

tutianlong

New member
VIP
how do you code to count the number of times the middle line cuts through the body of the candle? Say input length is 80, so i want to see among the 80 candles, how many of the candle bodies got cut through by the middle line.
 

wtf_dude

Active member
how do you code to count the number of times the middle line cuts through the body of the candle? Say input length is 80, so i want to see among the 80 candles, how many of the candle bodies got cut through by the middle line.
phewww you got your work cut out for you. What exactly are you going to use that for? Just curious
 

mashume

Well-known member
VIP
Lifetime
how do you code to count the number of times the middle line cuts through the body of the candle? Say input length is 80, so i want to see among the 80 candles, how many of the candle bodies got cut through by the middle line.
I know you put in the request a LONG time ago, but I had a thought.
you can create a series:
Code:
def pierced_bars = if open > MiddleLine and close < MiddleLine then 1 else 0;
and then once you have that, you can sum() the series over the desired length:
Code:
addLabel(yes, "Pierced Bars Last 80: " + sum(pierced_bars, 80), color.gray);
and have some idea.
You could also chart it on a lower study and see how it changes over time. I don't know if it will repaint, or give the number of bars that CURRENTLY fit the condition but I assume so (it will be recalculated at each new bar). If you need the historical values, it gets much, much messier.

Hope this is still of some help.

-mashume

P.S. I've coded all this in the post-editor, not in ToS, it may not run quite right right off, but it should be close.
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Whats the reasoning behind 80 length and 100 length i know they are counting bars but what is the standard linear regression look back length? Thanks
Like all trendlines, swing high / lows, pivots, etc... Regression channels repaint. The use of standard lengths of 155 have been found to help minimize the repainting effect due to having so many stabilizing data points.
 

Similar threads

Top