Syntax
public LinearReg(DataSeries ds, int period, string description)
public static LinearReg Series(DataSeries ds, int period)
public static double Value(int bar, DataSeries ds, int period)
Parameter Description
ds |
Price series |
period |
Indicator calculation period |
Description
Returns the Linear Regression value for the specified DataSeries
ds and
period. LinearReg gathers the prices for the number of specified periods and finds a straight line which best fits all the prices using a linear regression model. The procedure is reinitialized and repeated for each bar in the Price Series. Since a new value is calculated at each Bar, the result is not a straight linear regression trendline; rather, it is an indicator which loosely tracks the price action.
LinearReg is a statistical indicator. Other indicators in the same class are
LinearRegSlope,
StdError,
RSquared and
StdDev.
Interpretation
- Since the Linear Regression indicator displays the statistically-predicted price value, you can look for cases where price veers sharply from the predicted value. Use RSquared to determine significant weakness in the trend and if due for a return to the predicted value.
Calculation
Linear Regression is a somewhat complex statistical calculation. It uses the least square method to fit a trendline to the data by minimizing the distance between the price and the Linear Regression trendline. LinearReg returns the final value of the LinearRegLine, recalculated for each bar over the regression Period to complete indicator.
Example
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
protected override void Execute()
{
// Report on how far closing prices are away from predicted value
string s;
int bar = Bars.Count-1;
double close = Close[bar];
double Diff = close - LinearReg.Series( Close, 20 )[bar];
Diff = Diff/close*100;
if( Diff > 0 )
s = "Price closed above "; else
s = "Price closed below ";
Diff = Math.Abs( Diff );
s = s + "the Regression Line by " + Bars.FormatValue( Diff ) + "%";
DrawLabel( PricePane, s );
PlotSeries( PricePane, LinearReg.Series( Close, 20 ), Color.Black, WealthLab.LineStyle.Solid, 1 );
}
}
}