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

Re: Fwd: Automatic Linear Regression Channels around ZigZags



PureBytes Links

Trading Reference Links

I got this to verify but when I applied it to the chart I got an error
message saying it had to many trend lines that 650 was the limit. Do you
need to make two indicators out of this so it will work properly?

Robert





>Seeing the code for ZigZag yesterday inspired me to merge it with the
>Linear Regression Channel code I posted a while ago. Here is the result.
>
>I wrote it to work in real-time. So when the zigzag changes direction,
>it draws a new channel. And when the zigzag extends itself, it updates
>the channel to include the new information.
>
>Plaintext and ela are attached.
>
>/Greg Wood
>
>{===================================================================}
>
>{ Indicator: LinearRegChn-ZigZag }
>
>Inputs: Strength(1), WavePcnt(.5), ShowZig(true);
>input:  price(c);
>var: colorTL(tool_darkgray), thickTL(0);
>var: colorZig(tool_darkblue), thickZig(0);
>
>{ ======================================
>
>I took some Zigzag code posted on the OmegaList and added some Linear
>Regression 
>Code I posted earlier to create a version that automatically draws
>linear  regression 
>channels around zigs and zags. 
>
>This indicator updates the channel whenever the zigzag changes.
>
>It works in real time.
>
>Gregory Wood
>SwissFranc@xxxxxxxxxxxxxxxx
>10/28/98
>
>NB: This indicator uses the pnt2line function, which is included at the
>bottom of this file.
>
>========================================}
>
>var: x1(0), x2(0), y1(0), y2(0), han1(-1), han2(-1), han3(-1);
>Vars: SH(0), SL(0), SHB(0), SLB(0), RP(0), RPDate(0),
>RPTime(0),ZigZag(0), Switch(0);
>
>{ zigzag part }
>
>IF CurrentBar = 1 Then Begin
>   RP = MedianPrice;
>   RPDate = Date;
>   RPTime = Time;
>End;
>
>if han2 > -1 then begin
>   value1 = TL_GetValue(han2,date,time);
>{   if switch = -1 then if l < value1 then plot1(value1,"");}
>end;
>if han3 > -1 then begin
>   value1 = TL_GetValue(han3,date,time);
>{   if switch = 1 then if h > value1 then plot2(value1,"");}
>end;
>
>SH = SwingHigh(1, High, Strength, Strength+1);
>SL = SwingLow(1, Low, Strength, Strength+1);
>
>var: zigmoved(false), newzig(false);
>IF SH <> -1 Then Begin
>   IF Switch <> -1 AND SH >= RP * (1+(WavePcnt/100)) Then Begin
>      Condition1 = True;
>      Switch = -1;
>      Condition2 = True;
>   End;
>   IF Condition1 = False AND Switch = -1 AND SH >= RP Then Begin
>      if ShowZig then TL_SetEnd(ZigZag, Date[Strength], Time[Strength],
>SH);
>      zigmoved = true;
>      Condition2 = True;
>   End;
>   IF Condition2 Then Begin
>      Condition2 = False;
>      RP = SH;
>      RPDate = Date[Strength];
>      RPTime = Time[Strength];
>   End;
>End;
>
>IF SL <> -1 Then Begin
>   IF Switch <> 1 AND SL <= RP / (1+(WavePcnt/100)) Then Begin
>      Condition1 = True;
>      Switch = 1;
>      Condition2 = True;
>   End;
>   IF Condition1 = False AND Switch = 1 AND SL <= RP Then Begin
>      if ShowZig then TL_SetEnd(ZigZag, Date[Strength], Time[Strength],
>SL);
>      Condition2 = True;
>      zigmoved = true;
>   End;
>   IF Condition2 Then Begin
>      Condition2 = False;
>         RP = SL;
>         RPDate = Date[Strength];
>         RPTime = Time[Strength];
>   End;
>End;
>
>IF Condition1 Then Begin
>   Condition1 = False;
>   if ShowZig then begin
>      ZigZag = TL_New(RPDate, RPTime, RP, RPDate[1], RPTime[1], RP[1]);
>      TL_SetColor(ZigZag, colorZig);
>      TL_SetSize(ZigZag, thickZig);
>      TL_SetExtRight(ZigZag, False);
>      TL_SetExtLeft(ZigZag, False);
>   end;
>   IF False Then Plot1[Strength](RP, "");
>   newzig = true;
>End;
>
>{ Linear Regression Channel part }
>
>if newzig then begin
>   if han1 > -1 then begin
>      TL_SetEnd(han1,date,time,TL_GetValue(han1,date,time));
>      TL_SetExtRight(han1, false);
>   end;
>   if han2 > -1 then begin
>      TL_SetEnd(han2,date,time,TL_GetValue(han2,date,time));
>      TL_SetExtRight(han2, false);
>   end;
>   if han3 > -1 then begin
>      TL_SetEnd(han3,date,time,TL_GetValue(han3,date,time));
>      TL_SetExtRight(han3, false);
>   end;
>   newzig = false;
>   x2 = x1;
>   han1 = -1; 
>   han2 = -1;
>   han3 = -1;
>   if false then plot1(c,"");
>        zigmoved = true;
>end;
>if zigmoved then begin
>   zigmoved = false;
>   x1 = currentbar;
>   y1 = LinearRegValue(price, x1-x2 ,0);
>   y2 = LinearRegValue(price, x1-x2, x1-x2);
>   
>   { Draw the regression line }
>   x1 = currentbar;
>   y1 = LinearRegValue(price, x1-x2 ,0);
>   y2 = LinearRegValue(price, x1-x2, x1-x2);
>   if han1 = -1 then begin
>      han1 = TL_New(date[x1-x2],time[x1-x2],y2, date,time,y1 );
>      TL_SetExtRight(han1, true);
>      TL_SetExtLeft(han1, false);
>      TL_SetSize(han1, thickTL);
>      TL_SetColor(han1, colorTL);
>   end else begin
>      TL_SetBegin(han1,date[x1-x2],time[x1-x2],y2);
>      TL_SetEnd(han1,date,time,y1);
>   end;
>
>   var: kk(0), hval(0), lval(0), hh(0), ll(0), hhbb(0), llbb(0),
>pvv1(0), pvv2(0);
>   { find the max excursion on each side of the line }
>             hval = 0;
>             lval = 0;
>             for kk = x2 to x1 begin
>             value1 = pnt2line(x1,y1,x2,y2, barnumber[currentbar-kk],
>high[currentbar-kk]);
>             if value1 > 0 and hval < value1 then begin
>                hval = value1;
>                hh = h[currentbar-kk];
>                hhbb = kk;
>             end;
>             value2 = pnt2line(x1,y1,x2,y2, barnumber[currentbar-kk],
>low[currentbar-kk]);
>             if value2 < 0 and lval > value2 then begin
>                lval = value2;
>                ll = l[currentbar-kk];
>                llbb = kk;
>             end;
>         end;
>
>         { Now draw the channel lines }
>         if false then plot2[currentbar-hhbb](hh,"");
>         pvv1 = TLValue(ll,llbb, ll-(y2-y1), llbb-(x2-x1), x1);
>         pvv2 = TLValue(ll,llbb, ll-(y2-y1), llbb-(x2-x1), x2);
>         if han2 = -1 then begin
>              han2 = TL_New(date[x1-x2],time[x1-x2],pvv2, date,
>time,pvv1);
>              TL_SetExtRight(han2, true);
>              TL_SetExtLeft(han2, false);
>              TL_SetSize(han2, thickTL);
>              TL_SetColor(han2, colorTL);
>         end else begin
>              TL_SetBegin(han2,date[x1-x2],time[x1-x2],pvv2);
>              TL_SetEnd(han2,date,time,pvv1);
>         end;
>
>         if false then plot3[currentbar-llbb](ll,"");
>         pvv1 = TLValue(hh,hhbb, hh-(y2-y1), hhbb-(x2-x1), x1);
>         pvv2 = TLValue(hh,hhbb, hh-(y2-y1), hhbb-(x2-x1), x2);
>         if han3 = -1 then begin
>              han3 = TL_New(date[x1-x2],time[x1-x2],pvv2, date,
>time,pvv1);
>              TL_SetExtRight(han3, true);
>              TL_SetExtLeft(han3, false);
>              TL_SetSize(han3, thickTL);
>              TL_SetColor(han3, colorTL);
>         end else begin
>              TL_SetBegin(han3,date[x1-x2],time[x1-x2],pvv2);
>              TL_SetEnd(han3,date,time,pvv1);
>         end;
>end;
>
>if false then plot1(0,"");
>if false then plot2(0,"");
>if false then plot3(0,"");
>if false then plot4(0,"");
>
>{
>
>{ FUNCTION: PNT2LINE }
>
>input: x1(numericsimple), y1(numericsimple), 
>          x2(numericsimple), y2(numericsimple),
>          x(numericsimple),   y(numericsimple);
>
>{ returns the distance from (x,y) to the line formed by (x1,y1) (x2, y2)
>}
>
>var: qq(0), m1(0), m2(0);
>
>if x-x2 <> 0 then begin
>        m1 = (y1-y2) / (x1-x2);
>        m2 = (y-y2) / (x-x2);
>        qq = SquareRoot(Square(y-y2)+Square(x-x2));
>        value1 = qq * sine( arctangent( (m2-m1) / (1 + m1*m2)));
>end else
>        value1 = 0;
>
>pnt2line = value1;
>}
>
>