From Alexander Elder.
Provides good stop placement by determining the average upside & downside penetration over a specific number of days (lookback period), multiplying this figure by a selected constant (Factor) then either minusing this from todays low (uptrend) or adding onto todays high (downtrend).
Factor (f = 2)
Lookback (d = 10)
REM Variable definition
Factor = f
Lookback = d
dnPen = LOW - LOW[1]
upPen = HIGH - HIGH[1]
REM SPalte E und L
IF dnPen > 0 THEN
dnPenCount = 1
ELSE
dnPen = 0
dnPenCount = 0
ENDIF
IF upPen > 0 THEN
upPenCount =1
ELSE
upPen = 0
upPenCount = 0
ENDIF
REM Spalte I und P
dnAvg = Summation[Lookback](dnPen) / SUMMATION[Lookback](dnPenCount)
upAvg = Summation[Lookback](upPen) / SUMMATION[Lookback](upPenCount)
REM SPlate J und Q
bullStop = LOW - (Factor * dnAvg)
bearStop = HIGH + (Factor * upAvg)
REM Spalte K und R
bullzw = MAX(bullStop[1], bullStop[2])
bearzw = MIN(bearStop[1], bearStop[2])
protectedBull = MAX (bullStop, bullzw)
protectedBear = MIN (bearStop, bearzw)
RETURN bullStop AS"Bull STOP", bearStop AS"Bear STOP", protectedBull AS"protected Bull", protectedBear AS"Protected Bear"