Actual Turning Points of ZIG

Hi guys,
I'm looking for some advice, and not sure if this is possible. So I have open a discussion regarding this topic. I would like to plot the zig formula with its actual turning points, rather than when zig turns ( to your percentage, you give it in the formula), and reverts back to its last previous high or low. I hope this makes sense. The reason for this is this. when you code for example the line to be down, that is all great, BUT, when the zig eventually turns, all the bars that were previously down will disappear, as the ZIG turning point reverts back to a previous low point. So I would like to see, when zig turns (to your %), those actual turning points.

MA20        = MA(C,20);
MA20ZIG     = Zig(MA20,0.1);
Plot(MA20,"20ma ",colorBlue,styleDashed,0,0,0,0,2);
Plot(MA20ZIG,"20ma zig",colorYellow,styleLine,0,0,0,0,5);``

![ZIG|690x285](upload://h3qwRJ3CTkPKBdbA3kEWAKUojSy.png) 

In this example, the Yellow is the zig and the Blue is the actual 20MA.
The red is a drawn in line of guessing, just to illustrate this point, of what it could look like. Any thoughts/advice on this would be greatly appreciated.
Thanks.

Corrected your uploaded image.

Are you looking to shift the Yellow Zig line a certain number of bars in the future ?

This part isn't clear.

and this too!

ZIG

Hi, sorry. I hope this makes it clearer. I have done a bar replay on this. The right Image is when the zig is down. Move bar replay 1 bar forward, there is a turning point in zig, this is the left image. The square is 7 bars long. As you can see, like 6 bars have now turned up, when they were previously down. I would like to keep them down, and from the point of zig turn, have those bars up. Make sense? These bars were always down, but zig reverts back to a low point and the bars after that are now all up. I would like to see the bars down as that is the way it was, and when zig does turn up, then we can have up bars going forward.

Read about Zig and Zig-Zag indicator. This is the result of looking into the future or lookahead.

Caveat: this function is based on Zig-Zag indicator and may look into the future

Once you familiarize yourself, you need to code taking care of future leaks if they arise.

I'm not sure @travick is understanding what I'm trying to do. I understand the lookahead of zig and its issues. Forgive me @travick, if perhaps I may be completely losing my mind here....hahaha. But all I'm trying to do is, find out how to plot zig in a way that when zig actaully turns, that is the actual turning point in the plot, not revert back to previous highs/lows. So I have plotted a red zig with a delay of 8 bars, this seems to be correct in what I'm trying to achieve. But this is not the right way to code this, this example just shows what I'm trying to do. because if you change the zig % the ref formula will be inaccurate.
So yellow is zig and red is the zig delayed by 8 bars.zig

Assuming I understand you want to detect a change in direction of the prevailing Zig Line

You can take two data points from the straight Zig Line and project it using LineArray() or pure geometrical y = mx + c, have a threshold at each newly added bar and detect a deviation larger than a predefined threshold.

you could only plot the Zig line once the pivots are known, like:

perc = Param( "ZigZag Amount (%)", 5, 0.1, 20, 0.1 );
bi = BarIndex();

pk = PeakBars( C, perc ) == 0;
tr = TroughBars( C, perc ) == 0;
zg = Zig( C, perc );

VarSet( "px1", ValueWhen( pk, bi, 1 ) );
VarSet( "tx1", ValueWhen( tr, bi, 1 ) );

zg = IIf( LastValue( px1 ) > LastValue( tx1 ) && bi > LastValue( px1 ), Null, zg );
zg = IIf( LastValue( px1 ) < LastValue( tx1 ) && bi > LastValue( tx1 ), Null, zg );

SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
Plot( zg, "Zig", colorYellow, styleLine, Null, Null, 0, 0, 2 );
PlotShapes( shapeSmallcircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallcircle* pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

Why are you using

VarSet( "px1", ValueWhen( pk, bi, 1 ) );
VarSet( "tx1", ValueWhen( tr, bi, 1 ) );

when you could just write

px1 = ValueWhen( pk, bi, 1 );
tx1 = ValueWhen( tr, bi, 1 );

yes I copied it from code where I have it in a loop

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, x, i ) );
    VarSet( "tl" + i, ValueWhen( tr, x, i ) );
}
1 Like

To O.P. -

If you go to the AFL library in the Members area of the Amibroker site, look for an AFL called "ZigZagValid" circa 2004. It should provide a jumping off point for what you want. It shows when a ZZ reversal is actually valid for trading.

In the picture below there are two panes. The top is a "perfect" ZZ at 5%. The bottom shows the true reveral bars. For example, the blue line is the top of an up move in the look ahead ZZ. The bottom pane has a red dot at that point, but a red down arrow at the point the 5% reversal is known in real time. So if you wanted how the ZZ could actually be traded, you would connect the up/down arrows.

Capture

2 Likes

OP has tried to explain but there is still ambiguity or contradictory nature in the posts.

The code you have provided simply removes the latest line segment which is prone to change depending on new data.
OP on the other hand says "quoted below", but there is a clash because it is the consequence of a future bar that changes a subset of the past bars.

yes ok, maybe this is nicer.

perc = Param( "ZigZag Amount (%)", 5, 0.1, 20, 0.1 );
bi = BarIndex();

pk = PeakBars( C, perc ) == 0;
tr = TroughBars( C, perc ) == 0;
zg = Zig( C, perc );

px1 = ValueWhen( pk, bi, 1 );
tx1 = ValueWhen( tr, bi, 1 );

zg = IIf( LastValue( px1 ) > LastValue( tx1 ) && bi > LastValue( px1 ), Null, zg );
zg = IIf( LastValue( px1 ) < LastValue( tx1 ) && bi > LastValue( tx1 ), Null, zg );

lasttop = LastValue( HighestSince( tr, C ) );
lastbot = LastValue( LowestSince( pk, C ) );
lasttopidx = LastValue( ValueWhen( C == lasttop, bi ) );
lastbotidx = LastValue( ValueWhen( C == lastbot, bi ) );

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSelectPen( ColorYellow, 1, 2 );

if( LastValue( px1 ) < LastValue( tx1 ) )
{
    GfxMoveTo( LastValue( tx1 ), C[LastValue( tx1 )] );
    GfxLineTo( lasttopidx, C[lasttopidx] );
}

if( LastValue( px1 ) > LastValue( tx1 ) )
{
    GfxMoveTo( LastValue( px1 ), C[LastValue( px1 )] );
    GfxLineTo( lastbotidx, C[lastbotidx] );
}

SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
Plot( zg, "Zig", colorYellow, styleLine | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( shapeSmallcircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallcircle* pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

Hi @empottasch. Thanks very much for attempting this. I have posted a screenshot of your code and drawn what I would like it to do. Its not quite there unfortunately.
@abbruiser, I shall take a look at trying to connect the valid zig and see if its what I'm looking for. I trying to use this for trend definition. So in this example, the "trend" "zig" is down. So I may have an entry to sell while this zig is down, which is all valid until the zig will change and turn up, this valid entry at the time will be plotted, but then will disappear, because zig reverts backwards to its pivot.
Please check out the attached.ZIG

that would in my opinion change the entire ZIG function not just the end. I thought you just wanted the effect on the end of the zig function removed. Your idea will change the zig function around every pivot (if I understand it correctly). Since in your last postm 3rd graph you want to extend the line but this will also happen at all other pivots. Looks to me this will get messy. I'll leave that for others to answer

1 Like

That's what I've been thinking, it is paradoxical all along.
OP: you are looking into the future plotting the present, now you want to change that same present to alter the future.
The 6-7-8 bars etc are just a scenario, this will be worse than curve-fitting.

A better approach is to try as I suggested, define a certain number of bars that allow the line to extend in a direction.
You can then detect a change from it, and that will inevitably occur on the most recent bar, then take logical action for the next step.

Even then, backtesting wouldn't be recommended at all.

1 Like

i thought the idea was more for "visual trading" to get a sense of the trend or something like that.

so i just add the level at which the last pivot changes, since I already done that

perc = Param( "ZigZag Amount (%)", 5, 0.1, 20, 0.1 );
bi = BarIndex();

pk = PeakBars( C, perc ) == 0;
tr = TroughBars( C, perc ) == 0;
zg = Zig( C, perc );

px1 = ValueWhen( pk, bi, 1 );
tx1 = ValueWhen( tr, bi, 1 );

zg = IIf( LastValue( px1 ) > LastValue( tx1 ) && bi > LastValue( px1 ), Null, zg );
zg = IIf( LastValue( px1 ) < LastValue( tx1 ) && bi > LastValue( tx1 ), Null, zg );

lasttop = LastValue( HighestSince( tr, C ) );
lastbot = LastValue( LowestSince( pk, C ) );
lasttopidx = LastValue( ValueWhen( C == lasttop, bi ) );
lastbotidx = LastValue( ValueWhen( C == lastbot, bi ) );

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSelectFont( "Segoe UI", 9, 700, True );
ext = 3;

if( LastValue( px1 ) < LastValue( tx1 ) )
{
    GfxSelectPen( colorYellow, 1, 2 );
    GfxMoveTo( LastValue( tx1 ), C[LastValue( tx1 )] );
    GfxLineTo( lasttopidx, C[lasttopidx] );

    GfxSelectPen( colorLightOrange, 2, 1 );
    GfxMoveTo( lasttopidx, C[lasttopidx] - C[lasttopidx] / 100 * perc );
    GfxLineTo( BarCount + ext, C[lasttopidx] - C[lasttopidx] / 100 * perc );

    GfxSetTextColor( colorLightOrange );
    GfxTextOut( "" + prec( C[lasttopidx] - C[lasttopidx] / 100 * perc, 2 ), BarCount + ext, C[lasttopidx] - C[lasttopidx] / 100 * perc );
}

if( LastValue( px1 ) > LastValue( tx1 ) )
{
    GfxSelectPen( colorYellow, 1, 2 );
    GfxMoveTo( LastValue( px1 ), C[LastValue( px1 )] );
    GfxLineTo( lastbotidx, C[lastbotidx] );

    GfxSelectPen( colorLightBlue, 2, 1 );
    GfxMoveTo( lastbotidx, C[lastbotidx] + C[lastbotidx] / 100 * perc );
    GfxLineTo( BarCount + ext, C[lastbotidx] + C[lastbotidx] / 100 * perc );

    GfxSetTextColor( colorLightBlue );
    GfxTextOut( "" + prec( C[lastbotidx] + C[lastbotidx] / 100 * perc, 2 ), BarCount + ext, C[lastbotidx] + C[lastbotidx] / 100 * perc );
}

SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
Plot( zg, "Zig", colorYellow, styleLine | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( shapeSmallcircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallcircle* pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

@empottasch Thanks for the help really appreciated. I agree this would become quite messy. To avoid complicated code. This theory could be used in this way. Rather than not plotting the actual zig, is it possible to "save the actual state" of what zig was actually doing , prior to the turning point. If this is not possible, I'll look at another way to define trend. Its just zig, is really nice and neat. I'll look into plotting the "valid zig" entry code, as this might
be the easiest approach. Here is the last example of why zig needs to be plotted correctly.
@travick. I see your point about this. I just can't seem to get my head around the fact that that "scenario" is a true state, and its that true state that should be looked at. Otherwise all zig is really good for is finding pivots, support resistance levels.
Anyway check it out.

STOCH_D = StochD( 15 , 7, 7 );
 STOCH_K = StochK( 15 , 7);

Sell_cross = Cross(STOCH_D,STOCH_K);

TREND      = Zig(MA(C,20),0.1);
DOWN_trend = TREND < Ref(TREND,-1);

Sell_TRADE = Sell_cross AND DOWN_trend;

Plot(TREND,"20 MA zig",colorYellow,styleLine,0,0,0,0,5);
PlotShapes( IIf(DOWN_trend, shapeCircle, shapeNone ), colorPink, 0, HighestVisibleValue(TREND),0);
PlotShapes( IIf(Sell_TRADE, shapeSmallCircle, shapeNone ), colorBlue, 0, HighestVisibleValue(TREND),0);

ZIG

@Kevin don't you think that your screenshots are far from ideal? 80% of the picture above is just white space and the things which are important are barely visible. You can use Windows' Snipping tool or any other similar software to snip only selected parts of the screen.

3 Likes

@Milosz thanks for this. I have snipping tool, but never thought to use it here. The way I was going about providing screenshots was a complete mission to do. Thanks for the heads up.

Hi guys,
I would like to thank you all for your input on this topic, it really has been very helpful. @abbruiser, you are indeed correct. The easiest solution, to what I'm looking for is a "zigzagvalid", which I found here, for anyone who is interested.

http://www.amibroker.com/members/traders/11-2003.html

Appolgies for the major circle I have taken to get to such a simple solution, and again thank you all for your input!
If anyone has code to already to plot the zigzag trend from this link, it would be great if you could share, otherwise the the ribbon array is what I'm looking for.