Fast afl code for ZigZag based on Price Change- anyone?

actually this is nicer (code below). You can also put it inside the ZigZag array, but I did not do this. I just draw the yellow line

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
SetBarsRequired( ( lvb - fvb ) * 4 );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;
Buy = Sell = 0;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;
            trend = -1;
            Sell[i] = 1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
                topidx = i;
                breakl = topprc - fluc;
                trend = 1;
                Buy[i] = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

uptrend = Flip( Buy, Sell );
dntrend = Flip( Sell, Buy );

Plot( ZigZag, "", IIf( uptrend, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 2 );

BuyPrice = SellPrice = C;
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, H ) );
lastvaltr = LastValue( ValueWhen( tr, L ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( tr, H ) ); 
idxpk = LastValue( ValueWhen( H == valpk, bi ) );
valtr = LastValue( LowestSince( pk, H ) ); 
idxtr = LastValue( ValueWhen( L == valtr, bi ) );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        if( Sell[i] AND lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
        {
            x0 = lastidxpk;
            y0 = lastvalpk;
            x1 = idxtr;
            y1 = valtr;
            GfxSelectPen( colorYellow, 2, 1 );
            GfxMoveTo( x0, y0 );
            GfxLineTo( x1, y1 );
        }

        if( Buy[i] AND lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
        {
            x0 = lastidxtr;
            y0 = lastvaltr;
            x1 = idxpk;
            y1 = valpk;
            GfxSelectPen( colorYellow, 2, 1 );
            GfxMoveTo( x0, y0 );
            GfxLineTo( x1, y1 );
        }

        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}
1 Like

Hi Ed,

Thanks! But I thought it would be a very neat solution to just add the unconfirmed pk/tr after your Loop and that way it will be used as normal pivots in the remaining code since then I can get the earliest possible visual/code signals.

Unconfirmed Pivot

The first yellow line should actually be Black since Price has traveled up more than my set 4 ticks. I will fix that now.

Jorgen

have a look here. Last peak or trough is unconfirmed. Will be away now for a bit (shopping)

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
SetBarsRequired( ( lvb - fvb ) * 4 );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;
Buy = Sell = 0;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;
            trend = -1;
            Sell[i] = 1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
                topidx = i;
                breakl = topprc - fluc;
                trend = 1;
                Buy[i] = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

uptrend = Flip( Buy, Sell );
dntrend = Flip( Sell, Buy );

lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, H ) );
lastvaltr = LastValue( ValueWhen( tr, L ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( tr, H ) );
idxpk = LastValue( ValueWhen( H == valpk, bi ) );
valtr = LastValue( LowestSince( pk, H ) );
idxtr = LastValue( ValueWhen( L == valtr, bi ) );

if( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
{
    x0 = lastidxpk;
    y0 = lastvalpk;
    x1 = idxtr;
    y1 = valtr;
    line = linedn = LineArray( x0, y0, x1, y1 );
    tr[idxtr] = 1;
}

if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
{
    x0 = lastidxtr;
    y0 = lastvaltr;
    x1 = idxpk;
    y1 = valpk;
    line = lineup = LineArray( x0, y0, x1, y1 );
    pk[idxpk] = 1;
}

ZigZag = IIf( !IsEmpty( line ), line, ZigZag );

Plot( ZigZag, "", IIf( uptrend, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 2 );
Plot( IIf( !IsEmpty( line ), line, Null ), "", colorYellow, styleDashed, Null, Null, 0, 2, 2 );

BuyPrice = SellPrice = C;
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

// do this again to handle the last unconfirmed peak or trough
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, 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;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}
1 Like

Hi Ed,

I am working on the details around the last unconfirmed leg, since for the very earliest visual/code signals the last leg can at times be partially confirmed- as long at the price traveled the set distance.

Like this 2 lines drawn by hand:

early confirmation

But I have not yet confirmed that this would be the way to go- but looks like it... I am working on it but will take a break this evening since I don't trade today. Need to finish this first.

Thanks and Regards,

Jorgen

in my opinion the last code I posted is correct, you can not draw additional legs because you can only start drawing a new leg once the trend of this leg is established.

one thing with the last code I posted is that you need to initiallize "line"
line = Null;

(initialize below line 128). Not always neccesary but in some cases.

a 10 tick ES chart looks like this on my computer

es

1 Like

Hi Ed,

I didn't fully explain, I am sorry! You are correct! your code is correct and does a perfect job of creating a ZigZag indicator based on Price change!

But in the Wyckoff Wave method, things are slightly different. Since the black leg I draw in the screen capture is a confirmed move, I use it. If the Price goes up or down after this "created leg" doesn't matter since this portion will always stay as a Price Increase and therefore I can confirm it even if the trend of the leg is not established.

I hope that clarifies it.

Regards,

Jorgen

Hi again,

I added some code just after your ZigZag loop and done!

image

Thanks and Regards,

Jorgen

I put this just after your ZigZag loops:

    // IDENTIFY CONFIRMED AND UNCONFIRMED BARS FOR THE LAST ZIGZAG LEG:
    UnconfBars	= LastValue( Min( BarsSince( pk ), BarsSince( tr ) ) );
    Confirmed		= IIf( BI > LastValue( BI ) - UnconfBars + 1, 0, 1 );
    HighNoConf	= LastValue( HHV( prch, UnconfBars ) );
    LowNoConf	= LastValue( LLV( prcl, UnconfBars ) );

    if( tr[ LastValue( BI ) - UnconfBars ] AND HighNoConf > prch[ LastValue( BI ) ] )
    {
        if( HighNoConf - prcl[ LastValue( BI ) - UnconfBars ] >= fluc )
        {
            pkBI = LastValue( ValueWhen( HighNoConf == prch, BI, 1 ) );
            pk[  pkBI ]	= True;
        }

        Confirmed = IIf( BI > pkBI, 0, 1 );
    }
    else
        if( tr[ LastValue( BI ) - UnconfBars ] AND HighNoConf - LowNoConf >= fluc )
            Confirmed	= IIf( BI > LastValue( BI ) - UnconfBars + 1, 1, 1 );

    if( pk[ LastValue( BI ) - UnconfBars ] AND LowNoConf < prcl[ LastValue( BI ) ] )
    {
        if( prch[ LastValue( BI ) - UnconfBars ] - LowNoConf >= fluc )
        {
            trBI = LastValue( ValueWhen( LowNoConf == prcl, BI, 1 ) );
            tr[  trBI ]	= True;
        }

        Confirmed = IIf( BI > trBI, 0, 1 );
    }
    else
        if( pk[ LastValue( BI ) - UnconfBars ] AND HighNoConf - LowNoConf >= fluc )
            Confirmed	= IIf( BI > LastValue( BI ) - UnconfBars + 1, 1, 1 );

    UpOrDn					= LastValue( BarsSince( pk ) - BarsSince( tr ) );
    tr[ LastValue( BI ) ]	= IIf( UpOrDn < 0, True, False );
    pk[ LastValue( BI ) ]	= IIf( UpOrDn > 0, True, False );

Then I plot the ZigZag as follows:

Plot( ZigZag, "", IIf( Confirmed, colorBlack, colorYellow ), styleLine, Null, Null, Null, 5, 2 );

This way, I don't need to take any special care of the last leg if I create some signals.

Regards,

Jorgen

hi,

yes I understood you correctly but my point is that the last piece of line you draw is unconfirmed, however the leg before that is also unconfirmed. Well the leg before that the direction (uptrend of downtrend) is confirmed but the leg has not yet completed. It could go up or down much further. The last part you added both the direction and the entire existence are unconfirmed. The last piece of leg could disappear entirely.

For completion I also added that in my code. I called it line2 and has a lightblue color.

there was also an error in there which I corrected for I used
valtr = LastValue( LowestSince( pk, H ) ); and that should be
valtr = LastValue( LowestSince( pk, L ) );

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
SetBarsRequired( ( lvb - fvb ) * 4 );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;
Buy = Sell = 0;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;
            trend = -1;
            Sell[i] = 1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
                topidx = i;
                breakl = topprc - fluc;
                trend = 1;
                Buy[i] = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

uptrend = Flip( Buy, Sell );
dntrend = Flip( Sell, Buy );

lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, H ) );
lastvaltr = LastValue( ValueWhen( tr, L ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( tr, H ) );
idxpk = LastValue( ValueWhen( H == valpk, bi ) );
valtr = LastValue( LowestSince( pk, L ) );
idxtr = LastValue( ValueWhen( L == valtr, bi ) );
line = line2 = Null;

if( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
{
    x0 = lastidxpk;
    y0 = lastvalpk;
    x1 = idxtr;
    y1 = valtr;
    line = linedn = LineArray( x0, y0, x1, y1 );
    tr[idxtr] = 1;

    if( idxtr < BarCount - 1 )
    {
        line2 = LineArray( x1, y1, BarCount - 1, H[BarCount - 1] );
        pk[BarCount - 1] = 1;
    }
}

if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
{
    x0 = lastidxtr;
    y0 = lastvaltr;
    x1 = idxpk;
    y1 = valpk;
    line = lineup = LineArray( x0, y0, x1, y1 );
    pk[idxpk] = 1;

    if( idxpk < BarCount - 1 )
    {
        line2 = LineArray( x1, y1, BarCount - 1, L[BarCount - 1] );
        tr[BarCount - 1] = 1;
    }
}

ZigZag = IIf( !IsEmpty( line ), line, ZigZag );
ZigZag = IIf( !IsEmpty( line2 ), line2, ZigZag );

Plot( ZigZag, "", IIf( uptrend, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 2 );
Plot( IIf( !IsEmpty( line ), line, Null ), "", ColorRGB( 255, 255, 0 ), styleLine, Null, Null, 0, 2, 2 );
Plot( IIf( !IsEmpty( line2 ), line2, Null ), "", ColorRGB( 0, 255, 255 ), styleLine, Null, Null, 0, 2, 2 );

BuyPrice = SellPrice = C;
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

// do this again to handle the last unconfirmed peak or trough
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, 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;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}
1 Like

Hi Ed,

Maybe Confirming the Wave is a better way to put it. Anyway, I am viewing my live chart and check what we have so far.

Thanks and Regards,

Jorgen

i added small change to line2. I was drawing it to the last bar but should stop at the segment low or high. But that leaves room for another unconfirmed wave :slight_smile:

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
SetBarsRequired( ( lvb - fvb ) * 4 );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;
Buy = Sell = 0;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;
            trend = -1;
            Sell[i] = 1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
                topidx = i;
                breakl = topprc - fluc;
                trend = 1;
                Buy[i] = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

uptrend = Flip( Buy, Sell );
dntrend = Flip( Sell, Buy );

lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, H ) );
lastvaltr = LastValue( ValueWhen( tr, L ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( tr, H ) );
idxpk = LastValue( ValueWhen( H == valpk, bi ) );
valtr = LastValue( LowestSince( pk, L ) );
idxtr = LastValue( ValueWhen( L == valtr, bi ) );
line = line2 = Null;

if( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
{
    x0 = lastidxpk;
    y0 = lastvalpk;
    x1 = idxtr;
    y1 = valtr;
    line = linedn = LineArray( x0, y0, x1, y1 );
    tr[idxtr] = 1;
    valpk = LastValue( HighestSince( tr, H ) );
    idxpk = LastValue( ValueWhen( H == valpk, bi ) );

    if( idxtr < idxpk )
    {
        line2 = LineArray( x1, y1, idxpk, valpk );
        pk[idxpk] = 1;
    }
}

if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
{
    x0 = lastidxtr;
    y0 = lastvaltr;
    x1 = idxpk;
    y1 = valpk;
    line = lineup = LineArray( x0, y0, x1, y1 );
    pk[idxpk] = 1;
    valtr = LastValue( LowestSince( pk, L ) );
    idxtr = LastValue( ValueWhen( L == valtr, bi ) );

    if( idxpk < idxtr )
    {
        line2 = LineArray( x1, y1, idxtr, valtr );
        tr[idxtr] = 1;
    }
}

ZigZag = IIf( !IsEmpty( line ), line, ZigZag );
ZigZag = IIf( !IsEmpty( line2 ), line2, ZigZag );

Plot( ZigZag, "", IIf( uptrend, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 2 );
Plot( IIf( !IsEmpty( line ), line, Null ), "", ColorRGB( 255, 255, 0 ), styleLine, Null, Null, 0, 2, 2 );
Plot( IIf( !IsEmpty( line2 ), line2, Null ), "", ColorRGB( 0, 255, 255 ), styleLine, Null, Null, 0, 2, 2 );

BuyPrice = SellPrice = C;
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

// do this again to handle the last unconfirmed peak or trough
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, 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;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}
1 Like

Hi Ed,

Cool! That should work just fine! I will test it out for a while in live trading and see how it goes.

I will update you in a few days or so, unless anything before that. :slight_smile:

Thanks and Regards,

Jorgen

1 Like

i noticed there was another problem. Only with these last 2 segments "line" and "line2".

because I use highestsince and lowestsince. I assumed this function starts working the bar after the condition but this is not the case. It also includes the bar of the condition. So this sometimes could cause a peak and trough on the same bar.

So you need to use: valpk = LastValue( HighestSince( Ref( tr, -1 ), H , 1 ) );

etc.

hate to post more code but for the sake of completion .... here is my latest version. Hope it is correct now. I'll wait couple of days and save up additional errors I find :slight_smile:

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
SetBarsRequired( ( lvb - fvb ) * 4 );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;
Buy = Sell = 0;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;
            trend = -1;
            Sell[i] = 1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
                topidx = i;
                breakl = topprc - fluc;
                trend = 1;
                Buy[i] = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

uptrend = Flip( Buy, Sell );
dntrend = Flip( Sell, Buy );

lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, H ) );
lastvaltr = LastValue( ValueWhen( tr, L ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( Ref( tr, -1 ), H , 1 ) );
idxpk = LastValue( ValueWhen( H == valpk, bi ) );
valtr = LastValue( LowestSince( Ref( pk, -1 ), L, 1 ) );
idxtr = LastValue( ValueWhen( L == valtr, bi ) );
line = line2 = Null;

if( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
{
    x0 = lastidxpk;
    y0 = lastvalpk;
    x1 = idxtr;
    y1 = valtr;
    line = linedn = LineArray( x0, y0, x1, y1 );
    tr[idxtr] = 1;
    valpk = LastValue( HighestSince( Ref( tr, -1 ), H, 1 ) );
    idxpk = LastValue( ValueWhen( H == valpk, bi ) );

    if( idxtr < idxpk )
    {
        line2 = LineArray( x1, y1, idxpk, valpk );
        pk[idxpk] = 1;
    }
}

if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
{
    x0 = lastidxtr;
    y0 = lastvaltr;
    x1 = idxpk;
    y1 = valpk;
    line = lineup = LineArray( x0, y0, x1, y1 );
    pk[idxpk] = 1;
    valtr = LastValue( LowestSince( Ref( pk, -1 ), L, 1 ) );
    idxtr = LastValue( ValueWhen( L == valtr, bi ) );

    if( idxpk < idxtr )
    {
        line2 = LineArray( x1, y1, idxtr, valtr );
        tr[idxtr] = 1;
    }
}

ZigZag = IIf( !IsEmpty( line ), line, ZigZag );
ZigZag = IIf( !IsEmpty( line2 ), line2, ZigZag );

Plot( ZigZag, "", IIf( uptrend, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 2 );
Plot( IIf( !IsEmpty( line ), line, Null ), "", ColorRGB( 255, 255, 0 ), styleLine, Null, Null, 0, 2, 2 );
Plot( IIf( !IsEmpty( line2 ), line2, Null ), "", ColorRGB( 0, 255, 255 ), styleLine, Null, Null, 0, 2, 2 );

BuyPrice = SellPrice = C;
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

// do this again to handle the last unconfirmed peak or trough
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, 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;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}
3 Likes

Noted!

Thanks!

Jorgen

Hi Ed,

I changed one thing in your code and that it is the calculation and plotting of the ZigZag and I am using pcrh and prcl instead of H and L. Works better....

Regards,

Jorgen

HI again,

I am trading now, so soon we will know if we have to find a way to confirm the wave or not necessary...

Jorgen

@empottasch

I also have been searching for a zigzag code that shows actual price swings. Thank you for the code.

One thing I noticed, sometimes it plots wrong zig, because latest candle High/Low should be above/below previous candle to get a swing. Please look into it.

PriceTicks = Param( "Price Change In Ticks", 4, 1, 40, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

fluc = TickSize * PriceTicks;

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// initial condition trend is up
trend = 1;
topprc = prch[0];
topidx = 0;
breakl = topprc - fluc;

for( i = 1; i < BarCount; i++ )
{
    if( trend > 0 )
    {
        if( prcl[i] <= breakl AND prch[i] < topprc )
        {
            pk[topidx] = 1;
            botprc = prcl[i];
            botidx = i;
            breakh = botprc + fluc;          
            trend = -1;
        }
        else
            if( prch[i] >= topprc )
            {
                topprc = prch[i];
                topidx = i;
                breakl = prch[i] - fluc;
            }
    }
    else
        if( trend < 0 )
        {
            if( prch[i] >= breakh AND prcl[i] > botprc )
            {
                tr[botidx] = 1;
                topprc = prch[i];
				topidx = i;
				breakl = topprc - fluc;
                trend = 1;
            }
            else
                if( prcl[i] <= botprc )
                {
                    botprc = prcl[i];
                    botidx = i;
                    breakh = prcl[i] + fluc;
                }
        }
}

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, 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;

// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, L, 1 );
zigupHigh = ValueWhen( pk, H, 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, L, 0 );
zigdnHigh = ValueWhen( pk, H, 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 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
Plot( ZigZag, "", colorwhite, styleLine, Null, Null, 0, 0, 2 );

PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 7;

GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {

        if( ll[i] )
        {
            str = "LL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hl[i] )
        {
            str = "HL\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( db[i] )
        {
            str = "DB\n" + L[i];
            PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -30 );
        }

        if( hh[i] )
        {
            str = "HH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( lh[i] )
        {
            str = "LH\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }

        if( dt[i] )
        {
            str = "DT\n" + H[i];
            PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 35 );
        }
    }
}

Swing

hi,

no it is intended to be like that, see also: https://mboxwave.com/

i assembled a later version which I use currently. I took it from a larger piece of code so I hope it works correctly

// AFL code, E.M.Pottasch 9/2020

PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );
mboxswitch = ParamToggle( "Show MBox", "No|Yes", 0 );
plottrailswitch = ParamToggle( "Show Trail Line", "No|Yes", 0 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
sz = Param( "Text Size", 8, 5, 12, 1 );

GraphXSpace = 0;
pk = tr = Buy = Sell = BuyPrice = SellPrice = 0;
ZigZag = line1 = line2 = Null;
trailarrayup = Null;
trailarraydn = Null;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// if ticksize is not set in code or information window use 0.01
if( TickSize == 0 )
{
    TickSize = 0.01;
}

function calculatePivots( PriceTicks )
{
    fluc = TickSize * PriceTicks;
    // initial condition trend is up (but -1 works also)
    trend = 1;
    topprc = prch[0];
    topidx = 0;
    botprc = prcl[0];
    botidx = 0;
    Buy = Sell = BuyPrice = SellPrice = 0;
    trailarrayup[0] = topprc - fluc;
    trailarraydn[0] = topprc + fluc;

    for( i = 1; i < BarCount; i++ )
    {
        if( trend > 0 )
        {
            // trend turning down, avoid same bar as the peak
            if( prcl[i] <= trailarrayup[i - 1] AND prch[i] < topprc )
            {
                pk[topidx] = 1;
                botprc = prcl[i];
                botidx = i;
                trend = -1;
                Sell[i] = 1;
                trailarraydn[i] = botprc + fluc;
                trailarrayup[i] = trailarrayup[i - 1];

                if( priceswitch )
                    SellPrice[i] = Min( O[i], trailarrayup[i] );
                else
                    SellPrice[i] = C[i];

            }
            else

                // still in uptrend and new top reached
                if( prch[i] >= topprc )
                {
                    topprc = prch[i];
                    topidx = i;
                    trailarrayup[i] = Max( trailarrayup[i - 1], prch[i] - fluc );
                }
                // continuation inside uptrend
                else
                {
                    trailarrayup[i] = Max( trailarrayup[i - 1], prch[i] - fluc );
                }
        }
        else
            if( trend < 0 )
            {
                // trend turning up, avoid same bar as the trough
                if( prch[i] >= trailarraydn[i - 1] AND prcl[i] > botprc )
                {
                    tr[botidx] = 1;
                    topprc = prch[i];
                    topidx = i;
                    trend = 1;
                    Buy[i] = 1;
                    trailarrayup[i] = topprc - fluc;
                    trailarraydn[i] = trailarraydn[i - 1];

                    if( priceswitch )
                        BuyPrice[i] = Max( O[i], trailarraydn[i] );
                    else
                        BuyPrice[i] = C[i];
                }
                else

                    // still in downtrend and new trough reached
                    if( prcl[i] <= botprc )
                    {
                        botprc = prcl[i];
                        botidx = i;
                        trailarraydn[i] = Min( trailarraydn[i - 1], prcl[i] + fluc );
                    }
                    // continuation inside downtrend
                    else
                    {
                        trailarraydn[i] = Min( trailarraydn[i - 1], prcl[i] + fluc );
                    }
            }
    }
}

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( )
{
    //line1 = line2 = Null;
    lastidxpk = LastValue( ValueWhen( pk, bi ) );
    lastidxtr = LastValue( ValueWhen( tr, bi ) );
    lastvalpk = LastValue( ValueWhen( pk, prch ) );
    lastvaltr = LastValue( ValueWhen( tr, prcl ) );
    lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
    lastidxsell = LastValue( ValueWhen( Sell, bi ) );
    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( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
    {
        x0 = lastidxpk;
        y0 = lastvalpk;
        x1 = idxtr;
        y1 = valtr;
        line1 = linedn = LineArray( x0, y0, x1, y1 );
        tr[idxtr] = 1;
        valpk = LastValue( HighestSince( Ref( tr, -1 ), prch, 1 ) );
        idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
    }

    if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
    {
        x0 = lastidxtr;
        y0 = lastvaltr;
        x1 = idxpk;
        y1 = valpk;
        line1 = lineup = LineArray( x0, y0, x1, y1 );
        pk[idxpk] = 1;
        valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
        idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );
    }

    ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

calculatePivots( PriceTicks );
calculateZigZag();
calculateLastSegments();

uptrend1 = Flip( Buy, Sell );
dntrend1 = Flip( Sell, Buy );
uptrend = Ref( Flip( tr, pk ), -1 );
dntrend = Ref( Flip( pk, tr ), -1 );

mboxdnlowArray = IIf( tr AND dntrend, Min( O, C ), null );
mboxdnlowArray = IIf( dntrend AND !tr, ValueWhen( tr, Min( O, C ), 0 ), mboxdnlowArray );
mboxdnhighArray = IIf( dntrend, HighestSince( Ref( pk, -1 ), Max( O, C ) ), null );
mboxuphighArray = IIf( pk AND uptrend, Max( O, C ), null );
mboxuphighArray = IIf( uptrend AND !pk, ValueWhen( pk, Max( O, C ), 0 ), mboxuphighArray );
mboxuplowArray = IIf( uptrend, LowestSince( Ref( tr, -1 ), Min( O, C ) ), null );

Plot( ZigZag, "", IIf( uptrend1, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 1 );
Plot( IIf( !IsEmpty( line1 ), line1, Null ), "", ColorRGB( 255, 255, 0 ), styleLine, Null, Null, 0, 2, 1 );
PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

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;

GfxSetZOrder( -1 );
GfxSetCoordsMode( 1 );

if( mboxswitch )
{
    ft = "Arial Black";
    clr = colorDefault;

    for( i = fvb; i <= lvb; i++ )
    {
        if( pk[i] )
        {
            c1 = 0;
            c2 = 0;
            c3 = 80;
            x0 = Nz( tx1[i] + 0.5 );
            x1 = Nz( px1[i] + 0.5 );
            yh = Nz( mboxuphighArray[i] );
            yl = Nz( mboxuplowArray[i] );
            GfxSelectPen( ColorRGB( c1, c2, c3 * 3 ), 2 );
            GfxSelectSolidBrush( ColorRGB( c1, c2, c3 ) );
            GfxRoundRect( x0, yh, x1, yl, 0, 0 );
        }

        if( tr[i] )
        {
            c1 = 80;
            c2 = 0;
            c3 = 0;
            x0 = Nz( px1[i] + 0.5 );
            x1 = Nz( tx1[i] + 0.5 );
            yh = Nz( mboxdnhighArray[i] );
            yl = Nz( mboxdnlowArray[i] );
            GfxSelectPen( ColorRGB( 3 * c1, c2, c3 ), 2 );
            GfxSelectSolidBrush( ColorRGB( c1, c2, c3 ) );
            GfxRoundRect( x0, yh, x1, yl, 0, 0 );
        }
    }
}

if( plottrailswitch )
{
    Plot( trailarrayup, "trail up", colorLightBlue, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
    Plot( trailarraydn, "trail dn", colorOrange, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
    PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorLightBlue, 0, L, -15 );
    PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
    PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), colorYellow, 0, BuyPrice, 0 );
    PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), colorYellow, 0, SellPrice, 0 );
}

ft = "Arial Black";
clr = colorDefault;

if( labelsswitch )
{
    str1 = "";

    for( i = fvb; i <= lvb; i++ )
    {
        if( pk[i] )
        {
            if( dt[i] )
                str1 = "DT";
            else
                if( hh[i] )
                    str1 = "HH";
                else
                    if( lh[i] )
                        str1 = "LH";

            PlotTextSetFont( str1, ft, sz, i, H[i], colorRed, clr, sz * 3 );
            PlotTextSetFont( "" + prch[i], ft, sz, i, H[i], colorRed, clr, sz * 5 );
        }

        if( tr[i] )
        {
            if( db[i] )
                str1 = "DB";
            else
                if( ll[i] )
                    str1 = "LL";
                else
                    if( hl[i] )
                        str1 = "HL";

            PlotTextSetFont( str1, ft, sz, i, L[i], colorGreen, clr, -sz * 4 );
            PlotTextSetFont( "" + prcl[i], ft, sz, i, L[i], colorGreen, clr, -sz * 6 );
        }
    }
}
5 Likes

@empottasch
Thank you for the reply.
I wanted just zigzag lines (Exact Swing Pivots) based on candle's "High/low" OR "Close" price, not based on any percentage change. Only one condition is needed that outside candle should be included/excluded by looking the next price action. Amibroker's Zig function only show as per the % change. I am new to coding and just started learning. Is there any way to get exact swing pivots?

hi,

you could try adding the following:

instead of

if( prcl[i] <= trailarrayup[i - 1] AND prch[i] < topprc  )

use

if( prcl[i] <= trailarrayup[i - 1] AND prch[i] < topprc AND prcl[i] <= prcl[i - 1] )

and instead of

if( prch[i] >= trailarraydn[i - 1] AND prcl[i] > botprc )

use

if( prch[i] >= trailarraydn[i - 1] AND prcl[i] > botprc AND prch[i] >= prch[i - 1] )

total code would then be (below). Let me know if this is correct in your opinion.

// AFL code, E.M.Pottasch 9/2020
PriceTicks = Param( "Price Change In Ticks", 4, 1, 100, 1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );
mboxswitch = ParamToggle( "Show MBox", "No|Yes", 0 );
plottrailswitch = ParamToggle( "Show Trail Line", "No|Yes", 0 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
sz = Param( "Text Size", 8, 5, 12, 1 );

GraphXSpace = 0;
pk = tr = Buy = Sell = BuyPrice = SellPrice = 0;
ZigZag = line1 = line2 = Null;
trailarrayup = Null;
trailarraydn = Null;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

if( priceswitch )
{
    prch = H;
    prcl = L;
}
else
{
    prch = C;
    prcl = C;
}

// if ticksize is not set in code or information window use 0.01
if( TickSize == 0 )
{
    TickSize = 0.01;
}

function calculatePivots( PriceTicks )
{
    fluc = TickSize * PriceTicks;
    // initial condition trend is up (but -1 works also)
    trend = 1;
    topprc = prch[0];
    topidx = 0;
    botprc = prcl[0];
    botidx = 0;
    Buy = Sell = BuyPrice = SellPrice = 0;
    trailarrayup[0] = topprc - fluc;
    trailarraydn[0] = topprc + fluc;

    for( i = 1; i < BarCount; i++ )
    {
        if( trend > 0 )
        {
            // trend turning down, avoid same bar as the peak
            if( prcl[i] <= trailarrayup[i - 1] AND prch[i] < topprc AND prcl[i] <= prcl[i - 1] )
            {
                pk[topidx] = 1;
                botprc = prcl[i];
                botidx = i;
                trend = -1;
                Sell[i] = 1;
                trailarraydn[i] = botprc + fluc;
                trailarrayup[i] = trailarrayup[i - 1];

                if( priceswitch )
                    SellPrice[i] = Min( O[i], trailarrayup[i] );
                else
                    SellPrice[i] = C[i];

            }
            else

                // still in uptrend and new top reached
                if( prch[i] >= topprc )
                {
                    topprc = prch[i];
                    topidx = i;
                    trailarrayup[i] = Max( trailarrayup[i - 1], prch[i] - fluc );
                }
                // continuation inside uptrend
                else
                {
                    trailarrayup[i] = Max( trailarrayup[i - 1], prch[i] - fluc );
                }
        }
        else
            if( trend < 0 )
            {
                // trend turning up, avoid same bar as the trough
                if( prch[i] >= trailarraydn[i - 1] AND prcl[i] > botprc AND prch[i] >= prch[i - 1] )
                {
                    tr[botidx] = 1;
                    topprc = prch[i];
                    topidx = i;
                    trend = 1;
                    Buy[i] = 1;
                    trailarrayup[i] = topprc - fluc;
                    trailarraydn[i] = trailarraydn[i - 1];

                    if( priceswitch )
                        BuyPrice[i] = Max( O[i], trailarraydn[i] );
                    else
                        BuyPrice[i] = C[i];
                }
                else

                    // still in downtrend and new trough reached
                    if( prcl[i] <= botprc )
                    {
                        botprc = prcl[i];
                        botidx = i;
                        trailarraydn[i] = Min( trailarraydn[i - 1], prcl[i] + fluc );
                    }
                    // continuation inside downtrend
                    else
                    {
                        trailarraydn[i] = Min( trailarraydn[i - 1], prcl[i] + fluc );
                    }
            }
    }
}

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( )
{
    //line1 = line2 = Null;
    lastidxpk = LastValue( ValueWhen( pk, bi ) );
    lastidxtr = LastValue( ValueWhen( tr, bi ) );
    lastvalpk = LastValue( ValueWhen( pk, prch ) );
    lastvaltr = LastValue( ValueWhen( tr, prcl ) );
    lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
    lastidxsell = LastValue( ValueWhen( Sell, bi ) );
    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( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
    {
        x0 = lastidxpk;
        y0 = lastvalpk;
        x1 = idxtr;
        y1 = valtr;
        line1 = linedn = LineArray( x0, y0, x1, y1 );
        tr[idxtr] = 1;
        valpk = LastValue( HighestSince( Ref( tr, -1 ), prch, 1 ) );
        idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
    }

    if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
    {
        x0 = lastidxtr;
        y0 = lastvaltr;
        x1 = idxpk;
        y1 = valpk;
        line1 = lineup = LineArray( x0, y0, x1, y1 );
        pk[idxpk] = 1;
        valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
        idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );
    }

    ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

calculatePivots( PriceTicks );
calculateZigZag();
calculateLastSegments();

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.01 );
NumContracts = 1;
PositionSize = NumContracts * MarginDeposit;
slip = TickSize * 2;
BuyPrice = BuyPrice + slip;
SellPrice = SellPrice - slip;
Short = Sell;
ShortPrice = SellPrice;
Cover = Buy;
CoverPrice = BuyPrice;

uptrend1 = Flip( Buy, Sell );
dntrend1 = Flip( Sell, Buy );
uptrend = Ref( Flip( tr, pk ), -1 );
dntrend = Ref( Flip( pk, tr ), -1 );

mboxdnlowArray = IIf( tr AND dntrend, Min( O, C ), null );
mboxdnlowArray = IIf( dntrend AND !tr, ValueWhen( tr, Min( O, C ), 0 ), mboxdnlowArray );
mboxdnhighArray = IIf( dntrend, HighestSince( Ref( pk, -1 ), Max( O, C ) ), null );
mboxuphighArray = IIf( pk AND uptrend, Max( O, C ), null );
mboxuphighArray = IIf( uptrend AND !pk, ValueWhen( pk, Max( O, C ), 0 ), mboxuphighArray );
mboxuplowArray = IIf( uptrend, LowestSince( Ref( tr, -1 ), Min( O, C ) ), null );

Plot( ZigZag, "", IIf( uptrend1, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine, Null, Null, 0, 1, 1 );
Plot( IIf( !IsEmpty( line1 ), line1, Null ), "", ColorRGB( 255, 255, 0 ), styleLine, Null, Null, 0, 2, 1 );
PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

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;

GfxSetZOrder( -1 );
GfxSetCoordsMode( 1 );

if( mboxswitch )
{
    ft = "Arial Black";
    clr = colorDefault;

    for( i = fvb; i <= lvb; i++ )
    {
        if( pk[i] )
        {
            c1 = 0;
            c2 = 0;
            c3 = 80;
            x0 = Nz( tx1[i] + 0.5 );
            x1 = Nz( px1[i] + 0.5 );
            yh = Nz( mboxuphighArray[i] );
            yl = Nz( mboxuplowArray[i] );
            GfxSelectPen( ColorRGB( c1, c2, c3 * 3 ), 2 );
            GfxSelectSolidBrush( ColorRGB( c1, c2, c3 ) );
            GfxRoundRect( x0, yh, x1, yl, 0, 0 );
        }

        if( tr[i] )
        {
            c1 = 80;
            c2 = 0;
            c3 = 0;
            x0 = Nz( px1[i] + 0.5 );
            x1 = Nz( tx1[i] + 0.5 );
            yh = Nz( mboxdnhighArray[i] );
            yl = Nz( mboxdnlowArray[i] );
            GfxSelectPen( ColorRGB( 3 * c1, c2, c3 ), 2 );
            GfxSelectSolidBrush( ColorRGB( c1, c2, c3 ) );
            GfxRoundRect( x0, yh, x1, yl, 0, 0 );
        }
    }
}

if( plottrailswitch )
{
    Plot( trailarrayup, "trail up", colorLightBlue, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
    Plot( trailarraydn, "trail dn", colorOrange, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
    PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorLightBlue, 0, L, -15 );
    PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
    PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), colorYellow, 0, BuyPrice, 0 );
    PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), colorYellow, 0, SellPrice, 0 );
}

ft = "Arial Black";
clr = colorDefault;

if( labelsswitch )
{
    str1 = "";

    for( i = fvb; i <= lvb; i++ )
    {
        if( pk[i] )
        {
            if( dt[i] )
                str1 = "DT";
            else
                if( hh[i] )
                    str1 = "HH";
                else
                    if( lh[i] )
                        str1 = "LH";

            PlotTextSetFont( str1, ft, sz, i, H[i], colorRed, clr, sz * 3 );
            PlotTextSetFont( "" + prch[i], ft, sz, i, H[i], colorRed, clr, sz * 5 );
        }

        if( tr[i] )
        {
            if( db[i] )
                str1 = "DB";
            else
                if( ll[i] )
                    str1 = "LL";
                else
                    if( hl[i] )
                        str1 = "HL";

            PlotTextSetFont( str1, ft, sz, i, L[i], colorGreen, clr, -sz * 4 );
            PlotTextSetFont( "" + prcl[i], ft, sz, i, L[i], colorGreen, clr, -sz * 6 );
        }
    }
}
2 Likes