Pete Hahn
Pete Hahn

Reputation: 21

Translating linreg() function from pinescript to thinkscript

I have seen numerous posts in this forum asking about this function in pinescript but so far none of them provide an answer to how the third parameter "offset" is used to modify the computation inside the function.

Here is the description of that function provided by TradingView:

https://www.tradingview.com/pine-script-reference/v4/#fun_linreg

I found that if you provide a value of zero for the offset parameter in pinescript the output is exactly the same as a function named "Inertia()" on Thinkorswim. TD Ameritrade provides a detailed example of how their function is computed. Listed here:

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Statistical/Inertia

Here is the code from that link:

script inertiaTS {
    input y = close;
    input n = 20;
    def x = x[1] + 1;
    def a = (n * Sum(x * y, n) - Sum(x, n) * Sum(y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    def b = (Sum(Sqr(x), n) * Sum(y, n) - Sum(x, n) * Sum(x * y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    plot InertiaTS = a * x + b;
}

input length = 20;
plot LinReg1 = Inertia(close, length);
plot LinReg2 = InertiaTS(close, length);

My question is this. How to modify the formula in that code for Thinkorswim so that it includes a parameter that works equivalent to the "offset" parameter in pinescript.

Upvotes: 1

Views: 389

Answers (0)

Related Questions