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

Re: Trailing stop formula



PureBytes Links

Trading Reference Links

Philip

By modifying the Value variable to suit your own requirements you should be able to do what you want
with this pair of trailing stops. These stops are very flexible but can look a bit daunting so if
you want a fuller explanation than the comments provide feel free to drop a line to
rlarsen@xxxxxxxxxxxxx

Roy

  {Trailing Stop - Long}
  {Trade leading edge formula here}
Enter:=0 {Fml("Your Long Entry code")};
Mpr:=Input("ATR Multiplier",1.5,5,2.5);
Slb:=Input("Support Lookback Periods",2,10,6);
Rlb:=Input("Resistance Lookback Periods",1,20,3);
Hcl:=Input("Suspend Trail From 1=C, 2=H, 3=L, 4=MP",1,4,1);
Ins:=Input("Include Support Level in Trail  0=No,  1=Yes",0,1,1);
  {Resistance}
Res:=ValueWhen(1,Cross(Mov(C,Rlb,S),C),HHV(H,Rlb));
  {Support}
Spt:=ValueWhen(1,Cross(C,Mov(C,Slb,S)),LLV(L,Slb));
  {Historical volatility - with 2 or 3 bar EMA}
Hvm:=Mov(Std(Log(C/Ref(C,-1)),6)/Std(Log(C/Ref(C,-1)),50),2,E);
  {Composite trail value - with 2 or 3 bar EMA}
Value:=Mov(If(Hcl=1,C,If(Hcl=2,H,If(Hcl=3,L,MP())))-
 Mov(ATR(1),45,S)*Mpr*If(Hvm<1,Hvm,1/Hvm),3,E);
  {Reset for reference trail}
CheckReset:=Cum(1)=58 OR Ref(Cross(C,Res),-1);
  {Raw reference trail}
RawCheck:=HighestSince(1,CheckReset,If(Ins=1,Max(Value,Spt),Value));
  {Precision reference trail - ASX trade steps}
PrecCheck:=If((RawCheck>=0.1 AND RawCheck<0.5),PREC(RawCheck*2,2)/2,
 If(RawCheck<0.1,PREC(RawCheck,3), PREC(RawCheck,2)));
  {Primary trail reset}
Reset:=Cum(1)=60 OR Enter OR (C<PrecCheck
 AND ((Ref(C,-1)<PrecCheck) AND Cross(C,Res)));
  {Raw primary trail}
RawTrail:=HighestSince(1,Reset,If(Ins=1,Max(Value,Spt),Value));
  {Precision primary trail - ASX trade steps}
PrecTrail:=If((RawTrail>=0.1 AND RawTrail<0.5),PREC(RawTrail*
 2,2)/2,If(RawTrail<0.1,PREC(RawTrail,3), PREC(RawTrail,2)));
PrecTrail;

  {Trailing Stop - Short}
  {Trade leading edge formula here}
Enter:=0  {Fml("Your Short Entry code")};
Mpr:=Input("ATR Multiplier ",1.5,5,2.5);
Slb:=Input("Support Lookback Periods",2,10,3);
Rlb:=Input("Resistance Lookback Periods",1,20,6);
Hcl:=Input("Float Trail from 1=C, 2=H, 3=L, 4=MP",1,4,4);
Inr:=Input("Include Resistance Level in Trail:  0=No,  1=Yes",0,1,1);
  {Resistance}
Res:=ValueWhen(1,Cross(Mov(C,Rlb,S),C),HHV(H,Rlb));
  {Support}
Spt:=ValueWhen(1,Cross(C,Mov(C,Slb,S)),LLV(L,Slb));
  {Historical volatility - with 2 or 3 bar EMA}
Hvm:=Mov(Std(Log(C/Ref(C,-1)),6)/Std(Log(C/Ref(C,-1)),50),2,E);
  {Composite trail value - with 2 or 3 bar EMA}
Value:=Mov(If(Hcl=1,C,If(Hcl=2,H,If(Hcl=3,L,MP())))+
  Mov(ATR(1),45,S)*Mpr*If(Hvm<1,Hvm,1/Hvm),3,E);
  {Reset for reference trail}
CheckReset:=Cum(1)=58 OR Ref(Cross(Spt,C),-1);
  {Raw reference trail}
RawCheck:=LowestSince(1,CheckReset,If(Inr=1,Min(Value,Res),Value));
  {Precision reference trail - ASX trade steps}
PrecCheck:=If((RawCheck>=0.1 AND RawCheck<0.5),PREC(RawCheck*2,2)/2,
  If(RawCheck<0.1,PREC(RawCheck,3),PREC(RawCheck,2)));
  {Primary trail reset}
Reset:=Cum(1)=60 OR Enter OR (C>PrecCheck AND
  ((Ref(C,-1)>PrecCheck) AND Cross(Spt,C)));
  {Raw primary trail}
RawTrail:=LowestSince(1,Reset,If(Inr=1,Min(Value,Res),Value));
  {Precision primarye trail - ASX trade steps}
PrecTrail:=If((RawTrail>=0.1 AND RawTrail<0.5),PREC((RawTrail+.005)*
  2,2)/2,If(RawTrail<0.1,PREC(RawTrail+.001,3), PREC(RawTrail+.01,2)));
PrecTrail;