Andrews Pitchfork (540).afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:8k
源码类别:
金融证券系统
开发平台:
Others
- //------------------------------------------------------------------------------
- //
- // Formula Name: Andrews Pitchfork
- // Author/Uploader: Bob Johnson
- // E-mail: bjohnson314@kc.rr.com
- // Date/Time Added: 2005-08-07 08:34:34
- // Origin:
- // Keywords: Andrews Andrew's Pitchfork
- // Level: semi-advanced
- // Flags: indicator
- // Formula URL: http://www.amibroker.com/library/formula.php?id=540
- // Details URL: http://www.amibroker.com/library/detail.php?id=540
- //
- //------------------------------------------------------------------------------
- //
- // 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.
- //
- //------------------------------------------------------------------------------
- // 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);