Those with PowerPivots might try this:
PPivot:=Input("P 1=minor 2=intermediate 3=major 4=primary",1,4,2);
TPivot:=Input("T -1=minor -2=intermediate -3=major -4=primary",-4,-
1,-2);
Mlt:=Input("ATR multiplier",0,10,0);
FTrough := LastValue( ExtFml("Powerpivots.NthPivotPrice",Tpivot,0,1));
STrough := LastValue( ExtFml("Powerpivots.NthPivotPrice",Tpivot,1,1));
DeltaVerticalU := FTrough - STrough;
DeltaHorizontalU :=
LastValue(ExtFml("Powerpivots.BarsSinceNthPivot",Tpivot,1)) -
LastValue(ExtFml("Powerpivots.BarsSinceNthPivot",Tpivot,0));
DeltaPerBarU := DeltaVerticalU / DeltaHorizontalU;
BarNoSecondTrough := LastValue(
Cum(1) - ExtFml("Powerpivots.BarsSinceNthPivot",Tpivot,1));
TrendD := If(Cum(1) < BarNoSecondTrough,
BarsSince(Cum(1) >= BarNoSecondTrough),
STrough +
(DeltaPerBarU * (Cum(1) - BarNoSecondTrough)));
Up:=If(FTrough >= STrough,
TrendD,
BarsSince(FTrough >= STrough));
FPeak := LastValue( ExtFml("Powerpivots.NthPivotPrice",Ppivot,0,1));
SPeak := LastValue( ExtFml("Powerpivots.NthPivotPrice",Ppivot,1,1));
DeltaVerticalD := FPeak - SPeak;
DeltaHorizontalD :=
LastValue(ExtFml("Powerpivots.BarsSinceNthPivot",Ppivot,1)) -
LastValue(ExtFml("Powerpivots.BarsSinceNthPivot",Ppivot,0));
DeltaPerBarD := DeltaVerticalD / DeltaHorizontalD;
BarNoSecondPeak := LastValue(
Cum(1) - ExtFml("Powerpivots.BarsSinceNthPivot",Ppivot,1));
TrendU := If(Cum(1) < BarNoSecondPeak,
BarsSince(Cum(1) >= BarNoSecondPeak),
SPeak +
(DeltaPerBarD * (Cum(1) - BarNoSecondPeak)));
Dn:=If(FPeak <= SPeak,
TrendU,
BarsSince(FPeak <= SPeak));
X:=If(PPivot=1,ATR(5),If(PPivot=2,ATR(20),ATR(40)));
Up+mlt*x;Up-Mlt*x;Dn+mlt*x;Dn-Mlt*x;Up;Dn
do not remember where I got this from. I added an allowance for atr margins above and below the trend lines.