Zigzag Osc - The Best Way to Sense How Close We are to Direction Change

We know Zigzag is very good if it is used in association with some other indicator. However, the greatest challenge of the Zigzag moment is to locate the direction change correctly. I was going through a topic discussed here:

Here they have mentioned that When Zigzag Trend is on an uptrend it can be very easily detected if Zig Zag Oscillator will be above 0. Similarly, when the Zig Zag is in a downtrend state, the oscillator will be below 0. So applying this oscillator very easily we can conclude how close we are to the minimum or maximum point since it calculates the percent change of the current price (close) from the previous pivot price.
Is it possible to make this indicator in the Amibroker Platform?
I could not find any detailed code regarding this oscillator so that I can understand the detailed logic.
L1

I think we can make o signal in this way?
Oscillator= 100 * (Close - Last Close value of Ongoing ZZ) / Last Close value of Ongoing ZZ

I think you can have some major fun attempting to code your thought. Go for it!

I tried - but that simple formula will not produce that histogram - some other hidden concepts are there. I think here Experts can understand that.

I just spent the past 12 hours attempting to find a problematic issue in some of my work. After discovering (and fixing the problem) this is the best I've felt in 12 hours.

Seriously, you are going to find great gratification in developing your ideas!

1 Like

Yes the concept is very unique. Because in market entry is easy but exit is 1000times tough. So logically this approach is very scientific - monitoring the gradual drop of the the gap between previous and present.

First task: read all of the Amibroker documentation 1000 times!
(most of the "nuggets" are somewhat hidden...lol).

You are most correct, in that getting into the market is easy. Don't you find it interesting that most commentary (and instructional docs) are primarily focused on the entry?

if you use the Zig function from Amibroker then the peaks and trough can be calculated using

perc = Param( "Percent (%)", 2, 0.1, 20, 0.05 );

zz = Zig( C, perc );
tr = Trough( C, perc, 1 );
pk = Peak( C, perc, 1 );
pk = zz == pk;
tr = zz == tr;

then the ZZO oscillator can be calculated as follows:

// https://www.linnsoft.com/techind/zig-zag-oscillator-indicator-zzo
BASE = ValueWhen( pk OR tr, C );
ZZO = 100 * SafeDivide( C - BASE, BASE );
clr = IIf( ZZO > Ref( ZZO, -1 ), colorBlue, colorRed );
Plot( ZZO, "", clr, styleHistogram | styleOwnScale, Null, Null, 0, 0, 4 );
3 Likes

Thanks, Edward for your valuable help. Now Applying your previous code of Zigzag and the this zigzag oscillator it is very convenient to track the overall trend.
Leg

Let me think if it can be developed more or not and I will ask your expert opinion.

if you apply it to this ZigZag code:

then you could better use:

// https://www.linnsoft.com/techind/zig-zag-oscillator-indicator-zzo
BASE = IIf( BarsSince( pk ) < barssince( tr ),  ValueWhen( pk, prch ), ValueWhen( tr, prcl ) );;
//Plot( BASE, "", colorWhite, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
numerator = IIf( BarsSince( pk ) < barssince( tr ), prch - ValueWhen( pk, prch ), prcl - ValueWhen( tr, prcl ) );
ZZO = 100 * SafeDivide( numerator, BASE );
clr = IIf( ZZO > Ref( ZZO, -1 ), colorBlue, colorRed );
Plot( ZZO, "", clr, styleHistogram | styleOwnScale, Null, Null, 0, 0, 4 );
2 Likes

Yes now it is looking good since in your code two functions prch & prcl (trend switch) were not initialized I added those two
Leg .

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

But I think if these four concepts be introduced then it will be more tunable through parameter. I may be wrong. Actually, I am trying to learn the codes slowly slowly since I am completely non-technical person.

ZigZag Depth- how far back in the chart bar series it will look.
ZigZag Deviation - percentage deviation before it reverses the trend and a Zig becomes a Zag
ZigZag Backstep-the minimal amount of bars between swing highs and lows.
ZigZag pivot point - pivot point to start calculating the distance from
If the pivot point is equal to zero, then the counting will be started from the nearest left ZigZag break or from the end of its last ray.

Hi Edward!

Here % of Deviation before turning the direction is very relative. I think only by looking back it can understand the oscillation will exactly start or end at which point. However, just for experimental purpose, I used your codes (with a loop) from a zig-zag chart to select pk and tr in place of what you have used above

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;
pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

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;

And I think I got a better result with tuning in the parameter section. Just look here for your expert opinion
Leg

See using your loop code less fluctuation is happening - obviously after tuning

yes, if I understand you correctly there are some issues at the end. As for the first version using the Amibroker ZigZag there the last pivot appears only when the last percentage threshold is exceeded. So then you all of a sudden see a jump in the ZZO indicator. With my ATR version there are also issues because I start drawing potential pivots but their location/position may change as more data comes in. So both versions have issues. I am not sure how linnsoft tackles this problem, Because with these pivots you always only can know their true position after the fact. You can not know the position of the pivot in real time. At least I never figured it out :smiley: If I only knew their position in real time trading would be so much more fun.

Absolutely true. When you will use Amibroker's default functions you will get more fluctuation since that is taking care % change till the threshold. Again when I am using your ATR based approach (potential pivot) - it may repaint with new data.
Today I was going through an interesting discussion on ZZO and Delta. I think this approach is pretty good..I understood their concept completelybut I am not so efficient to express it in terms of code. But how to apply it for Equity market?
You can check it out: Zig Zag Oscillator and Delta Since Last Pivot | Linn Software
"Delta Since Last Pivot" for previous pivots | Linn Software

@empottasch ...Now I got the point ...just look at this video carefully - they are using two pannels - one is customised with real ZZO and another one with refreshed values.

Amibroker refreshes all bars for every calculation, so we do not have that problem. But it doesn't matter. I see Linnsoft also has the problem my version has. Basically it is just a nonsense indicator if you ask me. It will repaint in the real time area of the chart. There is nothing you can do about it because you just will never be able to define a pivot in real time.

He also says the ZigZag is used for market structure. You can use it to find patterns or use confirmed pivots (which will no longer change their position/location) from the past as a reference to real time prices.

Zig Zag by definition looks into the future and as such it is USELESS, unless you address this basic shortcoming.

The only safe ZigZag is when you WAIT for last leg to be greater than defined "threshold". This technique was presented long time ago (18 years ago), including AFL code

http://traders.com/Documentation/FEEDbk_docs/2003/11/TradersTips/TradersTips.html#amibroker

3 Likes

Yes. The same problem they are also getting. It is better to use it for EoD purpose with patten recognition.
I think that problem will never end. It is better to use your past codes where repaint will not happen.

zz = Zig( C, perc );
tr = Trough( C, perc, 1 );
//==========================================================//
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightstrength = Param( "Right Strength", 5, 1, 50, 1 );
leftstrength = Param( "Left Strength", 5, 1, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;
pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

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;
//=====================================================//

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

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.