Another useful AFL for plotting peaks & troughs:
/*PLOTDYNHI*/
/*DYNAMICALLY PLOTS HIGHEST VALUE FOR THE PERIOD*/
//Suitable for overlaying price(C)charts
//DoubleClick left mouse button on any bar in the chart to select the
period
//DoubleClick left mouse button on space to the right of the chart to
deselect the period
//If no period is selected the default is all bars
P = (LastValue(BarIndex()) - BeginValue(BarIndex())) + 1;//Periods
PH = HHV(C,P);//PeriodHigh
SD = IIf(DateNum() < BeginValue(DateNum()), Null, LastValue
(PH));//SelectedDate
Plot(SD, "PeriodHi",34,8);
******************************************************************
/*P_DYNHIPEAK*/
/*DYNAMICALLY PLOTS HIGHEST 3 Bar PEAK VALUE FOR THE PERIOD*/
/*Suitable for overlaying price(C)charts*/
/*DoubleClick left mouse button on any bar in the chart to select the
period*/
/*DoubleClick left mouse button on space to the right of the chart to
deselect the period*/
/*If no bar is selected the defaults is all bars*/
/*Caution: it will not see peaks with flat tops (two or more equal
closes)*/
/*Caution:if there is no peak within the selected period the most
recent peak will be indicated*/
PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
PKV = IIf(PK == 1, ValueWhen(PK,C,1), ValueWhen(PK,C,0));//PeakValue
P = (LastValue(BarIndex()) - BeginValue(BarIndex())) + 1;//Periods
HPK = HHV(PKV,P);//HighestPeakValue
SD = IIf(DateNum() < BeginValue(DateNum()), Null, LastValue
(HPK));//SelectedDate
Plot(SD, "PeriodHiPeak",34,8);
********************************************************************
//PLOTMAJORPEAKS
//PLOTS ALL MAJOR PEAK RESISTANCE LINES
//Suitable for overlaying charts
//A MajorPeak is defined as the Highest Peak of any three Peaks
PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
PKV0 = ValueWhen(PK,C,0);//PeakValue0
PKV1 = ValueWhen(PK,C,1);//PeakValue1
PKV2 = ValueWhen(PK,C,2);//PeakValue2
MPK = PKV2 < PKV1 AND PKV1 > PKV0 ;//MajorPeak
MPKV = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1,
PKV1,1); //MajorPeakValue
Plot(MPKV, "LastPeak",34,1);
**********************************************************************
/*P_DYNHIPEAK*/
/*DYNAMICALLY PLOTS HIGHEST 3 Bar PEAK VALUE FOR THE PERIOD*/
/*Suitable for overlaying price(C)charts*/
/*DoubleClick left mouse button on any bar in the chart to select the
period*/
/*DoubleClick left mouse button on space to the right of the chart to
deselect the period*/
/*If no bar is selected the defaults is all bars*/
/*Caution: it will not see peaks with flat tops (two or more equal
closes)*/
/*Caution:if there is no peak within the selected period the most
recent peak will be indicated*/
PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
PKV = IIf(PK == 1, ValueWhen(PK,C,1), ValueWhen(PK,C,0));//PeakValue
P = (LastValue(BarIndex()) - BeginValue(BarIndex())) + 1;//Periods
HPK = HHV(PKV,P);//HighestPeakValue
SD = IIf(DateNum() < BeginValue(DateNum()), Null, LastValue
(HPK));//SelectedDate
Plot(SD, "PeriodHiPeak",34,8);
**********************************************************
//P_3BarPeaks
//Suitable for overlaying charts
//References future bars
//PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
//PKV = ValueWhen(PK,C,1);//PeakValue
//Plot(PKV, "LastPeak",34,1);
/********************************************************************/
/* draw horizontal lines at hi & lo pivot points*/
//JohnRUK TraderJUK
//Shifts resistance line 1 bar
//3 Bar peaks
//P = ParamField("Price field",-1);
//Hipiv = IIf(P<Ref(P,-1) AND (Ref(P,-1)>Ref(P,-2)),1,0);
//Lopiv = IIf(P>Ref(P,-1) AND (Ref(P,-1)<Ref(P,-2)),1,0);
//hiline=ValueWhen(hipiv==1,Ref(P,-1),1);
//loline=ValueWhen(lopiv==1,Ref(P,-1),1);
//Plot (Hiline,"hiline",colorRed);
//Plot (Loline,"loline",colorBlue);
/*********************************************************************/
/*P_Peaks*/
/*PLOTALLPEAKS*/
/*PLOTS ALL PEAK VALUES FOR THE PERIOD*/
//Suitable for overlaying price(C)charts
//References future bars
//Recognises flat peaks of any length
PKR = ROC(C,1) >= 0 AND Ref(ROC(C,1),1) < 0 ;//PeakRight
PKRV = ValueWhen(PKR == 1, C,1);//PeakRightValue
PKL = ROC(C,1) > 0 AND Ref(ROC(C,1),1) <= 0 ;//PeakLeft
BSPKL = BarsSince(PKL == 1);//BarsSincePeakLeft
PKP = ValueWhen(PKR == 1, BSPKL,1);//PeakPeriods
PKPH = HHV(C,PKP + 1); //PeakPeriodHigh
PKV = ValueWhen(PKRV == PKPH AND PKR == 1, PKRV,1);//PeakValue
Plot(PKV, "PeakHi",34,8);
*****************************************************************
/*P_HHV*/
P = Param("periods",7,0,50,1);
H = HHV(C,P);
Plot(H,"HHV",34,1);
*******************************************************************
//PLOTLASTPEAK
//Suitable for overlaying charts
//Once overlayed on one chart use the keyboard arrows to scroll up or
down through symbol lists
//SelectedDate ignores dates (bars) before the peak
//Add additional resistance lines if a deeper history is required
//Code variations can identify major peaks or change the timeframe
upwards
//PLOTS THE MOST RECENT PEAK RESISTANCE LINE
PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
PKV = ValueWhen(PK,C,1);//PeakValue
PKD = ValueWhen(PK,DateNum(),1);//PeakDate
SD = IIf(DateNum() < LastValue(PKD,lastmode = True ), Null, LastValue
(PKV,Lastmode = True));//SelectedDate
Plot(SD, "LastPeak",34,8);
//PLOT 2ND LAST PEAK RESISTANCE LINE
PKV2 = ValueWhen(PK,C,2);//PeakValue2
PKD2 = ValueWhen(PK,DateNum(),2);//PeakDate2
SD2 = IIf(DateNum() < LastValue(PKD2,lastmode = True ), Null, LastValue
(PKV2,Lastmode = True));//SelectedDate
Plot(SD2, "LastPeak2",34,8);
****************************************************************
//PLOTCurrentMAJORPEAKS
//Suitable for overlaying charts
//A MajorPeak is defined as the Highest Peak central to any three Peaks
//PLOT THE LAST MAJOR PEAK RESISTANCE LINE
PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
PKV0 = ValueWhen(PK,C,0);//PeakValue0
PKV1 = ValueWhen(PK,C,1);//PeakValue1
PKV2 = ValueWhen(PK,C,2);//PeakValue2
MPK = PKV2 < PKV1 AND PKV1 > PKV0 ;//MajorPeak
MPKV = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1,
PKV1,1); //MajorPeakValue
MPKD = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum
(),1); //MajorPeakDate
SD = IIf(DateNum() < LastValue(MPKD,lastmode = True ), Null, LastValue
(MPKV,Lastmode = True));//SelectedDate
Plot(SD, "LastMajorPeak",34,8);
//PLOT THE SECOND LAST MAJOR PEAK RESISTANCE LINE
//PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
//PKV0 = ValueWhen(PK,C,0);//PeakValue0
//PKV1 = ValueWhen(PK,C,1);//PeakValue1
//PKV2 = ValueWhen(PK,C,2);//PeakValue2
//MPK = PKV2 < PKV1 AND PKV1 > PKV0 ;//MajorPeak
MPKV2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1,
PKV1,2); //MajorPeakValue
MPKD2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum
(),2); //MajorPeakDate
SD2 = IIf(DateNum() < LastValue(MPKD2,lastmode = True ), Null, LastValue
(MPKV2,Lastmode = True));//SelectedDate
Plot(SD2, "LastMajorPeak",34,8);
******************************************************************
//PLOT ALL PEAKS
//Suitable for overlaying charts
//References future bars
//PK = C > Ref(C,-1) AND Ref(C,1) < C;//Peak
//PKV = ValueWhen(PK,C,1);//PeakValue
//Plot(PKV, "LastPeak",34,1);
/********************************************************************/
/* draw horizontal lines at hi & lo pivot points*/
//JohnRUK TraderJUK
//Shifts resistance line 1 bar
//P = ParamField("Price field",-1);
//Hipiv = IIf(P<Ref(P,-1) AND (Ref(P,-1)>Ref(P,-2)),1,0);
//Lopiv = IIf(P>Ref(P,-1) AND (Ref(P,-1)<Ref(P,-2)),1,0);
//hiline=ValueWhen(hipiv==1,Ref(P,-1),1);
//loline=ValueWhen(lopiv==1,Ref(P,-1),1);
//Plot (Hiline,"hiline",colorRed);
//Plot (Loline,"loline",colorBlue);
/*********************************************************************/
/*P_TROUGHS*/
/*PLOTS ALL TROUGH VALUES FOR THE PERIOD*/
//Suitable for overlaying price(C)charts
//References future bars
//Recognises flat troughs of any length
TR = ROC(C,1) <= 0 AND Ref(ROC(C,1),1) > 0 ;//TroughRight
TRV = ValueWhen(TR == 1, C,1);//TroughRightValue
TL = ROC(C,1) < 0 AND Ref(ROC(C,1),1) >= 0 ;//TroughLeft
BSTL = BarsSince(TL == 1);//BarsSinceTroughLeft
TP = ValueWhen(TR == 1, BSTL,1);//PeakPeriods
TPL = LLV(C,TP + 1); //TroughPeriodLow
TV = ValueWhen(TRV == TPL AND TR == 1, TRV,1);//TroughValue
Plot(TV, "TroughLo",32,8);
*****************************************************************
/*PLOTSALLMAJORTROUGHS*/
/*PLOTS ALL MAJOR TROUGH VALUES FOR THE PERIOD*/
//Suitable for overlaying price(C)charts
//References future bars
//Recognises flat troughs of any length
//A Major Trough comprises any two Troughs where a Trough is followed
by a higher Trough
TR = ROC(C,1) <= 0 AND Ref(ROC(C,1),1) > 0 ;//TroughRight
TRV = ValueWhen(TR == 1, C,1);//TroughRightValue
TL = ROC(C,1) < 0 AND Ref(ROC(C,1),1) >= 0 ;//TroughLeft
BSTL = BarsSince(TL == 1);//BarsSinceTroughLeft
TP = ValueWhen(TR == 1, BSTL,1);//TroughPeriods
TPL = LLV(C,TP + 1); //TroughPeriodLow
//TV = ValueWhen(TRV == TPL AND TR == 1, TRV,1);//TroughValue
//MTV = ValueWhen(TV < Ref(TV,1),TV,1);
TV0 = ValueWhen(TRV == TPL AND TR == 1, TRV,0);//TroughValue0
TV1 = ValueWhen(TRV == TPL AND TR == 1, TRV,1);//TroughValue1
TV2 = ValueWhen(TRV == TPL AND TR == 1, TRV,2);//TroughValue2
TV3 = ValueWhen(TRV == TPL AND TR == 1, TRV,3);//TroughValue3
MT = TV2 > TV1 AND TV1 < TV0 OR TV3 > TV2 AND TV2 == TV1 AND TV1 <
TV0; ; //MajorTrough
MTV = ValueWhen(Ref(MT,-1) == 0 AND MT == 1,
TV1,1); //MajorTroughValue
Plot(MTV, "MajorTroughLo",32,8);