you mean the built in Amibroker ZigZag function? Yes you can use that also it is just that this uses percentage. So if you switch to another timeframe your will have to adjust the percentage. I like ATR because it gives similar waves in all timeframes. Recently I made another one that I like even better than ATR. This one also shows a similar pattern in any timeframe without having to change the parameter settings, see code below
nbar = Param( "Swing number of Bars", 10, 1, 50, 1 );
priceswitch = ParamList( "ZigZag Mode", "Close|High&Low|Mixed 1|Mixed 2", 0 );
showTrailline = ParamToggle( "Show Trail Line", "Off|On", 0 );
showprimarypivotlabels = ParamToggle( "Show Primary Pivot Labels", "Off|On", 1 );
handleBothOutside = ParamToggle( "Long AND Short signal at the SAME bar", "Change Trend|Stay with current Trend", 0 );
sz = Param( "Text Size", 8, 5, 22, 1 );
ft = ParamList( "Font", "Tahoma|Helvetica|Arial Black|Verdana|Courier New|Compact|Segoe UI", 2 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = 0;
ZigZag = line1 = Null;
prcl = prch = crossup = crossdn = 0;
if( priceswitch == "High&Low" )
{
prch = H;
prcl = L;
mode = "High/Low mode";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = prch > Ref( res, -1 );
crossdn = prcl < Ref( sup, -1 );
// trail line price
bp = Max( O, Ref( res, -1 ) );
sp = Min( O, Ref( sup, -1 ) );
}
if( priceswitch == "Close" )
{
prch = C;
prcl = C;
mode = "Close mode";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = prch > Ref( res, -1 );
crossdn = prcl < Ref( sup, -1 );
// trail line price
bp = C;
sp = C;
}
if( priceswitch == "Mixed 1" )
{
prch = H;
prcl = L;
mode = "Mixed mode 1";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = C > Ref( res, -1 );
crossdn = C < Ref( sup, -1 );
// trail line price
bp = C;
sp = C;
}
if( priceswitch == "Mixed 2" )
{
prch = C;
prcl = C;
mode = "Mixed mode 2";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = H > Ref( res, -1 );
crossdn = L < Ref( sup, -1 );
// trail line price
bp = Max( O, Ref( res, -1 ) );
sp = Min( O, Ref( sup, -1 ) );
}
function calculateZigZag()
{
// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, prcl, 1 );
zigupHigh = ValueWhen( pk, prch, 0 );
zigupLowIndex = ValueWhen( tr, bi, 1 );
zigupHighIndex = ValueWhen( pk, bi, 0 );
slopeup = IIf( zigup, ( zigupHigh - zigupLow ) / ( zigupHighIndex - zigupLowIndex ) , Null );
zigupLine = IIf( zigup, zigupLow + slopeup * BarsSince( tr ), Null );
zigdn = Flip( pk, tr );
zigdnLow = ValueWhen( tr, prcl, 0 );
zigdnHigh = ValueWhen( pk, prch, 1 );
zigdnLowIndex = ValueWhen( tr, bi, 0 );
zigdnHighIndex = ValueWhen( pk, bi, 1 );
slopedn = IIf( zigdn, ( zigdnLow - zigdnHigh ) / ( zigdnLowIndex - zigdnHighIndex ) , Null );
zigdnLine = IIf( zigdn, zigdnHigh + slopedn * BarsSince( pk ), Null );
ZigZag = IIf( zigup, zigupLine, IIf( zigdn, zigdnLine, Null ) );
ZigZag = IIf( bi > Max( LastValue( ValueWhen( tr, bi ) ), LastValue( ValueWhen( pk, bi ) ) ), Null, ZigZag );
}
function calculateLastSegments()
{
lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, prch ) );
lastvaltr = LastValue( ValueWhen( tr, prcl ) );
valpk = LastValue( HighestSince( Ref( tr, -1 ), prch , 1 ) );
idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );
if( lastidxpk > lastidxtr )
{
x0 = lastidxpk;
y0 = lastvalpk;
x1 = idxtr;
y1 = valtr;
line1 = linedn = LineArray( x0, y0, x1, y1 );
tr[idxtr] = 1;
}
if( lastidxpk < lastidxtr )
{
x0 = lastidxtr;
y0 = lastvaltr;
x1 = idxpk;
y1 = valpk;
line1 = lineup = LineArray( x0, y0, x1, y1 );
pk[idxpk] = 1;
}
ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}
// is used in High&Low mode only
// if both crossup and crossdn are true then,
// when handleBothOutside is true then it will stay in the current trend
// when handleBothOutside is false it will change the trend (default)
if( handleBothOutside )
{
crossup = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossup, -1 ) ) > BarsSince( Ref( crossdn, -1 ) ) ), 0, crossup );
crossdn = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossdn, -1 ) ) > BarsSince( Ref( crossup, -1 ) ) ), 0, crossdn );
}
crossup = ExRem( crossup, crossdn );
crossdn = ExRem( crossdn, crossup );
trendup = Flip( crossup, crossdn );
trenddn = Flip( crossdn, crossup );
// find pivots
rcrossdn = Reverse( crossdn );
rcrossup = Reverse( crossup );
l1 = LowestSince( crossdn, prcl );
rL = Reverse( prcl );
l2 = LowestSince( Ref( rcrossup, -1 ), rL );
rl2 = Reverse( l2 );
h1 = HighestSince( crossup, prch );
rH = Reverse( prch );
h2 = HighestSince( Ref( rcrossdn, -1 ), rH );
rh2 = Reverse( h2 );
tr = l1 == rl2 AND trenddn;
pk = h1 == rh2 AND trendup;
rpk = Reverse( pk );
rtr = Reverse( tr );
rpk = ExRem( rpk, rtr );
rtr = ExRem( rtr, rpk );
pk = Reverse( rpk );
tr = Reverse( rtr );
calculateZigZag();
calculateLastSegments();
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Price", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
if( showtrailline )
{
Plot( IIf( trendup, sup, Null ), "", colorGreen, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( IIf( trenddn, res, Null ), "", colorRed, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
//PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
//PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), ColorWhite, 0, sp, 0 );
}
PlotShapes( IIf( crossup, shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
PlotShapes( IIf( crossdn, shapeSmallCircle, shapeNone ), colorWhite, 0, sp, 0 );
Plot( zigzag, "ZigZag", colorGold, styleLine | styleNoLabel | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), colorGreen, 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), colorRed, 0, H, 10 );
if( showprimaryPivotLabels )
{
clr = colorDefault;
upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );
for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, prch, i ) );
VarSet( "tl" + i, ValueWhen( tr, prcl, i ) );
}
ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;
cumulativeVolumeUpleg = SumSince( tr, V );
cumulativeVolumeDnleg = SumSince( pk, V );
lengthUpleg = SumSince( tr, 1 );
lengthDnleg = SumSince( pk, 1 );
for( i = fvb; i <= lvb; i++ )
{
if( ll[i] )
{
str = "LL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
}
if( hl[i] )
{
str = "HL" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
}
if( db[i] )
{
str = "DB" + " / " + cumulativeVolumeDnleg[i] + " / " + lengthDnleg[i] + " / " + int( SafeDivide( cumulativeVolumeDnleg[i], lengthDnleg[i] ) );
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -sz * 4 );
}
if( hh[i] )
{
str = "HH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
}
if( lh[i] )
{
str = "LH" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
}
if( dt[i] )
{
str = "DT" + " / " + cumulativeVolumeUpleg[i] + " / " + lengthUpleg[i] + " / " + int( SafeDivide( cumulativeVolumeUpleg[i], lengthUpleg[i] ) );
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, sz * 3 );
}
}
}
Title = Name() +
" | " + Now( 1 ) + " | " + Now( 2 ) +
" | " + EncodeColor( colorAqua ) + priceswitch + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorLightOrange ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorGold ) + "Chart time frame (Min): " + Prec( Interval() / 60, 0 ) + " Min" + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorWhite ) + Prec( ( C - Ref( C, -1 ) ) / Ref( C, -1 ) * 100, 2 ) + " %";
Plot( ValueWhen( pk, H ), "", colorBlue, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( ValueWhen( tr, L ), "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );