Linear Regression CANDLEs For ThinkOrSwim

It's just another way of visualizing trends and changes in direction. I've been playing around with linear regression curves for a while (see https://usethinkscript.com/threads/...on-from-the-centerline-for-thinkorswim.10726/) and find them fascinating to use for visualization.

This indicator is looking at the linear regression curves (last point thereof) for open and close and drawing candles that indicate the distance and relative position of those two curves (the study I mentioned above uses the HL2 and close curves in a different way to determine direction of movement).

It's all just another way of looking at the same basic four things. We search and search for something that 'clicks' with us and then hope it keeps us from doing silly things with our cash balances.

-mashume
If you need additional insight on silly and even stupid things to do with your cash balances I am a freaking expert; ready willing and able and eager to show traders how, so I can be part of a group.

In regard to the indicator, I have a gut feel this thing is going to benefit me and maybe big time. Terrific work and thanks!
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

check the below. I add some potential signals where you need to confirm through other confluences.

CSS:
#study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)
# Converted and mod by Sam4COK@Samer800 - 12/2022

input BarColor = yes;
input ShowSignal = yes;
input HidePricePlot = no;
input HideLinRegCandles = no;
input signal_length = 11;    # "Signal Smoothing"
input signalMovAvg = AverageType.SIMPLE; # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"

HidePricePlot(HidePricePlot);

def na = Double.NaN;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low,  linreg_length) else low;
def bclose = if lin_reg then Inertia(close,linreg_length) else close;

def ohlc = (bopen + bhigh + blow + bclose) / 4;
def candleUp = if HideLinRegCandles then na else bopen < bclose;
def signal = MovingAverage(signalMovAvg, bclose, signal_length);

#// Plot Candles
AddChart(high = if candleUp then bhigh else na,
         low  = if candleUp then blow else na,
         open = if candleUp then bclose else na,
        close = if candleUp then bopen else na,
         type = ChartType.CANDLE, growcolor =  CreateColor(0,153,153));

AddChart(high = if candleUp then na else bhigh,
          low = if candleUp then na else blow ,
         open = if candleUp then na else bopen,
        close = if candleUp then na else bclose,
         type = ChartType.CANDLE, growcolor =  CreateColor(153,0,153));

# --- Signal Line
def sigUp = signal>signal[1];
plot SigLine = signal;
SigLine.AssignValueColor(if sigUp then Color.GREEN else Color.RED);
#--Bar Color
def raising = bclose > bopen;
def ExtHi = raising and sigUp;
def Hi = !ExtHi and (raising or sigUp);
def ExtLo =  !raising and !sigUp;
def Lo = !ExtLo and (!raising or !sigUp);

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if ExtHi then Color.GREEN else
                 if Hi then Color.DARK_GREEN else
                 if ExtLo then Color.RED else
                 if Lo then Color.DARK_RED else Color.GRAY);
# ---- Signal
def up = signal>signal[1] and ohlc>signal and ohlc>ohlc[1];
def countUp = if up then CountUp[1] + 1 else 0;
def Dn = signal<signal[1] and ohlc<signal and ohlc<ohlc[1];
def countDn = if Dn then CountDn[1] + 1 else 0;

plot PtUp = if countUp==1 and ShowSignal then signal else na;
PtUp.SetLineWeight(3);
PtUp.SetDefaultColor(Color.GREEN);
PtUp.SetStyle(Curve.POINTS);
plot PtDn = if countDn==1 and ShowSignal then signal else na;
PtDn.SetLineWeight(3);
PtDn.SetDefaultColor(Color.RED);
PtDn.SetStyle(Curve.POINTS);


#--- END CODE
This looks good
I like it because it kind of filters out the noise so it looks better visually
I have to see now if trading it as as easy as it looks
Thanks for making this indicator
 
Hi All! Happy New Year! I am new at this and can't figure out how to leave the line, but take the 'S' and 'B' off the chart. Can someone there tell me how to do that? You guys do great work! Thanks in advance.
 
Are you talking about the linear regression candle indicator or something else ?
I don't see any 'b' or 's' on the chart Perhaps you have another indicator loaded ?
On that other indicator looks for "Show label" option on turn that off
 
here is last update to include option to change the linear plot from candle to line.

CSS:
#study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)
# Converted and mod by Sam4COK@Samer800 - 12/2022 - Update - option to change plot Line/Candle

input BarColor = no;
input ShowSignal = yes;
input HidePricePlot = no;
input LinRegStyle = {Default Line, Candle, None};
input signal_length = 11;    # "Signal Smoothing"
input signalMovAvg = AverageType.SIMPLE; # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"

HidePricePlot(HidePricePlot);

def na = Double.NaN;
def Style = if LinRegStyle==LinRegStyle.Line then 1 else
            if LinRegStyle==LinRegStyle.Candle then -1 else 0;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low,  linreg_length) else low;
def bclose = if lin_reg then Inertia(close,linreg_length) else close;

def ohlc = (bopen + bhigh + blow + bclose) / 4;
def raising = bclose > bopen;
def candleUp = if Style>=0 then na else bopen < bclose;
def signal = MovingAverage(signalMovAvg, bclose, signal_length);

plot candleLine = if Style==1 then ohlc else na;
candleLine.AssignValueColor(if raising then Color.CYAN else Color.MAGENTA);
#// Plot Candles
AddChart(high = if candleUp then bhigh else na,
         low  = if candleUp then blow else na,
         open = if candleUp then bclose else na,
        close = if candleUp then bopen else na,
         type = ChartType.CANDLE, growcolor =  CreateColor(0,153,153));

AddChart(high = if candleUp then na else bhigh,
          low = if candleUp then na else blow ,
         open = if candleUp then na else bopen,
        close = if candleUp then na else bclose,
         type = ChartType.CANDLE, growcolor =  CreateColor(153,0,153));

# --- Signal Line
def sigUp = signal>signal[1];
plot SigLine = signal;
SigLine.AssignValueColor(if sigUp then Color.GREEN else Color.RED);
#--Bar Color
def ExtHi = raising and sigUp;
def Hi = !ExtHi and (raising or sigUp);
def ExtLo =  !raising and !sigUp;
def Lo = !ExtLo and (!raising or !sigUp);

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if ExtHi then Color.GREEN else
                 if Hi then Color.DARK_GREEN else
                 if ExtLo then Color.RED else
                 if Lo then Color.DARK_RED else Color.GRAY);
# ---- Signal
def up = signal>signal[1] and ohlc>signal and ohlc>ohlc[1];
def countUp = if up then CountUp[1] + 1 else 0;
def Dn = signal<signal[1] and ohlc<signal and ohlc<ohlc[1];
def countDn = if Dn then CountDn[1] + 1 else 0;

plot PtUp = if countUp==1 and ShowSignal then signal else na;
PtUp.SetLineWeight(3);
PtUp.SetDefaultColor(Color.GREEN);
PtUp.SetStyle(Curve.POINTS);
plot PtDn = if countDn==1 and ShowSignal then signal else na;
PtDn.SetLineWeight(3);
PtDn.SetDefaultColor(Color.RED);
PtDn.SetStyle(Curve.POINTS);


#--- END CODE
Hello Samer800, great job! Love this indicator! Question for you: Is there a way for the Linear Regression to make a distinction where there Gap in Price? For instance, it could be a Change in the Color of the Candle? or simply show the Gap?
 
This is a great indicator! Would it be possible to add a scan feature for 1) when the bars change colors, to provide a heads up on the smoothing line changing colors; and then 2) when the smoothing line changes colors for a second scan?
Thanks again!
 
Hello Samer800, great job! Love this indicator! Question for you: Is there a way for the Linear Regression to make a distinction where there Gap in Price? For instance, it could be a Change in the Color of the Candle? or simply show the Gap?
What is your definition of gap? to modify the script, the condition needs to be defined

This is a great indicator! Would it be possible to add a scan feature for 1) when the bars change colors, to provide a heads up on the smoothing line changing colors; and then 2) when the smoothing line changes colors for a second scan?
Thanks again!
I would also like to request a scan that would identify when signal line changes color if possible.
There are 4 colors. You need to provide a more precise definition of the condition that you want to scan for.
 
https://tos.mx/!3ZZdCFdq I am kinda new to posting info! Would like to know if I did this right. Also if you see this chart and you can tell me what you think. If not I apologize! thanks! Just did this from everything on here. I do not know how to script code - to everyone that does thank you!
 
Last edited:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
339 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top