# Req AFL CODE FOR ATR BASED STOPLOSS IN METASTOCK

#### ford7k

##### Banned
Hi friends

Saw this metastock code

can somebody help me to get it as afl,please
purpose
I just like to see if ms code is any better than my existing stoploss code.

of course,I already have a stoploss code in the lines of jimberg.
see it at the end of this post

HTML:
``````Can someone help me in converting the following Metastock code to
Amibroker AFL

A:=2.5*ATR(5) ; TL:=C-A; TS:=C+A;
ln:=if(C> =PREV,Max( TL,PREV), TL);
SH:=if(C< =PREV,Min( TS,PREV), TS);
U:=Cross(C,Ref( SH,-1));
D:=Cross(Ref( ln,-1),C) ;
I:=Cum(IsDefined( I+D))=1;
if(R,ValueWhen( 1,1,ln),ValueWhen(1,1,SH) );``````
HTML:
``````//afl code is on these lines-incomplete
A=2.5*ATR(5);
TL =(C-A);
TS = (C+A);
PREV = GOD KNOWS!!!!!!!

ln = IIf(C> =PREV,Max( TL,PREV), TL);//LONG
SH = IIf(C< =PREV,Min( TS,PREV), TS);//SHORT
U = Cross(C,Ref( SH,-1));
D = Cross(Ref( ln,-1),C) ;

I=Cum(IsDefined( I+D))=1;
if(R,ValueWhen( 1,1,ln),ValueWhe n(1,1,SH) );
//ABOVE CODE INCOMPLETE-YET TO PREPARE- DONT TRY TO USE IT``````
-------------------------------------------------

//mystoplosscode

SetChartBkColor(64);
BARCOLOR =IIf(C>O,colorGreen,colorRed);
Plot(C,"C",BARCOLOR,styleBar);
TrailStop25 = HHV( C - 2.5 * ATR(5), 15 );
STOPCOL= IIf(C>TrailStop25,colorBlue,colorCustom12);
Plot( TrailStop25, "Trailing stop2.5", STOPCOL, styleLine );
Plot(L,"LOW OF EACH BAR",colorViolet,styleDashed);
Plot(H,"HIGH OF EACH BAR",colorOrange,styleDashed);
//note for intraday trading use 1.5 factor in place of 2.5
//all the best
================================= Last edited:

#### KelvinHand

##### Well-Known Member
Finally. I got it right both side as shown below.  Last edited:

#### KelvinHand

##### Well-Known Member
For Education Purpose Only,
If other people using it, Trade as your own risk. Repaint don't look for me. Accuracy is not my concern.

PHP:
``````A=2.5*ATR(5);
TL=C-A;
TS=C+A;

_ln=TL;
_SH=TS;

for(i=1; i<BarCount; i++)
{
PrevTL = _ln[i-1];
if (C[i]>=PrevTL)
_ln[i] = Max(TL[i], PrevTL);
else
_ln[i] = TL[i];

PrevTS = _sh[i-1];
if (C[i]<=PrevTS)
_sh[i] = Min(TS[i], PrevTS);
else
_sh[i] = TS[i];
}

U=Cross(C,Ref(_sh,-1));
D=Cross(Ref( _ln,-1),C);

I=Cum( !IsNull(I+D))==1;

ATRStop =IIf(R,ValueWhen(1,_ln),ValueWhen(1,_SH) );

Color = IIf(C>ATRStop, colorLime, IIf(C<ATRStop, colorRed, colorDarkYellow));
Plot(ATRStop, "ATRStop", Color, styleThick);``````

Last edited:

#### hitesh

##### Active Member
Chaloke Modified ATR SL uses same parameters. AFL as follows:-

_SECTION_BEGIN("chaloke modified atr sl");
///Chaloke Modified ATR Trailing Stop///

A=IIf( Cum(1)<16 , C,
IIf( C > HHV(H-2.5*ATR(5),10) AND C > Ref(C,-1),HHV(H-2.5*ATR(5),10),
Prev));

Plot(A,"Chaloke Modified ATR Trailing Stop-Level 4",colorBlack,4);
Color=IIf(C>A,colorBrightGreen,IIf(C<A,colorRed,colorBlack));
Plot(C,"",color,styleCandle);
PlotShapes(shapeDownArrow*Cross(A,C),colorBlue,0,H,Offset=-24);
PlotShapes(shapeUpArrow*( Cross(C,A) ) ,colorYellow,0,L,Offset=-15);

Plot(A,"Chaloke Modified ATR Trailing Stop-Level 4",colorBlack,4);
_SECTION_END();