I've been trying to plot linear regression lines, with no luck. Hoping
someone can point out my error.
When I am determining to enter a trade, my trading formula tests against
the slope of the linear regression line over the last 4 Closes (in
addition to other tests). For visual tracking purposes, I want to plot
these linear regression lines.
Part of my trading formula resides in a BarCount loop, and inside this
loop I successfully populate arrays, TestSignalLong and TestSignalShort.
When these arrays are true, I want to plot the linear regression lines.
Here is what I tried coding but it does not properly plot the lines. I
get weird results:
X2 = Cum(1); //
2nd X-coordinate in Cartesian line formula: Y = M*X + B
LinePeriod = 4; //
look-back period for linear regression line
X1 = LastValue(X2) - LinePeriod; //
1st X-coordinate in Cartesian line formula: Y = M*X + B
Slope = LastValue( LinRegSlope(C, LinePeriod) ); //
this is the 'M' in Cartesian line formula: Y = M*X + B
Intercept = LastValue( LinRegIntercept(C, LinePeriod) ); // this
is the 'B' in Cartesian line formula: Y = M*X + B
Y2 = Slope * X2 + Intercept; //
2nd Y-coordinate in Cartesian line formula: Y = M*X + B
Y1 = Slope * X1 + Intercept; //
1st Y-coordinate in Cartesian line formula: Y = M*X + B
// Barcount Loop
for (i = LinePeriod; i < BarCount; i++)
{
//----------------------------------------------------------------------\
------------------------------------------------------------------------
// MY TRADING FORMULA GOES HERE. DURING THE FORMULA, ARRAYS ARE FILLED
FOR:
// TestSignalLong[i] AND TestSignalShort[i], for use in the Indicator
below
//----------------------------------------------------------------------\
------------------------------------------------------------------------\
-
}
if (Status("action") == 1 /* Indicator */)
{
// Plot Linear Regression Lines for each TestSignal
if (ParamToggle("Show slope signals", "No|Yes", True))
{
firstVisibleBar = Status("firstVisibleBar");
lastVisibleBar = Status("lastVisibleBar");
for (b = firstVisibleBar; b < BarCount AND b <= lastVisibleBar; b++)
{
if (TestSignalLong[b])
{
RegLine = LineArray( X1[b], Y1[b], X2[b], Y2[b] );
Plot(RegLine, "RegLine", colorBrown, styleThick | styleDashed);
}
if (TestSignalShort[b])
{
RegLine = LineArray( X1[b], Y1[b], X2[b], Y2[b] );
Plot(RegLine, "RegLine", colorTurquoise, styleThick |
styleDashed);
}
}
}
Plot(C, "Close", colorGreen, styleThick);
}