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:

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.

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: