I have a tool called ZigZag in my list of indicators. Not sure where I got it from, (Maybe like Joe said from the indicator library), but here is the code in case you can't find it:
Code: Select all
# if use_percent is true, will look for percent change
# if use_percent is false, will look for a specific point drop
# highprice = what price to use when measuring from highs
# lowprice = what price to use when measuring from lows
# if show_min_target is true, the minimum target for a reversal will be drawn
input:highprice(high),lowprice(low),percent(3.25),
color(red),thickness(1),use_percent(false),
show_min_target(true),min_target_tl_length(20);
if (barnum==barsback) {
lasthi=highprice;
lastlo=lowprice;
lasthibar=barnum;
lastlobar=barnum;
direction=1;
perc=percent/100.0;
active_tl=trendline(0,c,0,c,color);
set_tl_style(active_tl,ps_dot);
target=c;
target_tl=trendline(0,c,0,c,color);
set_tl_style(target_tl,ps_dot);
if (show_min_target==true)
target_tx=text(0,c,"target",color,tx_left,8);
}
if (use_percent==true) {
if (direction==1) {
if (highprice>lasthi) {
lasthi=highprice;
lasthibar=barnum;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lasthi*(1.0-perc);
}
if (lowprice<lasthi*(1.0-perc)) {
ref=trendline(lastlobar,lastlo,lasthibar,lasthi,color);
set_tl_size(ref,thickness);
lastlo=lowprice;
lastlobar=barnum;
direction=-1;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lastlo*(1.0+perc);
}
}
if (direction==-1) {
if (lowprice<lastlo) {
lastlo=lowprice;
lastlobar=barnum;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lasthi*(1.0+perc);
}
if (highprice>lastlo*(1.0+perc)) {
ref=trendline(lasthibar,lasthi,lastlobar,lastlo,color);
set_tl_size(ref,thickness);
lasthi=highprice;
lasthibar=barnum;
direction=1;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lasthi*(1.0-perc);
}
}
}
else #this is points
{
if (direction==1) {
if (highprice>lasthi) {
lasthi=highprice;
lasthibar=barnum;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lasthi-percent;
}
if (lowprice<=lasthi-percent) {
ref=trendline(lastlobar,lastlo,lasthibar,lasthi,color);
set_tl_size(ref,thickness);
lastlo=lowprice;
lastlobar=barnum;
direction=-1;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lastlo+percent;
}
}
if (direction==-1) {
if (lowprice<lastlo) {
lastlo=lowprice;
lastlobar=barnum;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lastlo+percent;
}
if (highprice>=lastlo+percent) {
ref=trendline(lasthibar,lasthi,lastlobar,lastlo,color);
set_tl_size(ref,thickness);
lasthi=highprice;
lasthibar=barnum;
direction=1;
#update active TL
set_tl_bar1(active_tl,lastlobar);
set_tl_price1(active_tl,lastlo);
set_tl_bar2(active_tl,lasthibar);
set_tl_price2(active_tl,lasthi);
target=lasthi-percent;
}
}
}
#update next wave targets
if (show_min_target==true) {
set_tl_bar1(target_tl,barnum+10);
set_tl_bar2(target_tl,barnum+min_target_tl_length);
set_tl_price1(target_tl,target);
set_tl_price2(target_tl,target);
text_setprice(target_tx,target);
text_setbar(target_tx,barnum+min_target_tl_length+1);
text_setstring(target_tx,To_String(target));
}