Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
Find Out More Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

ATR ZigZag for Amibroker (AFL)

Copy & Paste Friendly
// ATR ZigZag, Multiple Timeframe
// E.M.Pottasch, Nov 2016

Version ( 5.87 );
GfxSetZOrder( -5 );
GfxSetCoordsMode( 1 );
x = BarIndex();
fvb = FirstVisibleValue( x );
lvb = LastVisibleValue( x );

perBull = Param( "perBull", 20, 1, 150, 1 );
perBear = Param( "perBear", 20, 1, 150, 1 );
multBull = Param( "multBull", 2, 1, 4, 0.05 );
multBear = Param( "multBear", 2, 1, 4, 0.05 );
tf = Param( "Time Frame (min)", 5, 1, 1440 * 10, 1 ); // 1440 minutes is 1 day
tfrm = in1Minute * tf;
tog2 = ParamToggle( "Plot Trail", "No|Yes", 0 );
tog4 = ParamToggle( "Show Labels", "No|Yes", 1 );
tog7 = ParamToggle( "Draw Zig Zag", "No|Yes", 1 );

fact = Nz( Max( tfrm / 60, Interval() / 60 ) / ( Interval() / 60 ) );

function vstop_func()
{
    tvHigh = C;
    tvLow = C;

    sup = tvHigh - multBull * ATR( perBull );
    res = tvLow + multBear * ATR( perBear );

    // following Trail code is from TJ
    // http://traders.com/Documentation/FEEDbk_docs/2009/06/TradersTips.html
    trailARRAY = Null;
    trailstop = 0;

    for( i = 1; i < BarCount; i++ )
    {
        if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
            trailstop = Max( trailstop, sup[ i ] );
        else
            if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
                trailstop = Min( trailstop, res[ i ] );
            else
                trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );

        trailARRAY[ i ] = trailstop;
    }

    return trailARRAY;
}

TimeFrameSet( tfrm );
trBull = multBull * ATR( perBull );
trBear = multBear * ATR( perBear );
trailArray = vstop_func();
ts = IIf( trailArray > C, trailArray, Null ); // dntrend
tl = IIf( trailArray < C, trailArray, Null ); // uptrend
TimeFrameRestore();

ts = TimeFrameExpand( ts, tfrm, expandLast );
tl = TimeFrameExpand( tl, tfrm, expandLast );

lll = LLV( L, BarsSince( !IsEmpty( tl ) ) );
lll = IIf( ts, lll, Null );
trm = ts AND L == lll;

hhh = HHV( H, BarsSince( !IsEmpty( ts ) ) );
hhh = IIf( tl, hhh, Null );
pkm = tl AND H == hhh;

tr = ExRem( Reverse( trm ), Reverse( pkm ) );
pk = ExRem( Reverse( pkm ), Reverse( trm ) );

tr = Reverse( tr );
pk = Reverse( pk );

for( i = 0; i < 3; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, x, i ) );
    VarSet( "tx" + i, ValueWhen( tr, x, i ) );
    VarSet( "ph" + i, ValueWhen( pk, H, i ) );
    VarSet( "tl" + i, ValueWhen( tr, L, 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;

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

PlotShapes( shapeSmallCircle*trm, ColorRGB( 0, 30, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle*pkm, ColorRGB( 50, 0, 0 ), 0, H, 10 );
PlotShapes( shapeSmallCircle*tr, IIf( ValueWhen( trm, x, 1 ) < ValueWhen( pkm, x, 0 ), ColorRGB( 0, 255, 0 ), ColorRGB( 255, 255, 255 ) ), 0, L, -10 );
PlotShapes( shapeSmallCircle*pk, IIf( ValueWhen( pkm, x, 1 ) < ValueWhen( trm, x, 0 ), ColorRGB( 255, 0, 0 ), ColorRGB( 255, 255, 255 ) ), 0, H, 10 );

function drawPivotLabels()
{
    sz = 5;

    for( i = lvb; i > fvb; i-- )
    {
        {
            if( ll[i] )
            {
                PlotTextSetFont( "LL", "Arial Black", sz, i, L[i], colorGreen, colorDefault, -25 );
            }

            if( hl[i] )
            {
                PlotTextSetFont( "HL", "Arial Black", sz, i, L[i], colorGreen, colorDefault, -25 );
            }

            if( db[i] )
            {
                PlotTextSetFont( "DB", "Arial Black", sz, i, L[i], colorLightBlue, colorDefault, -25 );
            }

            if( hh[i] )
            {
                PlotTextSetFont( "HH", "Arial Black", sz, i, H[i], colorRed, colorDefault, 20 );
            }

            if( lh[i] )
            {
                PlotTextSetFont( "LH", "Arial Black", sz, i, H[i], colorRed, colorDefault, 20 );
            }

            if( dt[i] )
            {
                PlotTextSetFont( "DT", "Arial Black", sz, i, H[i], colorOrange, colorDefault, 20 );
            }
        }
    }
}
function drawZigZag()
{
    for( i = lvb; i > fvb; i-- )
    {
        if( tr[i] )
        {
            GfxSelectPen( colorOrange, 1 );
            GfxMoveTo( tx1[i], tl1[i] );
            GfxLineTo( px1[i], ph1[i] );
        }

        if( pk[i] )
        {
            GfxSelectPen( colorLightBlue, 1 );
            GfxMoveTo( px1[i], ph1[i] );
            GfxLineTo( tx1[i], tl1[i] );
        }
    }
}
Title = Name() +
        " | " + Now( 2 );

if( tog4 ) drawPivotLabels();

if( tog7 ) drawZigZag();

if( tog2 )
{
    Plot( ts, "", colorRed, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( lll, "", colorRed, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( tl, "", colorGreen, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
    Plot( hhh, "", colorGreen, styleDashed | styleNoRescale, Null, Null, 0, 0, 1 );
}
Back