Download MetaTrader 5

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

To add comments, please log in or register
Forex Trader
Forex Trader  
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 :=

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


// 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;


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;



If Counter <> 0 Then

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


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

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


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;



If Counter <> 0 Then

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


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

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


PreSafeStop = SafeStop;

loopbegin = loopbegin - 1;



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


Trailing Stop - Elder's SafeZone



{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 }

{ }

{ User inputs }

coefficient:=Input("SafeZone coefficient",


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 }



AND L<Ref(L,-1);






{ Upside penetrations }



AND H>Ref(H,-1);






{ Stop signals }



{ Clean signals }


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


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

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

{ Plot on price chart }








[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.
Forex Trader
Forex Trader  
Wolf, did you get this solved?
Forex Trader
Forex Trader  
No yet sorry. =o Can't get anyone to do it, so Im trying to learn how to do it myself.
Forex Trader
Forex Trader  
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.
Forex Trader
To add comments, please log in or register