Hello guys,
I'm trying to code a piece of code in AFL to get Zigzag retracements.
I found something on the library which unfortunately didn't work properly so I'm trying to fix it.
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:
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.
I'm trying to code a piece of code in AFL to get Zigzag retracements.
I found something on the library which unfortunately didn't work properly so I'm trying to fix it.
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);
*/
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.