Linear Regression CANDLEs For ThinkOrSwim

That's interesting. ToS has very limited support for plotting additional candlesticks... but perhaps with a bit of trickery and unsupported functions: I didn't implement the different smoothings etc... just the bare bones

Code:
declare upper;

input length = 14;

def o = LinearRegCurve(length = length, price = OPEN);
def h = LinearRegCurve(length = length, price = HIGH);
def l = LinearRegCurve(length = length, price = LOW);
def c = LinearRegCurve(length = length, price = CLOSE);

def o_up = if c > c[1] then o else double.nan;
def h_up = if c > c[1] then h else double.nan;
def l_up = if c > c[1] then l else double.nan;
def c_up = if c > c[1] then c else double.nan;

def o_dn = if c <= c[1] then o else double.nan;
def h_dn = if c <= c[1] then h else double.nan;
def l_dn = if c <= c[1] then l else double.nan;
def c_dn = if c <= c[1] then c else double.nan;


AddChart(open = o_up, high = h_up, low = l_up, close = c_up, growColor = color.green, fallColor = color.green, neutralColor = color.green, type = ChartType.CANDLE);

AddChart(open = o_dn, high = h_dn, low = l_dn, close = c_dn, growColor = color.red, fallColor = color.red, neutralColor = color.red, type = ChartType.CANDLE);

input signal_type = averageType.SIMPLE;
input signal_length = 11;
plot signal = MovingAverage(data = c, length = signal_length, averageType = signal_type);

hope that helps.
-mashume
 
Last edited by a moderator:
That's interesting. ToS has very limited support for plotting additional candlesticks... but perhaps with a bit of trickery and unsupported functions: I didn't implement the different smoothings etc... just the bare bones

Code:
declare upper;

input length = 14;

def o = LinearRegCurve(length = length, price = OPEN);
def h = LinearRegCurve(length = length, price = HIGH);
def l = LinearRegCurve(length = length, price = LOW);
def c = LinearRegCurve(length = length, price = CLOSE);

def o_up = if c > c[1] then o else double.nan;
def h_up = if c > c[1] then h else double.nan;
def l_up = if c > c[1] then l else double.nan;
def c_up = if c > c[1] then c else double.nan;

def o_dn = if c <= c[1] then o else double.nan;
def h_dn = if c <= c[1] then h else double.nan;
def l_dn = if c <= c[1] then l else double.nan;
def c_dn = if c <= c[1] then c else double.nan;


AddChart(open = o_up, high = h_up, low = l_up, close = c_up, growColor = color.green, fallColor = color.green, neutralColor = color.green, type = ChartType.CANDLE);

AddChart(open = o_dn, high = h_dn, low = l_dn, close = c_dn, growColor = color.red, fallColor = color.red, neutralColor = color.red, type = ChartType.CANDLE);

hope that helps.
-mashume
hey you did an excellent job with the candles and that is no easy task on TOS, but can I have the indicator wave line also ? if you look at the trading view one it has a wave line that shows the exact cross over point. The script you did has a plot line { which like all tos scripts I turn the plot line off} hope I'm not asking too much. i must say the candles you did are unbelievable. thanks again
 
hey you did an excellent job with the candles and that is no easy task on TOS, but can I have the indicator wave line also ? if you look at the trading view one it has a wave line that shows the exact cross over point. The script you did has a plot line { which like all tos scripts I turn the plot line off} hope I'm not asking too much. i must say the candles you did are unbelievable. thanks again
Add these three lines to the end of the indicator and set your colours to taste
Code:
input signal_type = averageType.SIMPLE;
input signal_length = 11;
plot signal = MovingAverage(data = c, length = signal_length, averageType = signal_type);

-mashume
 
That's interesting. ToS has very limited support for plotting additional candlesticks... but perhaps with a bit of trickery and unsupported functions: I didn't implement the different smoothings etc... just the bare bones

Code:
declare upper;

input length = 14;

def o = LinearRegCurve(length = length, price = OPEN);
def h = LinearRegCurve(length = length, price = HIGH);
def l = LinearRegCurve(length = length, price = LOW);
def c = LinearRegCurve(length = length, price = CLOSE);

def o_up = if c > c[1] then o else double.nan;
def h_up = if c > c[1] then h else double.nan;
def l_up = if c > c[1] then l else double.nan;
def c_up = if c > c[1] then c else double.nan;

def o_dn = if c <= c[1] then o else double.nan;
def h_dn = if c <= c[1] then h else double.nan;
def l_dn = if c <= c[1] then l else double.nan;
def c_dn = if c <= c[1] then c else double.nan;


AddChart(open = o_up, high = h_up, low = l_up, close = c_up, growColor = color.green, fallColor = color.green, neutralColor = color.green, type = ChartType.CANDLE);

AddChart(open = o_dn, high = h_dn, low = l_dn, close = c_dn, growColor = color.red, fallColor = color.red, neutralColor = color.red, type = ChartType.CANDLE);

input signal_type = averageType.SIMPLE;
input signal_length = 11;
plot signal = MovingAverage(data = c, length = signal_length, averageType = signal_type);

hope that helps.
-mashume
great job @mashume , do you think it is possible to changes the candles for dot or something out, because with a candles is a little bit messy. thank you in advance
 
great job @mashume , do you think it is possible to changes the candles for dot or something out, because with a candles is a little bit messy. thank you in advance
ThinkOrSwim "supports" the following types of charts*

ChartType.AREA *
ChartType.BAR
ChartType.CANDLE
ChartType.CANDLE_TREND
ChartType.EQUIVOLUME *
ChartType.HEIKIN_ASHI
ChartType.LINE

I marked the equivolume and AREA with a star since they caused my instance of ToS to crash repeatedly and constantly for many minutes and restarts. The crash persisted through restart, and only loading paper money, quitting, and then going back to live trading and switching workspaces (before the indicator could load) and then editing the code without the indicator on screen allowed me to recover ToS.

-mashume

Of course, you could do something else with the data holders (c_up, c_dn, etc...) like plot statements rather than addChart().

EDIT: Part of the fun of this one is the changing height of the candle body showing the strength of the movement. You could 'clean up' a bit by specifying the h_up, h_dn, l_up, and l_dn as double.nan and so the wicks would never print to screen. That way you would get the candle body for the visual impact of the changing sizes and perhaps be a bit cleaner.

POST EDIT:
I really like this without the candle wicks, so I thought I'd throw the code up here:
Code:
declare upper;

input length = 14;
input entry_exit_multiplier = 1.5;
input body_only = no;

def o = LinearRegCurve(length = length, price = OPEN);
def h = LinearRegCurve(length = length, price = HIGH);
def l = LinearRegCurve(length = length, price = LOW);
def c = LinearRegCurve(length = length, price = CLOSE);

def o_up = if c > c[1] then o else double.nan;
def h_up = if c > c[1] then if body_only == yes then max(o, c) else h else double.nan;
def l_up = if c > c[1] then if body_only == yes then min(o, c) else l else double.nan;
def c_up = if c > c[1] then c else double.nan;

def o_dn = if c <= c[1] then o else double.nan;
def h_dn = if c <= c[1] then if body_only == yes then max(o, c) else h else double.nan;
def l_dn = if c <= c[1] then if body_only == yes then min(o, c) else l else double.nan;
def c_dn = if c <= c[1] then c else double.nan;


AddChart(open = o_up, high = h_up, low = l_up, close = c_up, growColor = color.green, fallColor = color.green, neutralColor = color.green, type = ChartType.CANDLE);

AddChart(open = o_dn, high = h_dn, low = l_dn, close = c_dn, growColor = color.red, fallColor = color.red, neutralColor = color.red, type = ChartType.CANDLE);

input signal_type = averageType.SIMPLE;
input signal_length = 11;
plot signal = MovingAverage(data = c, length = signal_length, averageType = signal_type);
 
Last edited:
Thanks you guys read my mind. It seemed busy on trading view it was really busy on tos. I really only wanted the wave. I like it better than the vwap or other wave/cross over indicators. Along with a dmi/adx read it seems to be pretty nice for scalping. I know the candles where the hard part. Funny cause on the mobile app it only showed the wave. I was like cool. ☹️
 
I saw this indicator on YouTube which is an indicator available on Tradingview and I was wondering if anyone knows how this works or maybe if it can be converted to be used on TOS
I heard of linear regression but I am not sure how it's used on a candle
From what I observed by using it on Tradingview it seems to break up a large candle into smaller candles so I am not sure if it can actually be traded based on this indicator
Let's say there was a big up bar candle of 4 points on a 1 min chart using the linear regression candle indicator will show 4 separate smaller up candles more or less equal sized 1 minute apart
So the way I see it if I try to use this indicator and try to enter after the first bar it most likely will not get filled because the price went past the candle onto the next one
most likely missing the big move

Here is the indicator in pine script
//@version=4
study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)

signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)

lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)

bopen = lin_reg ? linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
blow = lin_reg ? linreg(low, linreg_length, 0) : low
bclose = lin_reg ? linreg(close, linreg_length, 0) : close

r = bopen < bclose

signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)

plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

plot(signal, color=color.white)
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
 
How do you guys use this to trade? Chart is a mess.


3Vbx6wF.png
 
@mashume Would it be possible to convert this indicator to a lower indicator so only the linear candles will show up? I converted the candlestick chart to a line chart, and it did make the chart cleaner and the linear candles easier to read however, I wonder if converting it into a lower indicator would be better?
 
@mashume Would it be possible to convert this indicator to a lower indicator so only the linear candles will show up? I converted the candlestick chart to a line chart, and it did make the chart cleaner and the linear candles easier to read however, I wonder if converting it into a lower indicator would be better?
just add the below at the begning of the script.

Code:
declare lower;
 
just add the below at the begning of the script.

Code:
declare lower;

Awesome! Looks like that solved it. I was going to do that but thought it might mess up the indicator. Added a candlestick painting strategy if anyone wants to do that add this below the code:

assignpriceColor(if c > signal then color.light_green else color.red);
 
Awesome! Looks like that solved it. I was going to do that but thought it might mess up the indicator. Added a candlestick painting strategy if anyone wants to do that add this below the code:

assignpriceColor(if c > signal then color.light_green else color.red);
try my conversion in post#9, it include bar color feature as well.
 
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
 
I played around with it looking like this:

nHkluQN.png


It's still messy, but the black bars are my preferred color scheme anyway, and the color overlay doesn't disturb much

-mashume
Okay, but when do you enter? And what is your lowers indicator, please (OCEAN_NMM)? Thanks
 
Okay, but when do you enter? And what is your lowers indicator, please (OCEAN_NMM)? Thanks
I haven't used it for timing entries... this is just a study chart (looking at new and intersting things) I've looked at it for trend following.
and I'll post up the ocean lower when I work out some of the bugs. -- should be soon, it was my project for last weekend.

-mashume
 
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
Will try it thanks a lot! I know this sounds stupid but what is the regression study telling us?
 
Will try it thanks a lot! I know this sounds stupid but what is the regression study telling us?
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
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
398 Online
Create Post

Similar threads

Similar threads

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