Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: Andrews Pitchfork
Author/Uploader: Bob Johnson - bjohnson314 [at] kc.rr.com
Date/Time added: 2005-08-07 08:34:34
Origin:
Keywords: Andrews Andrew's Pitchfork
Level: semi-advanced
Flags: indicator

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

Uses Peak() & Trough() to determine 3 pivot points to use in plotting Andrews Pitchfork. Params: zig pct for peak and trough, look back before using peak/trough, no. of bars to extend pitchfork, shift to move pitchfork up/down, angle limit to filter out steep pitchforks, background and pitchfork color. This version replaces an earlier version that had some unused variables and lacked the angle limit and color params.

Formula:

// Andrews Pitchfork V3.1
// Use Peak() & Trough() to get peaks & troughs.
// This version deals with adjacent peaks and adjacent
// troughs, i.e. two peaks without an intervening trough or vice-versa.
// It goes backwards from the selected bar until it has a set of 
// peak,trough,peak or trough,peak,trough points to use for calulating
// the pitchfork.  When 2 or more peaks or 2 or more troughs are adjacent
// it only uses the last one, i.e. the rightmost one, of the series.
//
// Compared to V3.0 this version looks at the angle described by the handle
// line's logs.  It will look skip pitchforks that have angles beyond the
// angle threshhold limit param.
//
// This version plots the trendlines as exponential curves that plot as
// straight lines on an semilog chart.
//
// 07/22/2005 By Bob Johnson with invaluable help from Graham Kavanagh getting
// me pointed in the right direction on the exponential equation logic.

SetBarsRequired( 999999,999999);

bi=BarIndex();
sbi=SelectedValue(bi);

// How many bars to wait before believing the pivot.
Lookbk=Param("LookBack",5,1,200,1);

// Pct threshhold for peak() & trough()
Zigpct=Param("Zigpct",3.0,1.0,30.0,0.1,0);

// How many bars to extend the pitchfork past the selected bar.
xtsn=Param("Extension",62,1,200,1);

// Shift to move pitchfork up/down from original position one penny at time.
shift=Param("Shift",0,-2000,2000,0.01);

// Filter out cases when the angle of the median lines is too extreme,
// The loop will continue until it finds a pitchfork whose slope falls 
// between +- the Angle Limit.  Setting the angle limit to 90 effectively
// turns it off.
alimit=Param("Angle Limit",40,1,90,1);

// bkgrndcolor should match your background color.  It's used to mask the 
// parts of the pitchfork arrays outside the calculated pitchfork from view.
bkgrndcolor=ParamColor("Background Color",colorLightGrey);
pitchforkcolor=ParamColor("Pitchfork Color",colorRed);

// The peak/trough lines will be used to determine the y coordinates
// of the pitchfork's 3 determining points.

pline1=Peak(H,Zigpct,1);
tline1=Trough(L,Zigpct,1);

// Identify the pivots.

pzag1=pline1 != Ref(pline1,-1);
tzag1=tline1 != Ref(tline1,-1);

// Get the x,y coordinates of the pivots skipping adjacent
// peaks and troughs.  Go backwards from the current bar minus the lookback.

// These will hold the x,y coordinates of the pivot points in arrays at the
// sbi index.

zagx1=0;
zagx2=0;
zagx3=0;
zagy1=0;
zagy2=0;
zagy3=0;

for ( i = sbi - Lookbk, zagfnd = 0, pzagfnd = 0, tzagfnd = 0 ; i >= 0 && zagfnd
<= 3; i-- ) {
    if ( pzag1[i] || tzag1[i] ) {
        if ( pzag1[i] && NOT pzagfnd ) {
             zagfnd=zagfnd+1;
             pzagfnd=1;
             tzagfnd=0;
             if ( zagfnd == 1 ) {
                 zagx1[sbi]=i;
                 zagy1[sbi]=pline1[i];
             } else if (zagfnd == 2) {
                 zagx2[sbi]=i;
                 zagy2[sbi]=pline1[i];
             } else if (zagfnd == 3) {
                 zagx3[sbi]=i;
                 zagy3[sbi]=pline1[i];
             }
        } else if ( tzag1[i] && NOT tzagfnd ) {
             zagfnd=zagfnd+1;
             tzagfnd=1;
             pzagfnd=0;
             if ( zagfnd == 1 ) {
                 zagx1[sbi]=i;
                 zagy1[sbi]=tline1[i];
             } else if (zagfnd == 2) {
                 zagx2[sbi]=i;
                 zagy2[sbi]=tline1[i];
             } else if (zagfnd == 3) {
                 zagx3[sbi]=i;
                 zagy3[sbi]=tline1[i];
             }
        }
    }

    if ( zagfnd == 3 ) {  // Got 3 candidate peak/trough points

        echng=0;
        midx=0;
        midy=0;
        Handle=0;
        Top=0;
        Bot=0;

        // Determine Midpoint between the rightmost 2 pivots and the slope from
the
        // leftmost pivot to the midpoint.

        Midx[sbi]=zagx2[sbi] + (zagx1[sbi]-zagx2[sbi]) / 2;
        Midy[sbi]=exp( log(zagy1[sbi]) - ( log(zagy1[sbi])-log(zagy2[sbi]) ) /
2);
        echng=(log(midy[sbi])-log(zagy3[sbi]))/(midx[sbi]-zagx3[sbi]);

        // Apply the Angle Limit filter

        angle_rad = atan(echng);//radians
        angle_deg = 100 * angle_rad * 180/3.1416;//degrees

        if ( angle_deg < -alimit || angle_deg > alimit ) { // Too steep, reset
the search
                                                           // to begin from the
2nd pivot found
            if ( tzagfnd == 1 ) {  // was tr,pk,tr so switch to pk,tr,pk
                tzagfnd = 0;
                pzagfnd = 1;
                zagfnd = 1;
                zagx1[sbi]=zagx2[sbi];
                zagy1[sbi]=zagy2[sbi];
                i = zagx1[sbi];
                zagx2=0;
                zagx3=0;
                zagy2=0;
                zagy3=0;
            } else {  // was pk,tr,pk so switch to tr,pk,tr
                tzagfnd = 1;
                pzagfnd = 0;
                zagfnd = 1;
                zagx1[sbi]=zagx2[sbi];
                zagy1[sbi]=zagy2[sbi];
                i = zagx1[sbi];
                zagx2=0;
                zagx3=0;
                zagy2=0;
                zagy3=0;
            }        }
    }
}

// Calculate the Pitchfork itself

// Handle first

for ( j=zagx3[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
   Handle[j]=exp(log(zagy3[sbi]) + n*echng) + shift;
}

// High & low median lines.

if ( zagy2[sbi] > zagy1[sbi] ) {  // Which one is top?
   for ( j=zagx2[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      top[j]=exp(log(zagy2[sbi]) + n*echng) + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=exp(log(zagy1[sbi]) + n*echng) + shift;
   }
} else {
   for ( j=zagx2[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=exp(log(zagy2[sbi]) + n*echng) + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      top[j]=exp(log(zagy1[sbi]) + n*echng) + shift;
   }
}

Hcolor=IIf(Handle==0,bkgrndcolor,IIf(Ref(handle,-1)== 0 AND handle != 0,
bkgrndcolor,pitchforkcolor));
Tcolor=IIf(Top==0,bkgrndcolor,IIf(Ref(top,-1)== 0 AND top != 0,
bkgrndcolor,pitchforkcolor));
Bcolor=IIf(Bot==0,bkgrndcolor,IIf(Ref(bot,-1)== 0 AND bot != 0,
bkgrndcolor,pitchforkcolor));
Plot(Handle,"\nAndrews: pct="+Zigpct+" lookback="+lookbk+" shift="+shift+"
alimit="+alimit+" Angle="+angle_deg+"
Handle",Hcolor,styleLine+styleDashed+styleNoRescale);
Plot(Bot,"MLL",Bcolor,styleLine+styleDashed+styleNoRescale);
Plot(Top,"MLH",Tcolor,styleLine+styleDashed+styleNoRescale);

Comments:

cs

2006-01-15 20:34:24
How to add parallel lines to it?
Bob Johnson
bjohnson314 [at] kc.rr.com
2006-02-09 20:41:27
What parallel lines are looking for? The median lines (Handle, MLH & MLL) are parallel on a log chart.
HussainK
hk626 [at] yahoo.com
2006-02-15 01:29:00
hi
i get this error when i apply this Formula :
Error Message :-------------------------
Line 29, Column 42:
Lookbk=Param("LookBack",5,1,200,1);
// Pct threshhold for peak() & trough()
Zigpct=Param("Zigpct",3.0,1.0,30.0,0.1,0);
-----------------------------------------^
Error 11.
Too many arguments
-----------------------------------
what to do ?
Best regards
tm
afinata [at] gmail.com
2006-02-18 17:42:49
bob
I suppose he is looking for parallel lines to the central line at fib numbers distance (as in esignal)

regards
Bob Johnson
bjohnson314 [at] kc.rr.com
2006-02-20 20:07:19
HussainK, I can't duplicate the error. I suspect you've got either got a cut-and-paste error if you didn't download the formula or a very old version of Amibroke. This should work with V4.70 and above.
Bob Johnson
bjohnson314 [at] kc.rr.com
2006-02-20 20:11:24
tm, I don't have esignal so I don't know what they do. It would not be difficult to add parallel lines at 23.6%, 38.2%, etc. from the handle to the median lines. Is that what esignal does?
majed
majed [at] rainpath.com
2007-11-03 18:49:06
i got errors , must of them is a syntax error ,

i'm using vr 5.0


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM