Slope of a linear regression

Hello. How to calculate the slope in degrees of a linear regression? What am I doing wrong?

Plot( C, "Price", colorDefault,styleBar|styleThick );

period = 5;

Plot( LinearReg( C, period ), "LinearReg", colorBlue );

bi = BarIndex();
sbi = SelectedValue( bi );

b = SelectedValue( LinRegIntercept( C, period ) );
a = SelectedValue( LinRegSlope( C, period ) );

x = bi - sbi;

ll = a * ( x + period - 1 ) + b;

ll = IIf( x >= - period AND x <= 0, ll, Null );

Plot( ll, "Line", colorRed, styleThick );

bb = LinRegSlope(C, period );
pi = 4 * atan(1);
slopeangle = atan( bb)*( 180 / pi );
printf("Slope");
WriteVal(slopeangle);

@kam57, welcome to this community.

Your calculation is correct (essentially the same as the formula posted here by @Tomasz).

To see the "correct" (approximate) slope of the line on the chart you need to shrink/stretch the bars on the chart as needed, since the chart ratio is not necessarily 1:1 - see this detailed answer by @Tomasz about anisotropic charts.

2 Likes