[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Andrews Pitchfork



PureBytes Links

Trading Reference Links

I wrote this over the last couple of weeks and wanted to share it and ask 
for some help.

It uses peak and trough with variable lookback and variable pk/trough pct to 
plot Andrews Pitchfork
relative to the selected bar.  It seems to do a good job of plotting the 
pitchfork on a linear chart using
the linear equation for a line.

My question is:

  Is there an exponential equation that would allow plotting the pitchfork 
as a curve that appears
  as straight line on a semilog chart?

  My algebra, trig & calc courses are too far in the past to figure that out 
without a few weeks of reading
  old textbooks.

A couple of usage notes:

   The shift parameter works fine to offset the pitchfork, but it's a lot 
faster to use if you 'eyeball' the shift
   you want and type it in.  Then fine tune with the slider.  Just 
activating the slider sometimes shifts it
   clear out of sight since the hi/lo of the shift param is very wide.

   You can't put more than one on a chart.  The variables used in 
calculating the pitchforks would be overwrite
   each other.

   That actually raises a second question that I think I've seen answered in 
this forum before.  Is there an
   accessable variable Amibroker uses to add '1', '2'  etc. to the standard 
indicators?
   The pitchfork afl could be modified to use varget() & varset() with that 
to allow reuse in a chart.

Regards,

Bob.

The indicator:

// Andrews Pitchfork V.2
// 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.

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()
Zigpct1=Param("Zigpct1",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);

// The peak/trough lines will be used to determine the y coordinates
// of the pitchfork's 3 determining points.
pline1=Peak(H,Zigpct1,1);
tline1=Trough(L,Zigpct1,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];
           }
       }
   }
}

// Calculate the Pitchfork itself
tslope=0;
tline=0;
midx=0;
midy=0;
Handle=0;
Top=0;
Bot=0;

// 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]=zagy1[sbi] + (zagy2[sbi]-zagy1[sbi]) / 2;
tslope[sbi]=(midy[sbi]-zagy3[sbi]) / (midx[sbi]-zagx3[sbi]);

// Handle first
for ( j=zagx3[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
   Handle[j]=zagy3[sbi] + n*tslope[sbi] + 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]=zagy2[sbi] + n*tslope[sbi] + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=zagy1[sbi] + n*tslope[sbi] + shift;
   }
} else {
   for ( j=zagx2[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=zagy2[sbi] + n*tslope[sbi] + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      top[j]=zagy1[sbi] + n*tslope[sbi] + shift;
   }
}

// I'm using a white background, adjust to your own preferred background 
color
Hcolor=IIf(Handle==0,colorWhite,IIf(Ref(handle,-1)== 0 AND handle != 0, 
colorWhite,colorRed));
Tcolor=IIf(Top==0,colorWhite,IIf(Ref(top,-1)== 0 AND top != 0, 
colorWhite,colorRed));
Bcolor=IIf(Bot==0,colorWhite,IIf(Ref(bot,-1)== 0 AND bot != 0, 
colorWhite,colorRed));
Plot(Handle,"\nAndrews: pct="+Zigpct1+" lookback="+lookbk+" shift="+shift+" 
Handle",Hcolor,styleLine+styleDashed+styleNoRescale);
Plot(Top,"MLH",Tcolor,styleLine+styleDashed+styleNoRescale);
Plot(Bot,"MLL",Bcolor,styleLine+styleDashed+styleNoRescale); 




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Try Online Currency Trading with GFT. Free 50K Demo. Trade 
24 Hours. Commission-Free. 
http://us.click.yahoo.com/DldnlA/9M2KAA/U1CZAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> 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/