Looks like my math was wrong. If you want to avoid the looping of GP's reply, you could probably take his math lessons and modify the arrays of my initial reply. Then again, unless you're experiencing performance problems, the looping may be easier to understand anyway.
Mike --- In [email protected], "Mike" <[EMAIL PROTECTED]> wrote: > > Hi, > > Note that LastValue always looks at the end of the array. That's not > what you want since you are wanting the regression at each signal, > not only at the last bar in the series. That's one reason why your > first attempt failed. > > Also, note that LinRegSlope and LinRegIntercept take arrays as their > first argument, not a single element (i.e. not C[b] as in your > example). That's one reason why your second attempt failed. > > Try something along the following, which uses the regression values > of the current bar (when current bar is a signal bar), or the bar of > the next signal, as the case may be. > > You will need to do the math to double check it all. But, the > important thing to note is that you must dynamically select the bar > for your regression values as each signal occurs, not just using the > values of the last bar in the data (i.e. not LastValue(...)). > > LookBack = 4; > Slope = LinRegSlope(Close, LookBack); > Intercept = LinRegIntercept(Close, LookBack); > > TestSignalLong = Cross(MA(Close, 5), MA(Close, 25)); > NextSignalBar = ValueWhen(TestSignalLong, BarIndex(), 0); > > M = IIF(TestSignalLong, Slope, ValueWhen(TestSignalLong, Slope, 0)); > X = IIF(TestSignalLong, LookBack, LookBack - (NextSignalBar - BarIndex > ())); > B = IIF(TestSignalLong, Intercept, ValueWhen(TestSignalLong, > Intercept, 0)); > > Y = M * X + B; > > Plot(Close, "Close", colorLightGrey, styleBar); > PlotShapes(IIF(TestSignalLong, shapeUpArrow, null), colorGreen, 0, > Close); > Plot(IIF(X > 0 && X <= LookBack, Y, null), "LinReg", colorRed); > > Mike > > --- In [email protected], "ozzyapeman" <zoopfree@> wrote: > > > > I also tried putting everything inside the BarCount loop but now I > get > > an error with LineArray (Error 5 - the function expects a different > > argument type here): > > > > > > // Plot the Linear Regression Lines > > if (ParamToggle("Show slope signals", "No|Yes", True)) > > { > > > > for (b = LinePeriod; b < BarCount; b++) > > { > > > > if (slopeSignalLong[b]) > > { > > X2 = b; > > X1 = b - LinePeriod; > > Intercept = LinRegIntercept(C[b], LinePeriod); > > Slope = LinRegSlope(C[b], LinePeriod); > > Y2 = Slope * X2 + Intercept; > > Y1 = Slope * X1 + Intercept; > > RegLine = LineArray( X1, Y1, X2, Y2,0 ); > > Plot(RegLine, "RegLine", colorBrown, styleThick | > styleDashed); > > } > > > > if (slopeSignalShort[b]) > > { > > X2 = b; > > X1 = b - LinePeriod; > > Intercept = LinRegIntercept(C[b], LinePeriod); > > Slope = LinRegSlope(C[b], LinePeriod); > > Y2 = Slope * X2 + Intercept; > > Y1 = Slope * X1 + Intercept; > > RegLine = LineArray( X1, Y1, X2, Y2,0 ); > > Plot(RegLine, "RegLine", colorTurquoise, styleThick | > > styleDashed); > > } > > } > > } > > > > > > > > > > --- In [email protected], "ozzyapeman" <zoopfree@> wrote: > > > > > > 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); > > > > > > } > > > > > >
