Higher lows lower highs

Can you help this code for Amibroker ?

To find 3 Lower highs and 3 higher lows alternatively for 30 period.

First appears H1 than L1 than H2 than L2 than H3 than L3 such that H1>H2>H3 (Lower highs) and L1<L2<L3 (Higher Lows)

Set H1=1st highest high Value, set H2= 2nd highest of high value, Set H3= 3rd highest of high Value like wise Set L1=Lowest low value, same way L2, L3. and if close above H3 than buy.

And it should Plot the lines Joining H1,H2,H3 & L1,L2,L3.

Following code gives no results, ??

d1min=10; d1max=30)

for (d1=d1min;d1<d1max;d1++)
{

h1=HHV(H,d1max);
hd1=HHVBars(h1,d1max)-1;
l1=LLV(L,hd1);
ld1=LLVBars(l1,hd1)-1;
h2=HHV(H,ld1);
hd2=HHVBars(H,ld1)-1;
l2=LLV(L,hd2);
ld2=LLVBars(L,hd2)-1;
h3=HHV(H,ld2);
hd3=HHVBars(H,ld2)-1;
l3=LLV(L,hd3);
ld3=LLVBars(L,hd3)-1;

Cond1 = ((h1>h2>h3) AND (l3>l2>l1));//Allows for horizontal top or pennant

//Cond2 ensures triangle shape
Cond2 =(hd1>ld1 AND ld1>hd2 AND hd2>ld2 AND ld2>hd3 AND hd3>ld3);

Buying= Cond1 AND Cond2;
Buy= Buy + Buying;

}

Pls find the code for the triangular search.

Regards

Vivith

/* 30 day
This is a scan for triangles using highest high and lowest low over a chosen period, then next high over a chosen period after these HHV and LLV. Then the order of the highs and lows are to be in alternate order. This will pick up ascending, descending and eqaul triangles. Variable d1 is the number of days to search for the last highest high, and d2 is the gap after this HH to start searching for the next HH after the first. Similarly for the lowest lows
The variables w- represent the highs, and z- are the lows. */

Filter=Close>1;
d1=30;
d2=4;

z1=HHV(High,d1);
za1=HHVBars(High,d1);
zb1=za1-d2;
z2=HHV(High,zb1);
za2=HHVBars(High,zb1);
w1=LLV(Low,d1);
wa1=LLVBars(Low,d1);
wb1=wa1-d2;
w2=LLV(Low,wb1);
wa2=LLVBars(Low,wb1);

aa1=LastValue(High);
aa2=LastValue(Low);

Buy= ((z1>=z2 AND w2>w1) OR(z1>z2 AND w2>=w1)) AND za1>za2 AND wa1>wa2 AND ((za1>wa1 AND wa1>za2 AND za2>wa2) OR (wa1>za1 AND za1>wa2 AND wa2>za2)) AND aa1<z2 AND aa2>w2 AND Ref(Volume,-za1) > MA(Volume,d2) AND Ref(MA(Close,d1),-za1) > Ref(MA(Close,d1),-2*za1) AND MA(Close,d1) > Ref(MA(Close,d1),-2*za1); 

NumColumns = 8;

Column0 = z1;
Column1 = z2;
Column2 = w1;
Column3 = w2;
Column4 = za1;
Column5 = za2;
Column6 = wa1;
Column7 = wa2;