I tried with the following code but without success ( it doesn'plot a straight line but a curve ):
#define N 50 // Number of bars
function run()
{
StartDate= 20060101;
EndDate = 20060601;
BarPeriod = 15;
// variables
var a,b,c,sumy=0.0,sumx=0.0,sumxy=0.0,sumx2=0.0,h=0.0,l=0.0;
int x,i;
int barsToCount=50;
vars myClose = series(priceClose());
for( i=0; i<N; i++)
{
sumy+=myClose[i];
sumxy+=myClose[i]*i;
sumx+=i;
sumx2+=i*i;
}
c=sumx2*N-sumx*sumx;
// Line equation
b=(sumxy*barsToCount-sumx*sumy)/c;
a=(sumy-sumx*b)/N;
var LR_line[N];
// Linear regression line in buffer
for(x=0;x<N;x++)
{ LR_line[x]=a+b*x;
plot("NEW_linreg", LR_line[x],MAIN, RED);
}
}
Setting LR_line as vars generate an error, thus I changed to var.
Last edited by Andrea66; 02/15/17 16:02.