BullCharts Forum Homepage
Forum Home Forum Home > BullCharts > BullScript
  New Posts New Posts RSS Feed: Zig Zag indicator
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Zig Zag indicator

 Post Reply Post Reply Page  12>
Author
Message Reverse Sort Order / View First Unread Post
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Topic: Zig Zag indicator
    Posted: 22 Apr 2011 at 7:07pm

Here's versions suitable for traders that prefer the labels Higher High & Lower Low.

Scan for Higher Lows also included.   Looking at AWC you can see it will find these.

ZigZag - Highs & Lows Indicator

[target=Price]
size := input("Zig zag size",5,0);
z := zigzag(Close,size,%);
ispeak := z>hist(z,1) and z>future(z,1);
istrough := z<hist(z,1) and z<future(z,1);
P1 := ValueWhen(1,ispeak,z);
P2 := ValueWhen(2,ispeak,z);
Higher := if(ispeak and P1 > P2,"Higher","Lower");
T1 := ValueWhen(1,istrough,z);
T2 := ValueWhen(2,istrough,z);
Lower := if(istrough and T1 > T2,"Higher","Lower");
[name=LowerPeak]
ScanLowerPeak := if(ispeak and P1 < P2,1,0);
[name=HigherPeak]
ScanHigherPeak := if(ispeak and P1 > P2,1,0);
[name=LowerTrough]
ScanLowerTrough := if(istrough and T1 < T2,1,0);
[name=HigherTrough]
ScanHigherTrough := if(istrough and T1 > T2,1,0);

[linestyle=Solid; color=black]
z;
[color=Blue]
[linestyle=Text; textalign=Above,Center]
if(isPeak, Higher + " High at " + z, undefined);
High;
[linestyle=Text; textalign=Below,Center]
if(isTrough, Lower + " Low at " + z, undefined);
Low;

.....................................................................

ZigZag - Higher Low signal for scanning

[Description="ZigZag Higher Low Signal"]
size:=input("ZigZag percentage",5,1);
z := zigzag(Close,size,%);
istrough:= ref(Z,-1)<z AND ref(z,-2)>ref(Z,-1);
T1:=ValueWhen(1,istrough,z);
T2:=ValueWhen(2,istrough,z);
HigherLow:=istrough and T1 > T2;
[name=HigherLow]
HigherLow



Edited by maximo - 23 Apr 2011 at 2:50am
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 21 Apr 2011 at 11:04pm

Hey Andre,

I know whats causing the problem with the scan.   The use of future() is not appropriate for scans, since it is looking forward in data for the peak, which doesn't make sense for a scan
into future data, ie. not possible.   Reserve it only for plotting labels or markers on the correct
bar for a turning point into the future.
 
Tested this code for a scan and it works using the following parameters.
 
1. First Value to Compare
Is an indicator
ZigZag Higher Peak Signal
 
2. Type of Comparison
Is greater than
Any bar in the last 3 bars
 
 

[Description="ZigZag Higher Peak Signal"]

size:=input("ZigZag percentage",5,1);

z := zigzag(Close,size,%);

ispeak:= ref(Z,-1)>z AND ref(z,-2)<ref(Z,-1);

P1:=ValueWhen(1,ispeak,z);

P2:=ValueWhen(2,ispeak,z);

HigherPeak:=ispeak and P1 > P2;

[name=HigherPeak]

HigherPeak

 


Edited by maximo - 22 Apr 2011 at 12:03am
Back to Top
Andrzej View Drop Down
Newbie
Newbie


Joined: 01 Feb 2006
Posts: 9
Post Options Post Options   Quote Andrzej Quote  Post ReplyReply Direct Link To This Post Posted: 20 Apr 2011 at 11:47pm
Hi Maximo,
I tested above program in whole and with some modifications. First thing, the scan run returing most records ( let say 293 out of 295), displays total 4 columns but labelled them Lower Trough , 2 columns don't have data.
But , good news is that custom indicator created without any modification displays on charts what suppose to display.
The scan however is a different story. Below is a restricted indicator called Higher Peaks that I wanted to use in scan either as : "Is an indicator" , or "Is an indicator signal " in order to get only records that have Higher Peaks in let say a few weeks: Scan script:
  "HigherPeak is true any bar in the last 10 bars "
Visual chart inspection shows stock that satisfy this criterion but the scan using custom indicator called Higher Peak does not return any records and quite often the scan crash ( seems does not like this syntax). Any ideas or suggestions for what may be a problem this time :-) ?
Custom indicator Higher Peaks has following code :

[target=Price]

size:= 8;

z := zigzag(Close,size,%);

ispeak := z>hist(z,1) and z>future(z,1);

P1 := ValueWhen(1,ispeak,z);

P2 := ValueWhen(2,ispeak,z);

Higher := if(ispeak and P1 > P2,"Higher",undefined);

[name=HigherPeak]

ScanHigherPeak := if(ispeak and P1 > P2,1,0);

ScanHigherPeak;

[linestyle=Solid; color=black]

z;

[color=Blue]

[linestyle=Text; textalign=Above,Center]

if(isPeak, Higher + " Peak at " + z, undefined);

High;

[name=HigherPeak; linestyle=marker; marker=type1; ];

ScanHigherPeak and not hist(ScanHigherPeak,1);

 

 

 
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 19 Apr 2011 at 6:12pm
Hi Andre
 
This indicator will show that this one is working. Let me know if the scan is working.
 
 

[target=Price]

size := input("Zig zag size",4,0);

z := zigzag(Close,size,%);

ispeak := z>hist(z,1) and z>future(z,1);

istrough := z<hist(z,1) and z<future(z,1);

P1 := ValueWhen(1,ispeak,z);

P2 := ValueWhen(2,ispeak,z);

Higher := if(ispeak and P1 > P2,"Higher","Lower");

T1 := ValueWhen(1,istrough,z);

T2 := ValueWhen(2,istrough,z);

Lower := if(istrough and T1 > T2,"Higher","Lower");

[name=LowerPeak]

ScanLowerPeak := if(ispeak and P1 < P2,1,0);

[name=HigherPeak]

ScanHigherPeak := if(ispeak and P1 > P2,1,0);

[name=LowerTrough]

ScanLowerTrough := if(istrough and T1 < T2,1,0);

[name=HigherTrough]

ScanHigherTrough := if(istrough and T1 > T2,1,0);

[linestyle=Solid; color=black]

z;

[color=Blue]

[linestyle=Text; textalign=Above,Center]

if(isPeak, Higher + " Peak at " + z, undefined);

High;

[linestyle=Text; textalign=Below,Center]

if(isTrough, Lower + " Trough at " + z, undefined);

Low;



Edited by maximo - 19 Apr 2011 at 10:19pm
Back to Top
Andrzej View Drop Down
Newbie
Newbie


Joined: 01 Feb 2006
Posts: 9
Post Options Post Options   Quote Andrzej Quote  Post ReplyReply Direct Link To This Post Posted: 16 Apr 2011 at 2:37pm

Thank you for your help. I have followed up the same idea in your code using ref function. Here is a complete snippet.

[target =Price]

size:= 4;

z:=zigzag(Close,size,%);

{Define using Ref function instead of hist and future }

ispeak1:= z>ref(z,-1) and ref(z,1)<ref(z,-2);

peak1:=ValueWhen(1,ispeak1,ref(C,-1));

peak2:=ValueWhen(2,ispeak1,ref(C,-1));

{Condition}filter := peak1 > peak2 ; filter;

peak1;

{Columns}

[name="IPeak1"; dp="3"] peak1;
 
However, when running scan on S&P ASX300 it returns 0 records saying: undefined filter results. I think it means that values for peak1 , peak2 are not created. The last line [name="IPeak1"; dp="3"] peak1 should return value for peak1, but it doesn't. Variable z is created and can be seen in scan.
Are you able to retest the snippet and get values peak1/peak2 and some records on Weekly or daily data please ?
thank you Maximo for you time and interest.
Andrzej
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 13 Apr 2011 at 1:15am
Hey Andrzej
 
This indicator uses ValueWhen to save previous values of ZigZag, RSI & Close to measure divergence.  Hope it helps with the coding.  I think using the future fuction in your code was a problem.
 
 

[Description="Divergence of RSI using ZigZag"]

expr := expression("Expression");

nrsi := input("RSI Time periods",14,1);

RSIval := rsi(expr, nrsi);

Zper := input("ZigZag percentage",3,1);

I1:=Zigzag(RSIval,Zper,%)>Ref(Zigzag(RSIval,Zper,%),-1) AND Ref(Zigzag(RSIval,Zper,%),1)<Ref(Zigzag(RSIval,Zper,%),-2);

{ save values 1 peak ago }

V1:=ValueWhen(1,I1,Ref(Zigzag(RSIval,Zper,%),-1));

V2:=ValueWhen(1,I1,Ref(C,-1));

{ save values 2 peaks ago }

V3:=ValueWhen(2,I1, Ref(Zigzag(RSIval,Zper,%),-1));

V4:=ValueWhen(2,I1,Ref(C,-1));

{ compare values for divergence }

C1:=V1>V3 AND V2<V4 ;

C1 AND Ref(C1,-1)=0 AND RSIval > Ref(RSIval,-1)



Edited by maximo - 13 Apr 2011 at 1:17am
Back to Top
Andrzej View Drop Down
Newbie
Newbie


Joined: 01 Feb 2006
Posts: 9
Post Options Post Options   Quote Andrzej Quote  Post ReplyReply Direct Link To This Post Posted: 10 Apr 2011 at 2:46pm
Thanks Maximo, I tried to define and create variable latest peak as follows:

ispeak1:=ValueWhen(1,(z>hist(z,1) and z>future(z,1)),z);

ispeak1:=prev(1,z>hist(z,1) and z>future(z,1));
Do you know why formula is still not working ?
Variable ispeak1 is still not created when I use code to show it:

{Columns}                                                                                                             [name="IPeak1"; dp="3"] ispeak1;

thanks
 
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 07 Apr 2011 at 2:30am
Yes previous() or prev() function is a bit tricky and needs experimentation to understand its operation.
 
Previous  same as prev(1,1)  { most recent value, ie peak }
 
prev(1,2) { second most recent value, peak before most recent}
prev(1,3) { third most recent value, peak before that.. etc. }
 
The use of ValueWhen()  eg. variable:=ValueWhen(1,test condition,save this variable if condition was true);
Just saves a value when a condition is true, so it can be used to save peak values as they occur too.
 
It seems to me both functions are the same except Prev doesn't need any condition to save any previous values.
 
 
 
 


Edited by maximo - 07 Apr 2011 at 2:38am
Back to Top
Andrzej View Drop Down
Newbie
Newbie


Joined: 01 Feb 2006
Posts: 9
Post Options Post Options   Quote Andrzej Quote  Post ReplyReply Direct Link To This Post Posted: 06 Apr 2011 at 10:01pm

Thank you maximo for the quick advice.

I replaced hist() with previous() but still can not get variable ispeak1 to be calculated.

Do I need to replace future () as well ?
I would try to use valuewhen () but not sure what parameters I need to enter .
 
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 04 Apr 2011 at 4:55pm
Hi Andrzej
 
hist() can't be used to look at previous peaks, the Previous() function must be use or ValueWhen().
 
hist() is looking back only the previous bar and not the previous peak is why it's not working.
 
 
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.69
Copyright ©2001-2010 Web Wiz