linear regression plot dependent on condition

Glefdar

Active member
If functions (such as "movingaverage" or "stdev") expect a constant as the length parameter, an error is returned when attempting to instead get the length parameter from a variable.

So if we want to calculate a moving average or standard deviation with a length that varies based on market conditions, it will be necessary to manually write code for movingaverage and stdev instead of using the built-in functions.
EDIT, wanted to share here that Mobius provided code in the TS Lounge that allows for variable length regression line and variable length standard deviation calculations:

Code:
# Linear Regression Channel anchored to Highest / Lowest Pivot
# Allows a floating point anchor
# Mobius
# V01.2015

def hh = highestAll(high);
def ll = lowestAll(low);
script S
    {
     input d = close;
     input n = 50;
     plot e = fold i = 0 to n
              with f
              do f + getValue(d, i);
    }
def y = close;
def x  = BarNumber();
def nan = double.nan;
def xx = if(isNaN(y[-1]) and !isNaN(y), x, xx[1]);
def hx = if(high == hh, x, hx[1]);
def lx = if(low == ll, x, lx[1]);
def MinX = Max(hx, lx);
def n = highestAll(xx - MinX);
def x1 = HighestAll(if !IsNaN(y) and IsNaN(y[-1])
                    then x
                    else nan);
def x0 = HighestAll(if GetValue(x, -n) == x1
                    then x
                    else nan);
def x_ = if GetValue(x, -n) >= x1
         then x - x0
         else x_[1];
def Ex  = S(x_, n);
def Ey  = S(y, n);
def Exy = S(x_ * y, n);
def Exsq = S(sqr(x_), n);
def b = (n * Exy - (Ex * Ey)) / (n * Exsq - (Ex * Ex));
def a = (GetValue(Ey, x - x1) - GetValue(b, x - x1) * GetValue(Ex, x - x1)) / n;
def avg = (fold i = 0 to n
           with s
           do s + getValue(y, i)) / n;
def SD = Sqrt((fold i2 = 0 to n
               with s2 = 0
               do s2 + Sqr(avg - GetValue(y, i2))) / n);
plot LRL = if x >= x0
           then a + (GetValue(b, x - x1) * x_)
           else nan;
     LRL.SetStyle(Curve.Firm);
     LRL.SetDefaultColor(Color.Cyan);
plot upper = LRL + (highestAll(SD) * .7);
     upper.SetStyle(Curve.Firm);
     upper.SetDefaultColor(Color.Yellow);
plot lower = LRL - (highestAll(SD) * .7);
     lower.SetStyle(Curve.Firm);
     lower.SetDefaultColor(Color.Yellow);
# End Code Linear Regression Line

This provided me with the template I needed. The only thing that was also important to know was that the fold function can break scripts if your chart is displaying an expansion area, and this has to be prevented by instructing the code with fold (or code accessing the fold output value) with a preliminary condition such as “def {variable} = if isNan(close) then {variable}[1} else ….” or “then double.nan else…” depending on what is needed.
 
Last edited:
Solution
Thanks so much for your reply. I am a little confused about how this would be used for a condition trigger based on a signal that isn't directly related to highs, lows, or moving averages across the whole chart.

For example, suppose we want to anchor the linear regression channel to the last occurrence of a long signal (painted as an up arrow) from the triple exhaustion indicator (i.e. maybe that last occurrence was one bar ago, or maybe it was 500 bars ago). And suppose we just want the linear regression channel to use the high and low from within the last 10 bars of that anchor point.

If the condition variable for that up arrow was named "triple_exhaustion_long_arrow" then would you mind giving an example of how you would...
Guys, This is somewhat of a game changer for me. I have profited more in the last few days using this indicator. See chart where I bought and sold. Here is my grid if anyone is interested. https://tos.mx/psH8Wtm
1707496881721.png
1707496970375.png

1707497054082.png

1707497266972.png
 

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
296 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