PureBytes Links
Trading Reference Links
|
> 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
=====
inputs:
Price((H+L)/2), MALen(22), Nbars(15), MULTIPLELONG(1), MULTIPLESHORT(1);
variables:
MA(0), Ldiff(0), Lday(0), Sdiff(0), Sday(0),
LONGSUM(0), SHORTSUM(0) , COUNTLONG(0), COUNTSHORT(0), AVGLOW(0),
AVGHIGH(0), LONGSTOP(0), SHORTSTOP(0),
LSTOP(0), SSTOP(0);
{determine trend by slope of EMA}
MA = xaverage( Price, MAlen );
CONDITION1 = MA > MA[3];
CONDITION2 = MA < MA[3];
Ldiff = iff(L[1] > L, L[1] - L, 0); { Difference of lows }
Lday = iff(L[1] > L, 1, 0); { 1 if this is a "lows" day }
Sdiff = iff(H[1] < H, H - H[1], 0);
Sday = iff(H[1] < H, 1, 0);
{SafeZone for Longs}
{GET AVERAGE OF LOWS LOWER THAN PRIOR LOW}
Longsum = summation(Ldiff, Nbars);
Countlong = summation(Lday, Nbars);
IF COUNTLONG <> 0 THEN
AVGLOW = LONGSUM / COUNTLONG;
{CALCULATE STOP AT "X" TIMES AVGLOW}
LSTOP = LOW[1] - (MULTIPLELONG * AVGLOW[1]);
{PREVENT STOP FROM BEING LOWERED}
LONGSTOP = MAXLIST(LSTOP, LSTOP[1], LSTOP[2], LSTOP[3], LSTOP[4], LSTOP[5]);
IF CONDITION1 THEN
PLOT1(LONGSTOP, "LSTOP");
{SafeZone for SHORTS}
{GET AVERAGE OF HIGHS HIGHER THAN PRIOR HIGH}
SHORTSUM = summation(Sdiff, Nbars);
COUNTSHORT = summation(Sday, Nbars);
IF COUNTSHORT <> 0 THEN
AVGHIGH = SHORTSUM / COUNTSHORT;
{CALCULATE STOP AT "X" TIMES AVGHIGH}
SSTOP = HIGH[1] + (MULTIPLESHORT * AVGHIGH[1]);
{PREVENT STOP FROM RISING}
SHORTSTOP = MINLIST(SSTOP, SSTOP[1], SSTOP[2], SSTOP[3], SSTOP[4], SSTOP[5]);
IF CONDITION2 THEN
PLOT2(SHORTSTOP, "SSTOP");
|