Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Alternative ZIG type function 2 for Amibroker (AFL)

Rating:
4 / 5 (Votes 3)
Tags:
amibroker

Coded in 2014, July,

Modified & improved the following to latest version (5.80 onward):
http://www.wisestocktrader.com/indic…tive-zig-type-function
AFL code by Edward Pottasch, Oct 2012

Abandon the following user defined functions:
GetVisibleBarCount()
GfxConvertPixelsToBarX()
GfxConvertPixelToValueY()

AND

to illustrate the new internal functions:
- GfxSetCoordsMode();
- PlotTextSetFont();

for better programming.

and possible reduce the Plot/PlotOHLC errors

ref. site: http://www.traderji.com/amibroker/94616-new-amibroker.html

Indicator / Formula

Copy & Paste Friendly
Version(5.80);

// Amibroker AFL code by Edward Pottasch, Oct 2012
// alternative ZIG type function based on the ATR and VSTOP functions
// added multiple timeframes. Maximum timeframe set to 1440 minutes 

// ****** Modified by Kelvinhand ******

GfxSetOverlayMode( 1 ); 
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel) 


tc=ParamList("Display Mode","ZIG|VSTOP|ZIG&VSTOP",0);
disp=ParamToggle("Display labels","Off|On",1);
tf=Param("Time Frame (min)",5,1,1440,1);tfrm=in1Minute*tf;
perBull=Param("perBull",20,1,150,1);
perBear=Param("perBear",20,1,150,1);
multBull=Param("multBull",2,0.05,4,0.05);
multBear=Param("multBear",2,0.05,4,0.05);

bi=BarIndex();


start = FirstVisibleValue( bi ); 
end   = LastVisibleValue( bi ); 



TimeFrameSet(tfrm);
function vstop_func(trBull,trBear)
{
    trailArray[0]=C[0];
    for(i=1;i<BarCount;i++)
    {
        prev=trailArray[i-1];
 
        if(C[i]>prev AND C[i-1]>prev)
            trailArray[i]=Max(prev,C[i]-trBull[i]);
        else if(C[i]<prev AND C[i-1]< prev)
            trailArray[i]=Min(prev,C[i]+trBear[i]);
        else if (C[i]>prev)
            trailArray[i]=C[i]-trBull[i];
        else
            trailArray[i]=C[i]+trBear[i];    
    }
    return trailArray;
}

trBull=multBull*ATR(perBull);
trBear=multBear*ATR(perBear);
trailArray = vstop_func(trBull,trBear);
ts=IIf(trailArray>C,trailArray,Null);
tl=IIf(trailArray<C,trailArray,Null);
TimeFrameRestore();

ts=TimeFrameExpand(ts,tfrm,expandLast);
tl=TimeFrameExpand(tl,tfrm,expandLast);
 
GraphXSpace=5;
SetChartOptions(0, chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle Up Color", colorBrightGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Shadow Up Color", colorBrightGreen),IIf(C<=O,ParamColor("Shadow Color", colorRed),colorLightGrey)),64,0,0,0,0,1);

lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll;
ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl)));
qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq;
tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception
hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh;
ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts)));
sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss;
pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception

px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

switch(tc)
{
    case("ZIG"):
        aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);aa1=IIf(pk,Ref(aa1,-1),aa1);ls1=aa1*(bi-tx1)+tl1;
        bb1=IIf(px0>tx1 AND px1<tx1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorBlue,styleLine,0,0,0,2,3);
        aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);aa1=IIf(tr,Ref(aa1,-1),aa1);ls1=aa1*(bi-px1)+ph1;
        bb1=IIf(tx0>px1 AND tx1<px1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorOrange,styleLine,0,0,0,2,3);
        break;
        
    case("VSTOP"):
        Plot(ts,"\ntrailShort",colorRed,styleLine,0,0,0,1,1);
        Plot(llls,"",colorRed,styleDashed,0,0,0,1,1);
        Plot(tl,"\ntrailLong",colorGreen,styleLine,0,0,0,1,1);
        Plot(hhhs,"",colorGreen,styleDashed,0,0,0,1,1);
        break;
        
    case("ZIG&VSTOP"):
        aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);aa1=IIf(pk,Ref(aa1,-1),aa1);ls1=aa1*(bi-tx1)+tl1;
        bb1=IIf(px0>tx1 AND px1<tx1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorBlue,styleLine,0,0,0,2,3);
        aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);aa1=IIf(tr,Ref(aa1,-1),aa1);ls1=aa1*(bi-px1)+ph1;
        bb1=IIf(tx0>px1 AND tx1<px1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorOrange,styleLine,0,0,0,2,3);
        Plot(ts,"\ntrailShort",colorRed,styleLine,0,0,0,1,1);
        Plot(llls,"",colorRed,styleDashed,0,0,0,1,1);
        Plot(tl,"\ntrailLong",colorGreen,styleLine,0,0,0,1,1);
        Plot(hhhs,"",colorGreen,styleDashed,0,0,0,1,1);
        break;
    }

    //PlotShapes(shapeSmallCircle*tr,colorGreen,0,L,-10);
    //PlotShapes(shapeSmallCircle*pk,colorRed,0,H,10);
    
    qq=Interval()/60;
    if(qq < 60){tf=" min";tt=qq;}
    else if(qq >= 60 AND qq < 1440){tf=" hrs";tt=qq/60;}
    else if(qq >= 1440){tf=" days";tt=(qq/60)/24;}
    qq=Max(tfrm/60,Interval()/60);
    if(qq < 60){tfa=" min";tta=qq;}
    else if(qq >= 60 AND qq < 1440){tfa=" hrs";tta=qq/60;}
    else if(qq >= 1440){tfa=" days";tta=(qq/60)/24;}

    Title = Name() + 
        "\nChart TF: " + tt + tf + 
        "\nZig TF: " + tta + tfa;


    if(disp)
    {
        ll=tr AND tl1<tl2;
        hl=tr AND tl1>tl2;
        hh=pk AND ph1>ph2;
        lh=pk AND ph1<ph2;
        dt=pk AND ph1==ph2;
        db=tr AND tl1==tl2;


    Yofs =10;    
    for ( i = start; i <= end; i++ ) 
    {
            
        if(ll[i]) PlotText("LL",i+2,L[i],colorWhite,colordefault, -Yofs-5 );
        if(hl[i]) PlotText("HL",i+2,L[i],colorWhite,colordefault, Yofs );
        
        if(db[i]) PlotText("DB",i+2,L[i],colorWhite,colordefault, -Yofs-5 );
        if(hh[i]) PlotText("HH",i+2,H[i],colorWhite,colordefault, Yofs );
        
        if(lh[i]) PlotText("LH",i+2,H[i],colorWhite,colordefault, Yofs );
        if(dt[i]) PlotText("DT",i+2,H[i],colorWhite,colordefault, Yofs );

    }
    
    
    for ( i = start; i <= end; i++ ) 
    {
        if(tr[i])    PlotTextSetFont("q", "Wingdings 3", 11, i, L[i], colorLime, colorDefault, -Yofs-5);
           if(pk[i])    PlotText("p",i, H[i], colorRed, colorDefault, Yofs);
    }
    
    
}  

3 comments

1. anandnst

AFL not working in 5.60 amibroker version

2. extremist

@ anandnst
plz read the first line of code before complaining.

Version(5.80);

this means code needs at least min version 5.80

3. moccha

Some expect the buttons to be pushed for them.

Leave Comment

Please login here to leave a comment.

Back