Hi,
This example can help you.
Best regards
|// ZigZag Retracement
// Percent and Bar numbers
_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", .618, .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", 1);
*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, "", *colorLightBlue*, *styleLine*);
xr = LineArray(xr0, yr0, xr1, yr1, 0);
Plot(xr, "", *colorLightBlue*, *styleLine*);
*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(" \n(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();|
Le 29/08/2010 05:42, aarbee60 a écrit :
I refer to the Gann Swing Chart code originally done be Tomasz. The
charts identify the Swing Low and Swing High charts with dots
depicting the no. of bars since the last swing point. I would like the
code to draw straight lines connecting the High and Low points of the
swing bars, much like a zig-zag indicator. Tomasz's code is quite
clever but I have not been able to do as I have outlined above.
Can someone please help, please.
The original code is appended below.
Best regards
_SECTION_BEGIN("Gann Swing chart v4 ");
/* Gann Swing chart v4 */
/* Last modified 24 Feb 2001 */
/* AFL implementation by */
/* (C)2001 Tomasz Janeczko */
/* one and two - bar reversal version */
/* Change this to 1 for two-bar mode */
twobarmode = 0;
outsidebar = Outside();
insidebar = H <= Ref( H, -1 ) AND L >= Ref( L, -1 ); // built-in
inside() works with < > instead of <= >=
upbar = H > Ref( H, -1 ) AND L >= Ref( L, -1 );
downbar = L < Ref( L, -1 ) AND H <= Ref( H, -1 );
upbar2 = upbar OR ( outsidebar AND Ref( downbar, 1 ) ) ;
downbar2 = downbar OR ( outsidebar AND Ref( upbar, 1 ) );
Poi = outsidebar AND Ref( insidebar, 1 );
Poo = outsidebar AND Ref( outsidebar, 1 );
Pooi = Poo AND Ref( insidebar, 2 );
Poio = Poi AND Ref( outsidebar, 2 );
Poii = Poi AND Ref( insidebar, 2 );
Pooo = Poo AND Ref( outsidebar, 2 );
Poooi = Pooo AND Ref( insidebar, 3 );
Pooio = Pooi AND Ref( outsidebar, 3 );
Poioo = Poio AND Ref( outsidebar, 3 );
upbar3 = upbar2 OR ( Poi AND Ref( downbar, 2 ) );
downbar3 = downbar2 OR ( Poi AND Ref( upbar, 2 ) );
upbar4 = upbar3 OR ( Poo AND Ref( upbar, 2 ) );
downbar4 = downbar3 OR ( Poo AND Ref( downbar, 2 ) );
upbar5 = upbar4 OR ( Pooi AND Ref( upbar, 3 ) );
downbar5 = downbar4 OR ( Pooi AND Ref( downbar, 3 ) );
upbar6 = upbar5 OR ( Poio AND Ref( upbar, 3 ) );
downbar6 = downbar5 OR ( Poio AND Ref( downbar, 3 ) );
upbar7 = upbar6 OR ( Poii AND Ref( downbar, 3 ) );
downbar7 = downbar6 OR ( Poii AND Ref( upbar, 3 ) );
upbar8 = upbar7 OR ( Pooo AND Ref( downbar, 3 ) );
downbar8 = downbar7 OR ( Pooo AND Ref( upbar, 3 ) );
upbar9 = upbar8 OR ( Poooi AND Ref( downbar, 4 ) );
downbar9 = downbar8 OR (Poooi AND Ref( upbar, 4 ) );
upbar10 = upbar9 OR ( Pooio AND Ref( downbar, 4 ) );
downbar10 = downbar9 OR ( Pooio AND Ref( upbar, 4 ) );
upbar11 = upbar10 OR ( Poioo AND Ref( downbar, 4 ) );
downbar11 = downbar10 OR ( Poioo AND Ref( upbar, 4 ) );
fupbar = upbar11;
fdownbar = downbar11;
// Two-bar mode handling
fupbar = IIf( twobarmode, fupbar AND Ref( fupbar, 1 ), fupbar );
fdownbar = IIf( twobarmode, fdownbar AND Ref( fdownbar, 1 ), fdownbar );
/* Barcount since most recent up and down swings */
Us = BarsSince( fupbar );
Ds = BarsSince( fdownbar ) ;
Sd1 =IIf( Us==0, 1, IIf( Ds==0, -1, 0) );
Sd2 = IIf( Sd1 == 1, IIf( Ref( BarsSince(Sd1==1), -1) > Ref(
BarsSince( Sd1 == -1), -1), 1, 0),
IIf( Sd1 == -1, IIf( Ref( BarsSince(Sd1==1),-1) < Ref( BarsSince( Sd1
== -1),-1), -1, 0), 0 ) );
g0 = ValueWhen( Sd2, Sd2 ); /* sample and hold non-zero values */
/* shift back one bar, add one dummy swing at the end and shift forward */
/* to ensure proper handling of the last swing */
lastbar = Cum(1) == LastValue( Cum(1) );
swinglevel = IIf( g0 == -1, HighestSince( Sd2 == 1, H ), LowestSince(
Sd2 == -1, L ) );
swinglevel = IIf( lastbar, IIf( g0 == -1, LowestSince( Sd2 == -1, L ),
HighestSince( Sd2 == 1, H )), Ref( swinglevel, 1 ) );
/* Gann Swing chart drawn here */
Plot(Ref( ValueWhen( Ref( Sd2 != 0, 1 ) OR lastbar, swinglevel , 0 ),
-1 ), _DEFAULT_NAME(), colorBlue, styleDots);
/* Colourized price bars drawn here */
/* graph0 = close; */
/* graph0style = 128; */
/* barcolor = IIF( outsidebar, 1, IIF( downbar, 4, IIF( upbar, 5, 0 )
) ); */
/* graph0barcolor = ValueWhen( barcolor != 0, barcolor ); */
_SECTION_END();