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

Re: Alexander Elder's "SafeZone Stop"



PureBytes Links

Trading Reference Links

Thats nice code Gary.

I agree that calculating LDay only after the start of the trend is causing
the difference in the stop for the first couple of days of a new trend.

Barry, If Dr Elder doesnt directly state that we should begin calculating
the LDay after the trend change I'd support removing the "IF CONDITION2
THEN" which will give the same result that Gary's code provides.  I've
pasted an updated version below and attached it as an ela.  Apologies for
the format of the comments below ... they're better in the ela.

A question for Gary.  Do you know which approach is better wrt to speed of
calculation?

John

----- Original Message -----
From: "Gary Fritz" <fritz@xxxxxxxx>
To: "Barry Silberman" <barry@xxxxxxxxxxxxxxxxxxxxx>
Cc: "Omega List" <omega-list@xxxxxxxxxx>
Sent: Wednesday, May 08, 2002 6:31 AM
Subject: RE: Alexander Elder's "SafeZone Stop"


> The version below is a combination of the code John Lynch provided
> to me privately for testing (which he has approved for posting to
> the list) and some neat additions from Eric Svendesen, which he
> previously provided to the list.   The array formula below gives
> identical results to the "LONG" way of doing it.

FWIW:  Here is a version that uses the TS variable history instead of
arrays.

This indicator's results are not quite identical to Barry's.  In
particular, for a few bars after a trend change, this code produces
slightly different results.  After a few bars, they match exactly.

I don't have Elder's book, so I can't say for sure, but I suspect
this code is more correct.  Both versions that Barry posted compute
the LSTOP and SSTOP values on every bar -- which they must, in order
for the 5-day min/max calculation to work correctly -- but they only
compute the LDAY/SDAY values when the trend is right.  Which means
that for the first few days after a trend change, the LSTOP/SSTOP
calculations are using invalid values of LDAY/SDAY.

Gary

=====


{ Barry Silberman's version of Alexander Elder's "SafeZone Stop" from book
"Come Into My Trading Room" page 173
Amended by John Lynch 5/1 with Eric Svendsen's Coloured Stop and trend
variable added 5/3}

Inputs:
 Price((H+L)/2 ), Length( 22), Lookback(15), trend(3),
 MULTIPLELONG(1), MULTIPLESHORT(1), ColourShort(Red), ColourLong(Blue);

Variables:
MA(0),  LONGSUM(0),  SHORTSUM(0) , COUNTLONG(0), COUNTSHORT(0), AVGLOW(0),
AVGHIGH(0), LONGSTOP(0), SHORTSTOP(0), LSTOP(0), SSTOP(0), ii(0);

Arrays: Lday[30](0), Sday[30](0);

{determine trend by slope of EMA}
MA = xaverage( Price, Length );
CONDITION1 = MA > MA[trend];
CONDITION2 = MA < MA[trend];

{SafeZone for Longs}
LONGSUM = 0;
COUNTLONG = 0;
For ii=1 to Lookback Begin
  Lday[ii]=IFF(L[ii]>L[ii-1],L[ii]-L[ii-1],0);
  LONGSUM =LONGSUM + Lday[ii];
   {TOTAL ALL VALUES OF LOWS BEING LOWER THAN PRIOR LOW}
  COUNTLONG = COUNTLONG + IFF(Lday[ii]>0,1,0);
   {COUNT NUMBER OF DAYS WITH LOWS BEING LOWER THAN PRIOR LOW}
  End;
IF COUNTLONG <> 0 THEN AVGLOW = LONGSUM / COUNTLONG;
   {GET AVERAGE OF LOWS LOWER THAN PRIOR LOW}
LSTOP = LOW[1] - (MULTIPLELONG * AVGLOW[1]);
   {CALCULATE STOP AT "X" TIMES AVGLOW}
LONGSTOP = MAXLIST(LSTOP, LSTOP[1], LSTOP[2], LSTOP[3], LSTOP[4], LSTOP[5]);
{PREVENT STOP FROM BEING LOWERED}
IF CONDITION1 THEN PLOT1(LONGSTOP, "STOP", ColourLong);
   {SET PLOT}


{SafeZone for SHORTS}
SHORTSUM = 0;
COUNTSHORT = 0;
For ii=1 to Lookback Begin
  Sday[ii]=IFF(H[ii]<H[ii-1],H[ii-1]-H[ii],0);
  SHORTSUM =SHORTSUM + Sday[ii];
    {TOTAL ALL VALUES OF HIGHS BEING HIGHER THAN PRIOR HIGH}
  COUNTSHORT = COUNTSHORT + IFF(Sday[ii]>0,1,0);
    {COUNT NUMBER OF DAYS WITH HIGHS BEING HIGHER THAN PRIOR HIGH}
  End;
IF COUNTSHORT <> 0 THEN AVGHIGH = SHORTSUM / COUNTSHORT;
    {GET AVERAGE OF HIGHS HIGHER THAN PRIOR HIGH}
SSTOP = HIGH[1] + (MULTIPLESHORT * AVGHIGH[1]);
    {CALCULATE STOP AT "X" TIMES AVGHIGH}
SHORTSTOP = MINLIST(SSTOP, SSTOP[1], SSTOP[2], SSTOP[3], SSTOP[4],
SSTOP[5]); {PREVENT STOP FROM RISING}
IF CONDITION2 THEN PLOT1(SHORTSTOP, "STOP", ColourShort);
    {SET PLOT}




Attachment: Description: "Elders SafeStop.ELA"