Wednesday, 23 October 2019

ANDREWS Peak Trough for Amibroker (AFL)


 ANDREWS Peak Trough for Amibroker (AFL)




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

// Pct threshhold for peak() & trough()

Zigpct=Param("Zigpct",4.6,1.0,30.0,0.1,0);


zema=EMA(C,14);
zpr=IIf(ROC(zema,1) > 0,H,L);
zag=Zig(zpr,zigpct);
tr=Ref(zag,-1) > zag AND zag < Ref(zag,1);
pk=Ref(zag,-1) < zag AND zag > Ref(zag,1);
pkprice=ValueWhen(pk,zpr);
trprice=ValueWhen(tr,zpr);


pklast=IIf(BarsSince(pk) < BarsSince(tr),1,0);
trlast=IIf(BarsSince(tr) < BarsSince(pk),1,0);


pk_id=((pklast OR tr) AND pkprice*(1-(zigpct*0.01)) > L ) AND NOT ( pk AND (H - L)/L >= 0.01 * zigpct);
tr_id=((trlast OR pk) AND H > trprice*(1+(zigpct*0.01)) ) AND NOT ( tr AND (H - L)/L >= 0.01 * zigpct);


pk_id=ExRem(pk_id,tr_id);
tr_id=ExRem(tr_id,pk_id);

pk_idlast=IIf(BarsSince(pk_id) < BarsSince(tr_id),1,0);
tr_idlast=IIf(BarsSince(tr_id) < BarsSince(pk_id),1,0);

Lookbk=IIf(pk_idlast,BarsSince(pk),BarsSince(tr));


xtsn=Param("Extension",62,1,2500,1);


shift=Param("Shift",0,-2000,2000,0.01);


alimit=Param("Angle Limit",40,1,180,1);


bkgrndcolor=ParamColor("Background Color",14);
pitchforkcolor=ParamColor("Pitchfork Color",10);


pline1=pkprice;
tline1=trprice;


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



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

for ( i = sbi - Lookbk[sbi], 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;

       

        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]);

       

        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 ( (exp(log(zagy2[sbi]) + (sbi-zagx2[sbi])*echng))
     > (exp(log(zagy1[sbi]) + (sbi-zagx1[sbi])*echng)) ) {  // 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));
Htitle=EncodeColor(pitchforkcolor)
      + StrFormat("\nAndrews: pct=%g lkbk=%g shft=%g alimit=%g Angle=%3.2f Handle=",
        Zigpct, Lookbk, shift, alimit, angle_deg);
Plot(Handle,Htitle,Hcolor,styleLine+styleDashed+styleNoRescale);
Plot(Bot,EncodeColor(pitchforkcolor)+" Bot=",Bcolor,styleLine+styleDashed+styleNoRescale);
Plot(Top,EncodeColor(pitchforkcolor)+" Top=",Tcolor,styleLine+styleDashed+styleNoRescale);

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

1 comment:

  1. how to make it for filtering? buy if yellow arrow, sell if red arrow?

    ReplyDelete

Thanks