# Zigzag through connecting highs and lows?

Hi,

I am looking for a zigzag formula that connects the highs and lows via % change parameter. I have this below but doesn’t quite produce what I am after.

``` period = 10; pk=PeakBars(H,period)==0; tr=TroughBars(L,period)==0; zzHi=Zig(H,period); zzLo=Zig(L,period); Avg=(zzHi+zzLo)/2; x=IIf(pk,zzHi,IIf(tr,zzLo,IIf(Avg>Ref(Avg,-1),H,L))); zzHiLo=Zig(x,period); Plot( zzHiLo “zzhl”, colorRed, styeLine); ```

But when I do a bar playback to see how the zigzag lines are created it doesn’t seem to work quite right. For instance, say the new low is at 1.0 and the zigzag % change is set at 10%. The zigzag hasn’t yet recognized the 1.0 as a trough. Only when the current bar high hits or hits beyond 1.1 it will then say 1.0 is the trough. But this code doesn’t produce the lines as expected.

Thank you !

There is a formula in AFL on-line library (in the members only area) that does exactly what you are after.

1 Like

I have sifted through but can’t seem to find it.

http://www.amibroker.com/members/library/formula.php?id=1095 is the one I showed. It connects the highs and lows but the confirmation of ZZ peak and trough is via low (for trough) and high (for peak).

For example, you see from the illustration (http://i.imgur.com/GpPKTsA.png) that the lowest low since the confirmation of previous peak is 74.227. Current %change parameter is set at 2%. What I am looking for is if the succeeding bar High hits the 74.227 * 1.02 point then the trough is confirmed but you see from this pic that the high of current bar is indeed higher and it’s not confirmed. It’s because the Low needs to breach that level to be confirmed.

Next pic shows the confirmation when that happens. http://i.imgur.com/LAaNb5s.png

Thank you.

There was a two part series of articles in TASC many years ago in which the author created a couple of indicators that attempted to fix the problem of future leak in the ZigZag. To get the most out of the codes I would strongly suggest you read the articles.

Raftopoulos, Spyros [2002]. “Zigzag Validity,” Technical Analysis of STOCKS & COMMODITIES, Volume 20: August.

Raftopoulos, Spyros [2003]. “The Zigzag Trend Indicator ,” Technical Analysis of STOCKS & COMMODITIES, Volume 21: November.

2 Likes

The last bit of this example from the article “The Step Candle Pattern” may have HL ZigZag code you are looking for:

2 Likes

An add-on I wish to consider is plot the Fibonacci retracements onto the Zig Indicator as mentioned by @Ron
For that I need to calculate the values of the Hi & Lo as marked by the indicator. Any clues?

Dear Gurus,

could you please have a look at this code snippet:

``````_rsi_14 = RSI(14);
_rsi_zz = Zig(_rsi_14, 25);
_price_peaks = IIf((Ref(_rsi_zz, -1) < _rsi_zz) AND (Ref(_rsi_zz, 1) < _rsi_zz) AND (_rsi_14 > 50), Close, Null);

_trough_rsi = LowestSince(NOT IsNull(_price_peaks), _rsi_14, 1);
_last_peak_rsi = IIf(_trough_rsi <= ValueWhen(NOT IsNull(_price_peaks), _rsi_14, 1)*0.75,
ValueWhen(NOT IsNull(_price_peaks), _rsi_14, 1),
ValueWhen(NOT IsNull(_price_peaks), _rsi_14, 2));

Plot(_rsi_14, "RSI", colorGrey40);
Plot(_rsi_zz, "Zig-Zag", colorLime);

Plot(_last_peak_rsi, "Last Peak", colorRed);
``````

Is it safe to use the _last_peak_rsi in further calculations? Doesn't it peek into the future?

I believe it does not, but I would be grateful for confirmation.

Yes, I did reinvent the wheel. I just wanted to be sure I understand everything in this matter and to have a small exercise in the AFL programming.

2. because of `Ref(_rsi_zz, 1)` -> see Ref() -> "A positive period references "n" periods in the future"