[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Help - Trying to plot linear regression lines



PureBytes Links

Trading Reference Links

Try something like this. For the sake of the example, the signals are
based on 30 & 60 day EMA crossovers.

**************************
GraphXSpace = 10;
Plot(Close, "Price", -1, styleCandle);

period = 4;

e30 = EMA(Close, 30);
e60 = EMA(Close, 60);
Buy = Cross(e30, e60);
Sell = Cross(e60, e30);
Short = Sell;
Cover = Buy;

slope = LinRegSlope(Close, period);
intercept = LinRegIntercept(Close, period);
y2 = intercept + slope * (period-1); // 

fb = Max(Status("firstVisibleBar"), period-1);
lb = Status("lastVisibleBar");

RegLineL = Null;
RegLineS = Null;
for (i = fb; i <= lb; i++) {
    if (Buy[i]) {
        rlines = LineArray(i-period+1, intercept[i], i, y2[i]);
        RegLineL = IIf(!IsNull(rlines), rlines, RegLineL);
        }
    if (Short[i]) {
        rlines = LineArray(i-period+1, intercept[i], i, y2[i]);
        RegLineS = IIf(!IsNull(rlines), rlines, RegLineS);
        }
    }

Plot(RegLineL, "RegLineL", colorBrown, styleThick | styleDashed);
Plot(RegLineS, "RegLineS", colorBlue, styleThick | styleDashed);

Plot(e30, "EMA(30)", colorOrange, styleLine);
Plot(e60, "EMA(60)", colorBlack, styleLine);
**************************

I used separate arrays for the long and short lines, so that they can
be displayed in different colours. Also note the use of "period-1" for
the 'x' distance. When regressing over 4 bars, the distance between
them is only 3 bars (so delta 'x' is 3, not 4).

There seems to be some confusion with the intercept. The intercept is
not for absolute index zero, but for the first bar of the regression
range. So when regressing over 4 bars, the intercept is for the bar 3
bars ago. Consequently, Y2 is not intercept+slope*X2, but rather
intercept+slope*(period-1), and Y1 is just the intercept.

So the four coordinates of the line at any bar 'i' are:

X1 = i-period+1; (ie. 3 bars ago if period is 4)
X2 = i;
Y1 = intercept[i]; (the y value 3 bars ago)
Y2 = Y1+slope[i]*(period-1);

The value of Y2 is just Y1+slope*deltaX, and rearranging would give
slope = (Y2-Y1)/deltaX = deltaY/deltaX as required.

Regards,
GP


--- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@xxx> 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 amibroker@xxxxxxxxxxxxxxx, "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);
> >
> >   }
> >
>



------------------------------------

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com
*********************

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html

*********************************
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/