ATR-based trailing stop value on the closing price.
{Get the required ATR period;}
period:=Input("ATR Period :",1,100,5);
{Calculate the biggest difference based on the true range concept;}
diff1:=Max(H-L,Abs(H-Ref(C,-1)));
diff2:=Max(diff1,Abs(L-Ref(C,-1)));
{Use Wilders' moving average method to calculate the Average True Range;}
Mov(diff2,period*2-1,E)
Sidebar code from page 35 of the June 2009 issue:
ATR TRAILING STOP
{SVE_Stop_trail_ATR}
atrper:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
loss:=atrfact*ATR(atrper);
trail:=
If(C>PREV AND Ref(C,-1)>PREV,
Max(PREV,C-loss),
If(C < PREV AND Ref(C,-1) < PREV,
Min(PREV,C+loss),
If(C>PREV,C-loss,C+loss)));
Trail
Code from page 36 of the June 2009 issue:
Long position: SVE_StopLong_Trail_ATR_Date.
{SVE_StopLong_Trail_ATR_Date - ATR trailing stop long from date}
InpMonth:=Input("Month",1,12,1);
InpDay:=Input("Day",1,31,1);
InpYear:=Input("Year",1800,2050,2009);
InitStop:=Input("Initial Stop Price",0.1,10000,10);
atrper:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
loss:=atrfact*ATR(atrper);
Entry:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth();
StopLong:=ExtFml( "AdvancedStop.StopLong", Entry,InitStop,0,C-Loss,0,0,0,0);
StopLong
Short position: SVE_StopShort_Trail_ATR_Date.
{SVE_StopShort_Trail_ATR_Date - ATR trailing stop short from date}
InpMonth:=Input("Month",1,12,1);
InpDay:=Input("Day",1,31,1);
InpYear:=Input("Year",1800,2050,2009);
InitStop:=Input("Initial Stop Price",0.1,10000,10);
atrper:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
loss:=atrfact*ATR(atrper);
Entry:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth();
StopShort:=ExtFml("AdvancedStop.StopShort",Entry,
InitStop,0,C+Loss,0,0,0,0);
StopShort
Sidebar code from page 38 of the June 2009 issue:
ATR MODIFIED
{SVE_ATR_Mod}
period:=Input("ATR Period :",1,100,5);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
Wilders(diff2,period);
Sidebar code from page 39 of the June 2009 issue:
MODIFIED ATR WITH TRAILING STOPS
{SVE_Stop_Trail_ATR_Mod}
period:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
atrmod:=Wilders(diff2,period);
loss:=atrfact*atrmod;
trail:=
If(C>PREV AND Ref(C,-1)>PREV,
Max(PREV,C-loss),
If(C < PREV AND Ref(C,-1) < PREV,
Min(PREV,C+loss),
If(C>PREV,C-loss,C+loss)));
Trail
Sidebar code from page 40 of the June 2009 issue:
Modified ATR trailing stop from start date
Since you are using your own trading method to finding entry points, you certainly want
to be able to use your own entry date. So again, since the number of input parameters in
MetaStock is limited to six, we have to create separate formulas for a long or a short
position:
Long position: SVE_StopLongMod_Trail_ATR_Date.
{SVE_StopLongMod_Trail_ATR_Date}
InpMonth:=Input("Month",1,12,1);
InpDay:=Input("Day",1,31,1);
InpYear:=Input("Year",1800,2050,2009);
InitStop:=Input("Initial Stop Price",0.1,10000,10);
period:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
atrmod:=Wilders(diff2,period);
loss:=atrfact*atrmod;
Entry:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth();
StopLong:=ExtFml( "AdvancedStop.StopLong", Entry,InitStop,0,C-Loss,0,0,0,0);
StopLong
Short position: SVE_StopShortMod_Trail_ATR_Date.
{SVE_StopShortMod_Trail_ATR_Date}
InpMonth:=Input("Month",1,12,1);
InpDay:=Input("Day",1,31,1);
InpYear:=Input("Year",1800,2050,2009);
InitStop:=Input("Initial Stop Price",0.1,10000,10);
period:=Input("ATR period :",1,100,5);
atrfact:=Input("ATR multiplication :",1,10,3.5);
HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
diff1:=Max(HiLo,Href);
diff2:=Max(diff1,Lref);
atrmod:=Wilders(diff2,period);
loss:=atrfact*atrmod;
Entry:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth();
StopShort:=ExtFml("AdvancedStop.StopShort",
Entry,InitStop,0,C+Loss,0,0,0,0);
StopShort