five40 23 posts msg #59629 - Ignore five40 | 
2/11/2008 6:27:06 PM
  Has anyone here used Dr Elders "SafeZone" in Stock Fetcher?
 
 The formula gives an guide of where to set a stop loss to avoid market noise. It's based on average downside penetration over the last X days. He lays out the formula for building this in an excel spreadsheet in the book "Come into my Trading Room".
 
 I have done this in excel and I really like the results.
 
 On the AmiBroker site I found the formula below but I have not been able to convert it to Stock Fetcher. (lots of IF statements).
 
 Trailing stop method
 Formula:
 
 _SECTION_BEGIN("Elder safe Zone Long + short");
 
 L1=Ref(L,-1);
 H1=Ref(H,-1);
 Pd=Param("Period",10,1,50,1);
 DnPen=IIf(L
 UpPen=IIf(H>H1,H-H1,0);
 DnPenSum=Sum(DnPen,Pd);
 UpPenSum=Sum(UpPen,Pd);
 DnPenCount=Sum(L
 UpPenCount=Sum(H>H1,Pd);
 AvgDnPen=IIf(DnPenCount>0,DnPenSum/DnPenCount,0.01);
 AvgUpPen=IIf(UpPenCount>0,UpPenSum/UpPenCount,0.01);
 AvgDnPenMult=Param("Multiplier",2,1,4,0.1);
 AvgUpPenMult=Param("Multiplier",2,1,4,0.1);
 DaysInTrade=Param("Days in trade",1,1,50,1);
 StopLong=L-(AvgDnPen*AvgDnPenMult);
 StopShort=H+(AvgUpPen*AvgUpPenMult);
 SafeZoneStopLong=HHV(StopLong,DaysInTrade);
 SafeZoneStopShort=LLV(StopShort,DaysInTrade);
 MilieuCorps = (C+O)/2;
 Plot( C, "Close", ParamColor("Couleur ", colorBlack ), styleNoTitle |
 ParamStyle("Style") | styleCandle|styleThick);
 ShortStop=BeginValue(C);
 LongStop=BeginValue(C);
 i=1;
 for (i=1;i < BarCount; i++)
 {
 if (SafeZoneStopShort[i] >= SafeZoneStopShort[i-1] && ShortStop[i-1] <= H[i])
 ShortStop[i] = SafeZoneStopShort[i] ;
 if (SafeZoneStopShort[i] >= SafeZoneStopShort[i-1] && ShortStop[i-1] > H[i])
 ShortStop[i] = ShortStop[i-1];
 if (SafeZoneStopShort[i] < SafeZoneStopShort[i-1] && ShortStop[i-1] <= H[i])
 ShortStop[i] = SafeZoneStopShort[i] ;
 if (SafeZoneStopShort[i] < SafeZoneStopShort[i-1] && ShortStop[i-1] > H[i])
 ShortStop[i] = ShortStop[i-1];
 if (IsEmpty(SafeZoneStopShort[i]))
 ShortStop[i] = SafeZoneStopShort[i] ;
 if (ShortStop[i] >= SafeZoneStopShort[i])
 ShortStop[i] = SafeZoneStopShort[i] ;
 
 if (SafeZoneStopLong[i] <= SafeZoneStopLong[i-1] && LongStop[i-1] >= L[i])
 LongStop[i] = SafeZoneStopLong[i] ;
 if (SafeZoneStopLong[i] <= SafeZoneStopLong[i-1] && LongStop[i-1] < L[i])
 LongStop[i] = LongStop[i-1];
 if (SafeZoneStopLong[i] > SafeZoneStopLong[i-1] && LongStop[i-1] >= L[i])
 LongStop[i] = SafeZoneStopLong[i] ;
 if (SafeZoneStopLong[i] > SafeZoneStopLong[i-1] && LongStop[i-1] < L[i])
 LongStop[i] = LongStop[i-1];
 if (IsEmpty(SafeZoneStopLong[i]))
 LongStop[i] = SafeZoneStopLong[i] ;
 if (LongStop[i] <= SafeZoneStopLong[i])
 LongStop[i] = SafeZoneStopLong[i] ;
 
 }
 
 
 PlotShapes(IIf(ShortStop>Ref(ShortStop,-1), shapeHollowSmallDownTriangle,
 shapeNone), colorRed, 0, ShortStop, Offset=-15);
 PlotShapes(IIf(LongStop
 shapeNone), colorGreen, 0, LongStop, Offset=-15);
 Plot(SafeZoneStopShort,"Safe Zone Short", ParamColor("Couleur Safe Zone Short",
 colorRed ),ParamStyle("Style Safe Zone Short",styleDots|styleNoLine) );
 Plot(SafeZoneStopLong,"Safe zone Long", ParamColor("Couleur Stop Long",
 colorGreen ),ParamStyle("Safe zone Long",styleDots|styleNoLine) );
 Plot(ShortStop,"Stop Short",
 IIf(ShortStop>Ref(ShortStop,-1),colorCustom1,colorBlue),ParamStyle("Style Stop
 Short",styleLine|styleLine) );
 Plot(LongStop,"Stop Long",
 IIf(LongStop
 Long",styleLine|styleLine) );
 
 
 _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
 Close %g (%.1f%%)n - Elder SafeZone long-Short pour trades aprčs le
 "+Date()+"n {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
 Title= Title
 +"nc56Periode: c56"
 +WriteVal(Pd,1.0)
 +"c56 - Multiplicateur Long : c56"
 +WriteVal(AvgDnPenMult,1.1)
 +"c56 - Multiplicateur Short : c56"
 +WriteVal(AvgUpPenMult,1.1)
 +"c56 - Jour en position(inclus): c56"
 +WriteVal(DaysInTrade,1.0)
 +"nc56Milieu corps : c56"
 +WriteVal(MilieuCorps,1.2 )
 +": c56 - N° barre : "
 +WriteVal(BarIndex(),1);
 _SECTION_END(); 
 
  |