Dr Elder's SafeZone stop - Has MT3 - need MT4

 
Has anyone got it? It'd be really useful. Here's the code in MT3. It would be great if you could translate it into MT4

Here's the MT3 script:



/*[[

Name := SafeZone Stop

Author := Copyright ?2004, MetaQuotes Software Corp.

Link := https://www.metaquotes.net/

Separate Window := No

First Color := Cyan

First Draw Type := Symbol

First Symbol := 159

Use Second Data := No

Second Color := Red

Second Draw Type := Line

Second Symbol := 218

]]*/

Inputs: LookBack(10), StopFactor(3), EMALength(13);

Variables: Pen(0), Counter(0), SafeStop(0), EMA0(0), EMA1(0), EMA2(0);

Variables: first(True), prevbars(0), loopbegin(0), shift(0), value1(0), PreSafeStop(0);

SetLoopCount(0);

// Initial checkings

If LookBack < 1 or EMALength < 1 Then Exit;

If Bars < prevbars Or Bars - prevbars > 1 Then first = True;

prevbars = Bars;

If first Then Begin

loopbegin = Bars - LookBack - 3;

If loopbegin < 0 Then Exit; // not enough bars

first = False;

End;

loopbegin = loopbegin + 1;

For shift = loopbegin DownTo 0 Begin

EMA0 = iMA(EMALength,MODE_EMA,shift);

EMA1 = iMA(EMALength,MODE_EMA,shift+1);

EMA2 = iMA(EMALength,MODE_EMA,shift+2);

PreSafeStop = GetIndexValue(shift+1);

Pen = 0;

Counter = 0;

If EMA0 > EMA1 Then Begin

For value1=0 to LookBack Begin

If Low[shift+value1] < Low[shift+value1+1] Then Begin

Pen = Low[shift+value1+1] - Low[shift+value1] + Pen;

Counter = Counter + 1;

End;

End;

If Counter <> 0 Then

SafeStop = Close[shift] - (StopFactor * (Pen/Counter))

Else

SafeStop = Close[shift] - (StopFactor * Pen);

If SafeStop < PreSafeStop and EMA1 > EMA2 Then SafeStop = PreSafeStop;

End;

If EMA0 < EMA1 Then Begin

For value1=0 to LookBack Begin

If High[shift+value1] > High[shift+value1+1] Then Begin

Pen = High[shift+value1] - High[shift+value1+1] + Pen;

Counter = Counter + 1;

End;

End;

If Counter <> 0 Then

SafeStop = Close[shift] + (StopFactor * (Pen/Counter))

Else

SafeStop = Close[shift] + (StopFactor * Pen);

If SafeStop > PreSafeStop and EMA1 < EMA2 Then SafeStop = PreSafeStop;

End;

PreSafeStop = SafeStop;

loopbegin = loopbegin - 1;

SetIndexValue(shift,SafeStop);

End;





I have it on Metastock as well, not sure if it'd be of any use :



================================

Trailing Stop - Elder's SafeZone

================================

---8<---------------------------



{Dr Alexander Elder's SafeZone trailing stop v3}



{ SafeZone types:



[1] Uses upside/downside penetration days as

suggested by Dr Elder in his book;



[2] Uses up/down days (instead of

upside/downside penetration days)

to match book's companion spreadsheet.}



{ Triggers: Long (+1) & Short (-1) signals at

crossover of user-defined trailing stops.}



{ ©Copyright 2003-2005 Jose Silva }

{ For personal use only }

{ http://www.metastocktools.com }



{ User inputs }

coefficient:=Input("SafeZone coefficient",

0,10,2.5);

bkpds:=Input("Lookback periods",1,260,10);

pds:=Input("Trend EMA periods",2,260,21);

adv:=Input("plot: [1]Today's SafeZone, [2]Tomorrow's stop",1,2,1)-1;

type:=Input("SafeZone type: [1]Book, [2]Spreadsheet",1,2,1);

plot:=Input("SafeZone: [1]Stop, [2]Long+Short, [3]Signals",1,3,1);



{ Downside penetrations }

DwSidePen:=

If(type=1,Mov(C,pds,E)>Ref(Mov(C,pds,E),-1),1)

AND L<Ref(L,-1);

DwSideDiff:=If(DwSidePen,Ref(L,-1)-L,0);

DwPenAvg:=Sum(DwSideDiff,bkpds)

/Max(Sum(DwSidePen,bkpds),.000001);

StLong:=Ref(L-DwPenAvg*coefficient,-1);

StopLong:=If(C<PREV,StLong,Max(StLong,PREV));



{ Upside penetrations }

UpSidePen:=

If(type=1,Mov(C,pds,E)<Ref(Mov(C,pds,E),-1),1)

AND H>Ref(H,-1);

UpSideDiff:=If(UpSidePen,H-Ref(H,-1),0);

UpPenAvg:=Sum(UpSideDiff,bkpds)

/Max(Sum(UpSidePen,bkpds),.000001);

StShort:=Ref(H+UpPenAvg*coefficient,-1);

StopShort:=If(C>PREV,StShort,Min(StShort,PREV));



{ Stop signals }

entry:=Cross(C,Ref(StopShort,-1));

exit:=Cross(Ref(StopLong,-1),C);



{ Clean signals }

Init:=Cum(IsDefined(entry+exit))=1;

bin:=ValueWhen(1,entry-exit<>0 OR Init,

entry-exit);

entry:=bin=1 AND (Alert(bin<>1,2) OR Init);

exit:=bin=-1 AND (Alert(bin<>-1,2) OR Init);



{ Plot on price chart }

If(plot=1,Ref(If(bin=1,stopLong,stopShort),

-1+adv),If(plot=2,Ref(stopLong,-1+adv),0));

If(plot=1,Ref(If(bin=1,stopLong,stopShort),

-1+adv),If(plot=2,Ref(stopShort,-1+adv),

entry-exit))



---8<---------------------------





http://www.metastocktools.com[/quote]



Description:





[QUOTE=Come into my Trading Room]'SafeZone measures market noise and places stops at a multiple of noise level away from the market.



We may use the slope of a 22-day EMA to define the trend. You need to choose the length of the lookback period for measuring noise level. It has to be long enough to track recent behaviour but short enough to be revelent for current trading. A period of 10 to 20 days works well, or we can make our lookback period 100 days or so if we want to average long-term market behaviour.



If the trend is up, mark all downside penetrations during the look-back period, add their depths, and divide the sum by the number of penetrations. This gives you the Average Downside Penetration for the selected lookback period. It reflects the average level of noise in the current uptrend. Placing your stop any close would be self-defeating. We want to place our stops farther away from the market than the average level of noise. Multiply the Average Downside Penetration by a coefficient, starting with two, but experiment with higher numbers. Subtract the result from yesterday's low, and place your stop there. If today's low is lower than yesterday's, do not move your stop lower since we are only allowed to raise stops on long positions, not lower them.



Reverse these rules in downtrends. When a 22-day EMA identifies a downtrend, count all the upside penetrations during the lookback period and find the Average Upside Penetration. Multiply it by a co-efficient, starting with two. When you go short, place a stop twice the Average Upside Penetration above the previous day's high. Lower your stop whenever the market makes a lower high, but never raise it.



I anticipate that SafeZone will be programmed into many software packages, allowing traders to control both the lookback period and the multiplication factor. Until then, you will have to do your own programming or else track SafeZone manually. Be sure to calculate it separately for uptrends and downtrends.[/QUOTE]



This is the description of the trailing stop in Dr Elder's 'Come into my trading room' (Pg. 174) I'd really appreciate it if anyone could program it into MT4.



A big thankyou in advance.
 
Wolf, did you get this solved?
 
No yet sorry. =o Can't get anyone to do it, so Im trying to learn how to do it myself.
 
This is good. If you learn it by yourself, then you will be able to find the bugs also by yourself. If there are some questions, ask, and somebody will help you.

EDIT: I had a look, it seems not to be so difficult, because its from MT3.
Reason: