Title: Re: [amibroker] Re: ZigZag code
Thanks you for sharing this Brian!
Re formatting, prettify worked nice for me ;-)
herman
Friday, June 26, 2009, 9:37:42 PM, you wrote:
> Hi
> Here is the updated code as promised. It corrects several things that I
> believe were errors in the original. All comments welcomed.
> Brian
> PS It hasn't copied across too well from Amibroker, so editing line
> returns might be required!
> // Zig Zag Code
> P = Param("Percent",3,1,5,0.01);
> WavePcnt = (1+0.01*P);
> Start_Bar_HL = 0;
> End_Bar_HL = 0;
> Start_Val_HL = Avg[0];
> End_Val_HL = Start_Val_HL;
> Oldbar = Start_Bar_HL;
> Oldval = Start_Val_HL;
> ZigArray_HL = Null;
> HiFlag = 0;
> LoFlag = 0;
> interp = 0;
> Hs_cnd = Ref(H,-1) <= H AND H >= Ref(H,1);
> Ls_cnd = Ref(L,-1) >= L AND L <= Ref(L,1);
> // Hs_Swing = Hs_cnd AND !Ls_cnd;
> // Ls_Swing = !Hs_cnd AND Ls_cnd;
> //-------------------------------------------------------------------
> // This bit checks whether candidate Hi and Low occur on the same bar
> // The Hi is allowed if the short term MA is rising & vice versa
> // Remove and re-instate the previous 2 statements if not required
> for (k=0;k<BarCount;k++)
> {
> m = MA(C,5);
> if (Hs_cnd[k] AND Ls_cnd[k])
> {
> if(m[k] > m[k-1]) Ls_cnd[k] = 0;
> else Hs_cnd[k] = 0;
> }
> }
> Hs_Swing = Hs_cnd;
> Ls_swing = Ls_cnd;
> //-------------------------------------------------------------------
> for (i=0; i < BarCount; i++)
> {
> if(Hs_Swing[i])
> {
> interp = 0;
> End_Bar_HL = i;
> End_Val_HL = H[i];
> // First potential swing is a high or this is a valid swing above the previous low
> if((!Loflag AND !Hiflag) OR (Loflag AND H[i] >= Start_Val_HL*WavePcnt))
> interp =1;
> if (Hiflag AND H[i] > Start_Val_HL)
> // We already have a Hi but this is higher, so revert to previous start conditions
> {
> interp =1;
> Start_Bar_HL = Oldbar;
> Start_Val_HL = Oldval;
> }
> // Now calculate Zig Array and initialise for next calc
> if(interp ==1)
> {
> for (j = Start_Bar_HL; j<End_Bar_HL;j++)
> {
> Zigarray_HL[j] = Start_Val_HL + (j-Start_Bar_HL)
> *(End_Val_HL - Start_Val_HL)/(End_Bar_HL - Start_Bar_HL);
> }
> Hiflag = 1;
> Loflag = 0;
> Oldbar = Start_Bar_HL;
> Oldval = Start_Val_HL;
> Start_Bar_HL = End_Bar_HL;
> Start_Val_HL = End_Val_HL;
> }
> }
> if(Ls_Swing[i])
> {
> interp = 0;
> End_Bar_HL = i;
> End_Val_HL = L[i];
> // First potential swing is a low or this is a valid swing below the previous high
> if((!Loflag AND !Hiflag) OR (Hiflag AND L[i] <= Start_Val_HL/WavePcnt))
> interp = 1;
> if (Loflag AND L[i] < Start_Val_HL)
> // We already have a Lo but this is lower, so revert to previous start conditions
> {
> interp = 1;
> Start_Bar_HL = Oldbar;
> Start_Val_HL = Oldval;
> }
> // Now calculate Zig Array and initialise for next calc
> if (interp ==1)
> {
> for (j = Start_Bar_HL; j<End_Bar_HL;j++)
> {
> Zigarray_HL[j] = Start_Val_HL + (j-Start_Bar_HL)
> *(End_Val_HL - Start_Val_HL)/(End_Bar_HL - Start_Bar_HL);
> }
> Hiflag = 0;
> Loflag = 1;
> Oldbar = Start_Bar_HL;
> Oldval = Start_Val_HL;
> Start_Bar_HL = End_Bar_HL;
> Start_Val_HL = End_Val_HL;
> }
> }
> }
> Z = ZigArray_HL;
> Plot(C,"Close",1,128);
> Plot(Z,"ZigZagHL",4,1);
> --- In amibroker@xxxxxxxxxxxxxxx, "brianw468" <wild21@xxx> wrote:
>> Hi
>> I am interested in this function also, but on inspection found a few things that I believe are errors. When I applied it to a test data set it missed a few valid highs and lows.
>> Looking at the code I saw that when testing for a swing High it (correctly) tests whether this is > End_Val_HL*WavePcnt.
>> However when testing for a swing Low it simply replaces the > sign with a < sign, whereas the test should be < End_Val_HL/WavePcnt.
>> There are also a few peculiar issues, like using Ref(H,0) where simply H would do, and setting a for loop between i=0 and i<Barcount, and then later testing whether i>0 and i<Barcount, where the condition could not occur anyway if i = 0. However, these don't affect the answer.
>> I am working on another version (hopefully correct!) and will post this when complete.
>> Brian
>> --- In amibroker@xxxxxxxxxxxxxxx, Thomas Ludwig <Thomas.Ludwig@> wrote:
>> >
>> > Thanks for sharing this code. It's seems not quite identical with the built-in
>> > function, though.
>> >
>> > Regards
>> >
>> > Thomas
>> >
>> > On 19.06.2009, 20:37:04 Herman wrote:
>> > > I think this was posted by Dave, a long time ago:
>> > >
>> > > function ZigZagHL(prcnt)
>> > > {
>> >
>> > [snip]
>> >
> ------------------------------------
> **** IMPORTANT PLEASE READ ****
> This group is for the discussion between users only.
> This is *NOT* technical support channel.
> TO GET TECHNICAL SUPPORT send an e-mail directly to
> SUPPORT {at} amibroker.com
> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> http://www.amibroker.com/feedback/
> (submissions sent via other channels won't be considered)
> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> http://www.amibroker.com/devlog/
> Yahoo! Groups Links
> <*> To visit your group on the web, go to:
> http://groups.yahoo.com/group/amibroker/
> <*> Your email settings:
> Individual Email | Traditional
> <*> To change settings online go to:
> http://groups.yahoo.com/group/amibroker/join
> (Yahoo! ID required)
> <*> To change settings via email:
> mailto:amibroker-digest@xxxxxxxxxxxxxxx
> mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx
> <*> To unsubscribe from this group, send an email to:
> amibroker-unsubscribe@xxxxxxxxxxxxxxx
> <*> Your use of Yahoo! Groups is subject to:
> http://docs.yahoo.com/info/terms/
__._,_.___
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
__,_._,___
|