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

[amibroker] Re: Elder SafeZone Stop



PureBytes Links

Trading Reference Links

This was a nice excercise for the use of the new loop capabilities of
AB. You need AB v 4.31 or higher to run this Code:

pds = Param("Periods",10);// variable lookback length
DnPen = IIf(Ref(L,-1)>L, Ref(L,-1)-L,0);
zsum = 0;
for(i=0;i<pds;i++)
{
zsum=zsum+Ref(DnPen,-i);
}

PenYN =IIf(L <Ref(L,-1),1,0);
DnNumb =0;
for(i=0;i<pds;i++)
{
DnNumb=DnNumb+Ref(PenYN ,-i);
}

DnAvg = zSum/DnNumb;
LongStop = L -2*DnAvg;
LMax = Max(LongStop, Ref(LongStop,-1));
LMax = Max(LMax, Ref(LongStop,-2));

Plot(C,"Price",colorBlack,styleCandle);
Plot(Lmax,"LMax",colorBlue,styleDots|styleNoLine);


More simple, you can use ABs sum() function instead of the for-loops:
zsum = sum(DnPen,pds);
and
DnNumb = sum(PenYN,pds);

see http://www.amibroker.com/guide/afl/afl_view.php?id=151

HTH

Stephan

--- In amibroker@xxxxxxxxxxxxxxx, "miked33" <miked@xxxx> wrote:
> I am trying to write the Elder SafeZone stop from his book. I have 
> written a version below, but would like to make the lookback period 
> variable.  Can someone help modify my code? 
> 
> thanks
> 
> 
> Elder SafeZone
> 1) If today's low is lower than yesterday measure downside 
> penetration otherwise 0
> 
> DnPen = IIf(Ref(L,-1)>L, Ref(L,-1)-L,0);
> 
> 2) Choose lookback period and sum all downside penetrations during 
> that period.  Would like to make this variable.
> 
> zSum = DnPen + Ref(DnPen,-1)+Ref(DnPen,-2)+Ref(D
nPen,-3)+Ref(DnPen,-4)
> +
>                Ref(DnPen,-5)+Ref(DnPen,-6)+Ref(D
nPen,-7)+Ref(DnPen,-8)
> +
>                Ref(DnPen,-9);
> 
> 3) Mark each bar that penetrates below the previous bar.
> 
> PenYN =IIf(L <Ref(L,-1),1,0);
> 
> 4) Count the number of downside penetrations during the lookback 
> period. Would like to be equal to lookback period defined in step 2.
> 
> DnNumb = PenYN + Ref(PenYN ,-1)+Ref(PenYN ,-2)+Ref(PenYN ,-3)+Ref
> (PenYN ,-4)+
>                Ref(PenYN ,-5)+Ref(PenYN ,-6)+Ref(PenYN ,-7)+Ref
> (PenYN ,-8)+
>                Ref(PenYN ,-9);
> 
> 5)Find average downside penetration during lookback period.
> 
> DnAvg = zSum/DnNumb;
> 
> 6)Place stop at multiple of yesterday's average downside
penetration 
> below yesterday's low and subtract the result from yesterday's low
to 
> obtain today's stop. (I think since I am using EOD - I should use 
> today's low and DnAvg?)
> 
> LongStop = L -2*DnAvg;
> 
> 7)Refine formula to prevent from lowering stops in uptrends.  If 
> formula tells us to lower stop, leave at previous day's level.
Elder 
> programs the SafeZone in Excel in his book.  For this step, he uses 
> MAX(J14:J16). He says that this will prevent the stop from
declining 
> for 3 days, by which time the uptrend resumes or the stop is hit.
> 
> LMax = Max(LongStop, Ref(LongStop,-1));
> LMax = Max(LMax, Ref(LongStop,-2));
> 
> 
> 
> Entire AFL
> 
> DnPen = IIf(Ref(L,-1)>L, Ref(L,-1)-L,0);
> zSum = DnPen + Ref(DnPen,-1)+Ref(DnPen,-2)+Ref(D
nPen,-3)+Ref(DnPen,-4)
> +
>                Ref(DnPen,-5)+Ref(DnPen,-6)+Ref(D
nPen,-7)+Ref(DnPen,-8)
> +
>                Ref(DnPen,-9);
> PenYN =IIf(L <Ref(L,-1),1,0);
> DnNumb = PenYN + Ref(PenYN ,-1)+Ref(PenYN ,-2)+Ref(PenYN ,-3)+Ref
> (PenYN ,-4)+
>                Ref(PenYN ,-5)+Ref(PenYN ,-6)+Ref(PenYN ,-7)+Ref
> (PenYN ,-8)+
>                Ref(PenYN ,-9);
> DnAvg = zSum/DnNumb;
> LongStop = L -2*DnAvg;
> LMax = Max(LongStop, Ref(LongStop,-1));
> LMax = Max(LMax, Ref(LongStop,-2));


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading!
Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/cjB9SD/od7FAA/AG3JAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/