Zig zag retracements like Ensign software ones

#1
Hello guys,
I'd like to have zigzag retracements like this:

http://charts.dacharts.com/2008-10-12/Johnny_Chart78.png


I found this code somewhere:

Code:
_SECTION_BEGIN("ZigZag Retracement");
function GetXSupport(Lo, Percentage, Back)
{
 return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
}
function GetYSupport(Lo, Percentage, Back)
{
 return (LastValue(Trough(Lo, Percentage, back)));
}

function GetXResistance(Hi, Percentage, Back)
{
 return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
}
function GetYResistance(Hi, Percentage, Back)
{
 return (LastValue(Peak(Hi, Percentage, Back)));
}

//////////////////////////////////////////////////////////////////
Per = Param("Period", 5.9618, .1, 20, .001);
Period = Param("Look back", 10, 1, BarCount-1);
ShowRet = ParamToggle("Show Retracement values", "No|Yes",1);
Price = ParamList("Price to follow:", "Close|High|Low", 0);
if(Price=="Close") ZigP = Zig(C, per);
else if(Price=="High") ZigP = Zig(H, per);
else ZigP = Zig(L, per);

//////////////////////////////////////////////////////////////////
Plot(C, "", IIf(O>=C, colorDarkRed, colorDarkGreen), ParamStyle("Price
Style",styleBar,maskPrice));
Plot(ZigP, "Zig", colorGold, styleThick);
//////////////////////////////////////////////////////////////////

xs1 = GetXSupport(ZigP, .01, 1);
xr1 = GetXResistance(ZigP, .01, 1);
ys1 = GetYSupport(ZigP, .01, 1);
yr1 = GetYResistance(ZigP, .01, 1);

if(xs1 < xr1)
{
	x = LineArray(xs1, ys1, BarCount - 1, LastValue(ZigP));
	Down = (yr1 - LastValue(ZigP)) / (yr1 - ys1);
	DnBars = BarCount - 1 - xr1;
	Plot(x, "", colorRed, styleDots);
	PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + BarCount -1)/2,
(ys1+LastValue(ZigP))/2, colorWhite);
}
else
{
	x = LineArray(xr1, yr1, BarCount - 1, LastValue(ZigP));
	Up = (LastValue(ZigP) - ys1) / (yr1 - ys1);
	UpBars = BarCount - 1 - xs1;
	Plot(x, "", colorRed, styleDots);
	PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + BarCount -1)/2,
(yr1+LastValue(ZigP))/2, colorWhite);
}
Plot( 1, "", IIf( xs1 > xr1, colorGreen,
colorRed),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
if(ShowRet)
for(i=2; i<=Period+1; i++)
{
	xs0 = GetXSupport(ZigP, .01, i);
	xs1 = GetXSupport(ZigP, .01, i-1);
	ys0 = GetYSupport(ZigP, .01, i);
	ys1 = GetYSupport(ZigP, .01, i-1);

	xr0 = GetXResistance(ZigP, .01, i);
	xr1 = GetXResistance(ZigP, .01, i-1);
	yr0 = GetYResistance(ZigP, .01, i);
	yr1 = GetYResistance(ZigP, .01, i-1);

	xs = LineArray(xs0, ys0, xs1, ys1, 0);
	Plot(xs, "", colorGold, styleLine|styleDashed);
	xr = LineArray(xr0, yr0, xr1, yr1, 0);
	Plot(xr, "",  colorGold, styleLine|styleDashed);
	if(xs1 < xr1)
	{
		Up = (yr1 - ys1) / (yr0 - ys1);
		Down = (yr0 - ys1) / (yr0 - ys0);
		UpBars = xr1 - xs1;
		DnBars = xs1 - xr0;
	}
	else
	{
		Up = (yr1 - ys0) / (yr0 - ys0);
		Down = (yr1 - ys1) / (yr1 - ys0);
		UpBars = xr1 - xs0;
		DnBars = xs1 - xr1;
	}
	PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + xr0)/2, (yr1+yr0)/2,
colorWhite);
	PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + xs0)/2, (ys1+ys0)/2,
colorWhite);
	//Plot(LineArray(xs0, ys0, BarCount-1, ys0), "", colorGreen, styleDashed);
	//Plot(LineArray(xr0, yr0, BarCount-1, yr0), "", colorRed, styleDashed);

}

str = StrFormat("   (Bars to END=%.0f)\n", BarCount - 1 - BarIndex());
Title =FullName()+" ("+Name()+") - "+Date()+" - Open: "+O+", Hi: "+H+", Lo:
"+L+", Close: "+C+StrFormat(" (%.2f  %.2f%%)", C-Ref(C, -1),
SelectedValue(ROC(C, 1)))+str;
WriteIf(1, "\nNote Fibonacci numbers:\nPrimary numbers: 0.618, 0.786, 1.27 and
1.618","");
WriteIf(1, "Secondary numbers: 0.382, 0.50, 1.00, 2.00, 2.24, 2.618 and
3.14","");


_SECTION_END();
Unfortunately it isn't very effective because it doesn't show all of the retracements. Can anyone pls make it better?

Thank you in advance for help.
Regards,
Davide
 
#2
Hello guys,
I made some breakthroughs but it still needs work to be good.

Code:
_SECTION_BEGIN("ZigZag Retracement");
function GetXSupport(Lo, Percentage, Back)
{
 return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
}
function GetYSupport(Lo, Percentage, Back)
{
 return (LastValue(Trough(Lo, Percentage, back)));
}

function GetXResistance(Hi, Percentage, Back)
{
 return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
}
function GetYResistance(Hi, Percentage, Back)
{
 return (LastValue(Peak(Hi, Percentage, Back)));
}

//////////////////////////////////////////////////////////////////
Per = Param("Period", 5, .1, 20, .001);
Period = Param("Look back", 10, 1, BarCount-1);
ShowRet = ParamToggle("Show Retracement values", "No|Yes",1);
Price = ParamList("Price to follow:", "Close|High-Low", 0);
if(Price=="Close") ZigP = Zig(C, per);
else if(Price=="High-Low") 

{
pk=PeakBars(H,Per)==0;
tr=TroughBars(L,Per)==0;

zzHi=Zig(H,Per);
zzLo=Zig(L,Per);
Avg=(zzHi+zzLo)/2;

x=IIf(pk,zzHi,IIf(tr,zzLo,IIf(Avg>Ref(Avg,-1),H,L)));
ZigP=Zig(x,Per);
}
//////////////////////////////////////////////////////////////////
Plot(C, "", IIf(O>=C, colorDarkRed, colorDarkGreen), ParamStyle("Price
Style",styleBar,maskPrice));
Plot(ZigP, "Zig", colorBlack, styleThick);
//////////////////////////////////////////////////////////////////

xs1 = GetXSupport(ZigP, .01, 1);
xr1 = GetXResistance(ZigP, .01, 1);
ys1 = GetYSupport(ZigP, .01, 1);
yr1 = GetYResistance(ZigP, .01, 1);

if(xs1 < xr1)
{
	x = LineArray(xs1, ys1, BarCount - 1, LastValue(ZigP));
	Down = (yr1 - LastValue(ZigP)) / (yr1 - ys1);
	DnBars = BarCount - 1 - xr1;
	Plot(x, "", colorRed, styleDots);
	PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + BarCount -1)/2,
(ys1+LastValue(ZigP))/2, colorDarkGrey);
}
else
{
	x = LineArray(xr1, yr1, BarCount - 1, LastValue(ZigP));
	Up = (LastValue(ZigP) - ys1) / (yr1 - ys1);
	UpBars = BarCount - 1 - xs1;
	Plot(x, "", colorRed, styleDots);
	PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + BarCount -1)/2,
(yr1+LastValue(ZigP))/2, colorDarkGrey);
}
Plot( 1, "", IIf( xs1 > xr1, colorGreen,
colorRed),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

if(ShowRet)
for(i=2; i<=Period+1; i++)
{
	xs0 = GetXSupport(ZigP, .01, i);
	xs1 = GetXSupport(ZigP, .01, i-1);
	ys0 = GetYSupport(ZigP, .01, i);
	ys1 = GetYSupport(ZigP, .01, i-1);

	xr0 = GetXResistance(ZigP, .01, i);
	xr1 = GetXResistance(ZigP, .01, i-1);
	yr0 = GetYResistance(ZigP, .01, i);
	yr1 = GetYResistance(ZigP, .01, i-1);

	xs = LineArray(xs0, ys0, xs1, ys1, 0);
	Plot(xs, "", colorBlack, styleLine|styleDashed);
	xr = LineArray(xr0, yr0, xr1, yr1, 0);
	Plot(xr, "",  colorBlack, styleLine|styleDashed);





/*	xs2 = GetXSupport(ZigP, .01, i-2);
	ys2 = GetYSupport(ZigP, .01, i-2);
	xr2 = GetXResistance(ZigP, .01, i-2);
	yr2 = GetYResistance(ZigP, .01, i-2);

	xs = LineArray(xs0, ys0, xs2, ys2, 0);
	Plot(xs, "", colorBlack, styleLine|styleDashed);
	xr = LineArray(xr0, yr0, xr2, yr2, 0);
	Plot(xr, "", colorBlack, styleLine|styleDashed);
*/

/*	xs3 = GetXSupport(ZigP, .01, i-3);
	ys3 = GetYSupport(ZigP, .01, i-3);
	xr3 = GetXResistance(ZigP, .01, i-3);
	yr3 = GetYResistance(ZigP, .01, i-3);

	xs = LineArray(xs0, ys0, xs3, ys3, 0);
	Plot(xs, "", colorBlack, styleLine|styleDashed);
	xr = LineArray(xr0, yr0, xr3, yr3, 0);
	Plot(xr, "", colorBlack, styleLine|styleDashed);

*/


	if(xs1 < xr1)
	{
		Up = (yr1 - ys1) / (yr0 - ys1);
		Down = (yr0 - ys1) / (yr0 - ys0);
		UpBars = xr1 - xs1;
		DnBars = xs1 - xr0;
	}
	else
	{
		Up = (yr1 - ys0) / (yr0 - ys0);
		Down = (yr1 - ys1) / (yr1 - ys0);
		UpBars = xr1 - xs0;
		DnBars = xs1 - xr1;
	}
	PlotText(StrFormat("%.3f (%.0f)", Up, UpBars), (xr1 + xr0)/2, (yr1+yr0)/2,
colorDarkGrey);
	PlotText(StrFormat("%.3f (%.0f)", Down, DnBars), (xs1 + xs0)/2, (ys1+ys0)/2,
colorDarkGrey);
	//Plot(LineArray(xs0, ys0, BarCount-1, ys0), "", colorGreen, styleDashed);
	//Plot(LineArray(xr0, yr0, BarCount-1, yr0), "", colorRed, styleDashed);

}

str = StrFormat("   (Bars to END=%.0f)\n", BarCount - 1 - BarIndex());
Title =FullName()+" ("+Name()+") - "+Date()+" - Open: "+O+", Hi: "+H+", Lo:
"+L+", Close: "+C+StrFormat(" (%.2f  %.2f%%)", C-Ref(C, -1),
SelectedValue(ROC(C, 1)))+str;
WriteIf(1, "\nNote Fibonacci numbers:\nPrimary numbers: 0.618, 0.786, 1.27 and
1.618","");
WriteIf(1, "Secondary numbers: 0.382, 0.50, 1.00, 2.00, 2.24, 2.618 and
3.14","");


_SECTION_END();

This code originally didn't even plot high low zig-zags it just connected highs to highs and lows to lows, that was useless.

Now there's one more problem.
The code doesn't plot all of the retracements but just some.
I'd like to have all of the retracements plotted.
So I looked into the code and I started to play with the variable counter "i" and this is what I came with:

Code:
/*	xs2 = GetXSupport(ZigP, .01, i-2);
	ys2 = GetYSupport(ZigP, .01, i-2);
	xr2 = GetXResistance(ZigP, .01, i-2);
	yr2 = GetYResistance(ZigP, .01, i-2);

	xs = LineArray(xs0, ys0, xs2, ys2, 0);
	Plot(xs, "", colorBlack, styleLine|styleDashed);
	xr = LineArray(xr0, yr0, xr2, yr2, 0);
	Plot(xr, "", colorBlack, styleLine|styleDashed);
*/
It works but now there's one more problem it connects all of the highs/lows with other high/lows as long as they are elements of their own arrays.
Of course we do not want this behavior.
If for example we have 3 highs (A,B,C) and we want to connect A to C, B doesn't have to break the trendline, if it does so then the retracement that connects A to C must not be calculated.

So this piece of code comes to rescue us:

Code:
_SECTION_BEGIN("Trend and Pattern sul prezzo");
/*Trend & Pattern Detection log V.1.6 
Date: 31-Okt.-2002 
written by Thomas Zmuck 
[email protected] 

//for best use, take 4 sheets and copy the formula in every sheet, at next change the value for per to 2, 5, 10, and 20 

/*Adjustment-Part*/ 
Per=Param("Periodo",5,2,30,1); 
V1 = 1.05; //V1 for reentry-before 
V2 = 1.01; //V2 for reentry after 

//a Low must be lower than V1(V2)*any Line to give a Reentry Signal 
Color_SL= colorRed;Color_PHL=colorRed; 
Color_RL= colorBlue;Color_PLL=colorBlue; 
DisRange = 15; //in % 

LL=L;
HH=H;
CC=C;

//select here the Range from Close to PLL & Close to PHL so that the PLL Line is displayed. 
x = Cum(1); s1=LL;s11=HH; 

//Support Line (SL) 
PS = TroughBars(s1,per,1) == 0; 
xb = LastValue(ValueWhen(PS,x,1)); 
xa = LastValue(ValueWhen(PS,x,2)); 
yb = LastValue(ValueWhen(PS,s1,1)); 
Ya = LastValue(ValueWhen(PS,s1,2)); 
xab_log = log(yb/yA)/(xb-xa); 
SL = exp((x-xb)*xab_log)*yb; 
RocSL = ROC(SL,1); 

//RL = Resistance Line 
PR1 = PeakBars(s11,per,1) == 0; 
Xd = LastValue(ValueWhen(pR1,x,1 )); 
xc = LastValue(ValueWhen(pR1,x,2 )); 
yd = LastValue(ValueWhen(pR1,s11,1)); 
Yc = LastValue(ValueWhen(pR1,s11,2)); 
xcd_log = log(yd/yc)/(xd-xc); 
RL = exp((x-xd)*xcd_log)*yd; 
RocRL = ROC(RL,1); 

//Lower Parallel line - calculated by the Lowest Low below the Res.Line 
Low_Value = LastValue(Ref(LLV(LL,xd-xc),-(x-xd))); 
x2 = LastValue(ValueWhen(LL==Low_Value & x>xc & x<xd,x)); 
PLL = IIf(LastValue(x)-x2>5,exp((x-x2)*xcd_log)*Low_Value,-1e10); 

//Higher Parallel line - calculated by the Highest High above the Supp.Line 
Hi_Value = LastValue(Ref(HHV(HH,xb-xa),-(x-xb))); 
x3 = LastValue(ValueWhen(HH==Hi_Value & x>xa & x<xb,x)); 
PHL = IIf(LastValue(x)-x3>5,exp((x-x3)*xab_log)*Hi_Value,-1e10); 

//Channels 
parallel= abs(1-RocRL/RocSL)<0.1; 
uptrend= (RocSL>0 & RocRL>0 & parallel) OR (LL<1.05*PLL & HH>0.95*PLL); 
downtrend=(RocSL<0 & RocRL<0 & parallel)OR (HH<1.05*PHL & HH>0.95*PHL); 
DT_UT = IIf (Downtrend & parallel,-1,IIf(Uptrend & parallel,1,0)); 

/******************************/ 
//Breakouts 
bu_Breakout = Cross(CC,RL) & X>XD; 
be_Breakout = Cross(SL,CC) & X>XB; 
breakout = bu_breakout OR be_breakout; 

//Reentry's 
RSL_b = LL<V1*SL & LL>V2*SL & Ref(LL,-1)>V2*SL; 

//SL-before 
RSL_a = LL<=V2*SL & LL>0.99*SL & Ref(LL,-1)>SL & CC>SL; 

//SL-after 
RPLL_b = LL>Low_Value & LL<V1*PLL & LL>V2*PLL & Ref(LL,-1)>V2*PLL;//PLL-before 
RPLL_a = LL>Low_Value & LL<V2*PLL & LL>0.99*PLL & Ref(LL,-1)>PLL & C>PLL;//PLL-after 
ARb = RSL_b OR RPLL_b; //all reentrys before 
ARa = RSL_a OR RPLL_a; //all reentrys after 
Ar = ARb OR ARa; //all reentrys 
Xm = xb-xa >4 & xd-xc >4; //x-min.distance 
AT= abs(RocRL)<0.03 & RocSL>0.05 & xa<xd & xc<xb & Xm; 
DT= abs(RocSL)<0.03 & RocRL<-0.05 & xa<xd & xc<xb & Xm; 
AT_DT= IIf(AT,1,IIf(DT,-1,0)); 
ST= RocSL>0.03 & RocRL<-0.03 & RL>SL & 
xa<xd & xc<xb & Xm; 
ET = RocRL>0.03 & RocSL<-0.03 & RL>SL & abs(ROCSL/ROCRL)<3 & xa<xd & xc<xb & Xm; 
ST_ET = IIf(ST,1,IIf(ET,-1,0)); 
RW = RocRL>0.05 & RocSL>0.07 & RocSL>1.5*RocRL & Xm; 
FW = RocSL<-0.05 & RocRL<-0.07 & RocRL<1.5*RocSL & Xm; 
RW_FW = IIf (RW,1,IIf(FW,-1,0)); 

//////////////////////////////////////////7 
//Sell-Conditions 
S_RL = HH>0.98*RL & CC < RL; 
S_PHL = HH>0.98*PHL & CC < PHL; 

//Buy- & Sell signals for colored C&lesticks 
BS = bu_breakout OR Ar; 
SS = be_breakout OR S_RL OR S_PHL; 
Buy = BS; Sell = SS; 
Longfilter = CC>MA(CC,100); 

Filter = Longfilter; 

Reentry_Column = IIf(ARb,-1,IIf(ARa,1,0)); 
AddColumn(Reentry_Column,"Re",1.0); 
bu_be_breakout = IIf (bu_breakout,1,IIf(be_breakout,-1,0)); 
AddColumn(bu_be_breakout,"B-out",1.0); 
AddColumn(DT_UT,"TC",1); 
AddColumn(AT_DT,"AT/DT",1); 
AddColumn(ST_ET,"ST/ET",1); 
AddColumn(RW_FW,"RW/FW",1); 
AddColumn (IIf(CC>SL,RocSL,0),"Roc(SL,1)",1.1); 
AddColumn (IIf(CC<RL,RocRL,0),"Roc(RL,1)",1.1); 

//Display Cond. for Fix when Chart is compl.zoomed out 
PLLd = abs((LastValue(CC)/LastValue(PLL))-1)<0.01*DisRange; 
PHLd = abs((LastValue(CC)/LastValue(PHL))-1)<0.01*DisRange; 
barvisible = Status("barvisible"); 
firstvisiblebar = barvisible & NOT Ref(barvisible,-1); 
HHvisible = LastValue(HighestSince(firstvisiblebar,HH)); 
LLvisible = LastValue(LowestSince(firstvisiblebar,LL)); 

RaH = HHvisible *1.05; //Range High 
RaL = LLVisible *0.95; //Range Low 

SL_plot = IIf(x>=xa & SL>RaL & SL<RaH,SL,IIf(x>=xa & RaL==0 ,SL,-1e10)); 
RL_plot = IIf(x>=xc & RL>RaL & RL<RaH,RL,IIf(x>=xc & RaL==0,RL,-1e10)); 
PLL_plot = IIf(x-x2>=0 & abs(LastValue(CC)/LastValue(exp((x-x2)*xcd_log)*Low_Value)-1)<0.01*DisRange & PLL>RaL & PLL<RaH,PLL,IIf(x-x2>=0 & RaL==0 & PLLd,PLL,-1e10)); 
PHL_plot = IIf(x-x3>=0 & abs(LastValue(CC)/LastValue(exp((x-x3)*xab_log)*Hi_Value)-1)<0.01*DisRange & PHL>RaL & PHL<RaH,PHL,IIf(x-x3>=0 & RaL==0 & PHLd,PHL,-1e10)); 

GraphXSpace = 1.5; 

LastBar = Cum(1) == LastValue(Cum(1)); 
//Plot(CC,"close" ,IIf(LastBar,IIf(BS,colorGreen,IIf(SS,4,1)),1),32); 
Plot(SL_plot ,"SL" ,Color_SL,1+4+styleNoRescale|4096); 
Plot(RL_plot ,"RL" ,Color_RL,1+4+styleNoRescale|4096); 
Plot(PLL_plot,"PLL",Color_PLL,1+4+styleNoRescale|4096); 
Plot(PHL_plot,"PHL",Color_PHL,1+4+styleNoRescale|4096); 

/*Title = Name() + EncodeColor( colorRed )+" pds:" + WriteVal(per,1) +" " 

+ EncodeColor(colorBlue)+ Date()+EncodeColor(3)+" "+WriteVal(CC,1.2)+ " ("+WriteIf(CC>Ref(CC,-1),"+","")+WriteVal(ROC(CC,1),1.2)+"%)"; */

/*Introduction 
This formula is for automatic analyzer & indicator builder. Select the last one Day in automatic analyzer. 
Note! Backtest gives unrealistic results !!! 
Reentry = Reentry-before(1)OR Reentry-after(-1) 
Breakout = bull.breakout(1) OR bear.breakout(-1) 
TC = uptrend(1) OR downtrend(-1) 
Patterns 
AT(+1) = Ascending Triangle 
DT(-1) = Descending Triangle 
ST(+1) = Symmetrical Triangle 
ET(-1) = Extended Triangle 
RW(+1) = Rising Wedge 
FW(-1) = Falling Wedge 
ROC SL = daily ROC Support Line (SL) 
ROC RL = daily ROC Resistance Line (RL) 
per = percent change to detect peaks & throughs (I prefer to use 3 OR 4 sheets with different values, so you see mostly the underlying trend OR pattern, OR also & more quickly switch to weekly OR Month.*/ 
_SECTION_END();
This defines what a trendline is, what a breakout is, etc.
Can anyone pls help me to finish the job?
 
#3
Hi freinds

i am looking for specific retracement. between 37.5% and 41.2 decline with a reasonable tolerance. (like image)

i changed gartley222 to get it buy there are some problrms:

when i scan , the dates are not correct. I want it to show the date of between 37.5% and 41.2 decline but this indicator is wrong.

Please help me.
 

Attachments

Similar threads