# Need help in understanding the following AFL

#### ike

##### Member
Hi,

Below is the code snippet which plots a line connecting the highs and lows and highlights the length of wave of the move. I would appreciate if someone could give an idea about the code and the logic of how the coordinates of line/wave is calculated.

Code:
``````period = param("Zig-Zag %age Change", 1, 0.1, 30, 0.1);
zz_c = paramcolor("Zig-Zag Line Color", colorblue);
zz_s = paramstyle("Zig-Zag Line Style", styleline | stylethick, maskall);
zz_w = param("Zig-Zag Line Width", 2, 1, 10, 1);

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", zz_c, zz_s, 0, 0, 0, 0, zz_w);``````
Sample output below where the blue line is plotted based on the above code.

Thanks in advance.

#### Happy_Singh

##### Happy
Calculates peaks n troughs based on the parameter "% change" that you are providing.

To understand any code . . .
Amibroker Help is a good place to start at

ZIG - zig-zag indicator Basic price pattern detection (AFL 1.1)

SYNTAX zig(ARRAY, change )

RETURNS ARRAY FUNCTION

Calculates the minimum % change Zig Zag indicator.

Caveat: this function is based on Zig-Zag indicator and may look into the future - this means that you can get unrealistic results when back testing trading system using this indicator. This function is provided rather for pattern and trend recognition formulas. EXAMPLE zig(close,5)
PeakBars - bars since peak Basic price pattern detection (AFL 1.1)

SYNTAX PeakBars(ARRAY, change, n = 1)

RETURNS ARRAY FUNCTION

Gives the number of bars that have passed from the n-th peak. This uses the Zig Zag function (see Zig Zag) to determine the peaks. n =1 would return the number of bars that have passed since the most recent peak. n =2 would return the number of bars that have passed since the 2nd most recent peak
Caveat: this function is based on Zig-Zag indicator and may look into the future. EXAMPLE peakbars(close,5,1)

.

#### ike

##### Member
Hi,

Thanks for your reply and pointer to the User Manual. I couldn't understand the exact way these functions are being used in the code above even after going through the explanations given. It would be helpful if you could elaborate a bit on the logic of the following lines.

Code:
``````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);``````
Thanks in advance.

#### lazydaze

##### New Member
Well most of the smarts are in the opaque zig function that calculates the zigzag lines linking the highs and lows in a time series.
The rest of the code just expands this to deal with the fact that each bar is a range from high to low and not just a single value per bar like Close.
Peakbars and troughbars return the number of bars to the last peak or trough bar and comparing it to zero returns true when at a peak (or trough) and zero/false otherwise. The the x = iif line just uses the peak and trough true values to choose the zzhi or zzlo or the h or l depending on the slope.

#### ike

##### Member
Thanks @lazydaze.

I basically want to use the output of ATR(14) instead of period which is a percent of the price in the current code snippet. I basically want to add the volume for the up and down wave and display it on the chart. The idea is exactly what Weis Wave does. I want to print the volume across the wave along side the HLC bar chart so as to avoid the extra pane to display this information.

#### Happy_Singh

##### Happy
Exact same thingy was recently discussed on the Amibroker forum and the solution / code provided by Ed Pottasch

.