I am trying to implement the Elder Safe Zone stop from code I got from the library and elsewhere. Try as I might (and it's been a while), I cannot determine how to convert the stop price I get from the SZ calculation into the "amount" for a trailing stop. I have searched Help, the AB site and newsgroups here but have not found any useful information on how to make this work or how AB handles a trailing stop.
As you can see below, I have approximated a trailing stop, but it has the disadvantage of using the the next day's open when the actual stop is greater than the next day's prices and it does not exit intraday.
The setup for EOD trading, exiting intraday is in my formula -- the profit stop I've coded works as expected. Following the work-around for the trailing stop is the code I started for the trailing Applystop, but of
course the code for the "amount" is wrong. What is there is the closest I could come to getting the right amount and it's very wrong.
Thank you for any help you can provide.
TrailingStop = 4; //simulate trailing stop
LookbackPeriod = 8;//Optimize("SZLkBk",12,4,12,1); //Set Lookback period
AvgDnPenCoeff = 1.9;//;//Optimize("SZLongCoeff", 1.35, 1.5, 3.5, 0.1); //
DownPen = IIf(Low < L1, L1 - Low, 0); //Identify amount of each penetration -- O=none
DownPenSum = Sum(DownPen,LookbackPeriod); //Total volume of penetrations during the lookback
period
DownPenCount = Sum(Low < L1, LookbackPeriod); //Number of penetrations during lookback period
AvgDownPen = DownPenSum/DownPenCount; //Average penetration amount during lookback period
AvgDownPen = Nz(AvgDownPen, 0);//Insure it is not NAN
DownPenSum = Sum(DownPen, LookbackPeriod); //Total volume of penetrations during the lookback period
SafeZoneLong = L1 - (AvgDownPen*AvgDnPenCoeff); // "Place your stop at a multiple of yesterday's AveDownPen below yesterday's low."
SafeZoneTrailLong = HHV( SafeZoneLong, BarsSince(Buy) + 1 );//stop can't drop
during trade
SZStopLong = IIf( Low <= Ref( SafeZoneTrailLong, -1), True, False); //the stop is trigger
SZStopLong = ExRem(SZStopLong, Buy);//mask off secondary SZ signals
//Trailing stop workaround
for( i = 0; i < BarCount; i++ )
{
if( SZStopLong[i] == True )
{
SellPrice[i] = SafeZoneTrailLong[i];
Sell[i] = TrailingStop;
}
}
Plot(SafeZoneTrailLong, " SellStop", colorYellow, styleStaircase);
//How to make this work?
SZTrailLong_Amount = High - Ref(SafeZoneTrailLong, - 1); //Wrong
SZTrail_ExitAtStop = 1;
SZTrail_Volatile = True;
SZTrail_ReEntryDelay = 1;
ApplyStop(stopTypeTrailing, stopModePoint, SZTrailLong_Amount, SZTrail_ExitAtStop, SZTrail_Volatile, SZTrail_ReEntryDelay);
Best regards,
Joe