Struggling with v2 to v4 pinescript Conversion, "Cannot modify global variable inside function" Error And I'm not really sure how to access the variable and modify inside the function, to complete the loop, and its not showing up on the chart.
I have tried p = 0 followed by p := "normal function text" inside the function, got that to compile, but had the indicator dissapear from the chart. V4 pine is clearly affecting the math calculation via bad syntax.
https://www.tradingview.com/script/9P5AUAMl-Adaptive-Least-Squares/
//@version=2
study("Adaptive Least Squares",overlay=true)
length = input(500),smooth = input(1.5)
//
alpha = pow(tr/highest(tr,length),smooth)
m(a) =>
p = alpha * a + (1-alpha) * nz(p[1],a)
//
x = n
y = close
x_ = m(x)
y_ = m(y)
//
dx = abs(x-x_)
dy = abs(y-y_)
mx = m(dx)
my = m(dy)
//
a1 = pow(2/alpha+1,2)*m(x*y) - ((2/alpha+1)*m(x))*((2/alpha+1)*m(y))
b1 = sqrt((pow(2/alpha+1,2)*m(x*x) - pow((2/alpha+1)*m(x),2)) * (pow(2/alpha+1,2)*m(y*y) - pow((2/alpha+1)*m(y),2)))
r = a1/b1
//
a = r * (my/mx)
b = y_ - a*x_
reg = x*a + b
//
plot(reg,color=red,transp=0)
p
ends. – Michel_T.