Tops Bottoms - Pivot Hs and Pivot Ls - How to pass on value

#1
Hi,

I am trying to highlight Pivot Highs and Pivot Lows on the chart. For which i am using this simple code:

PHP:
pivh = H > Ref(H,-1) AND H > Ref(H,-2) AND H > Ref(H,-3) AND H > Ref(H,1) AND H > Ref(H,2) ;
pivl = L < Ref(L,-1) AND L < Ref(L,-2) AND L < Ref(L,-3) AND L < Ref(L,1) AND L < Ref(L,2) ;
PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12); 
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
And the output is this: (Pls read below the pic)


--------------------------------------------------
-- PROBLEM ---
Many times there are 2 or more consecutive Pivot Highs
or 2 or more consecutive Pivot Lows


-- NEEDED ---
1. Pivot High and Pivot Low should alternate.

2.
PivH -
If there are two consecutive Pivot Hs then the one which is Bigger should be highlighted with Red dot and the smaller one should be dropped off / not considered any more.

PivL -
If there are two consecutive Pivot Ls then the one which is Smaller should be highlighted with Green dot and the Bigger one should be dropped off / not considered any more.

--------------------------------------------------

I am not a techie but keep trying to code. And below is what i came up with to solve the issue. Although, my solution is INCOMPLETE and could be raw or outright incorrect.

PHP:
x = 0;
pivh = H > Ref(H,-1) AND H > Ref(H,-2) AND H > Ref(H,-3) AND H > Ref(H,1) AND H > Ref(H,2) ;
pivl = L < Ref(L,-1) AND L < Ref(L,-2) AND L < Ref(L,-3) AND L < Ref(L,1) AND L < Ref(L,2) ;
a = pivh;
b = pivl;
a=Flip(a,b);
b=Flip(b,a);
x = IIf(a,x=1,IIf(b,x= -1,0));

pivhnew = IIf(pivh AND (Ref(x,-1) == 1), ValueWhen(

PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12); 
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
Hence, i request for help.

Could someone please help me complete this code ?

Would be grateful for your help.

Regards.
 
Last edited:

mastermind007

Well-Known Member
#2
Hi,

I am trying to highlight Pivot Highs and Pivot Lows on the chart. For which i am using this simple code:

PHP:
pivh = H > Ref(H,-1) AND H > Ref(H,-2) AND H > Ref(H,-3) AND H > Ref(H,1) AND H > Ref(H,2) ;
pivl = L < Ref(L,-1) AND L < Ref(L,-2) AND L < Ref(L,-3) AND L < Ref(L,1) AND L < Ref(L,2) ;
PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12); 
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
And the output is this: (Pls read below the pic)


--------------------------------------------------
-- PROBLEM ---
Many times there are 2 or more consecutive Pivot Highs
or 2 or more consecutive Pivot Lows


-- NEEDED ---
1. Pivot High and Pivot Low should alternate.

2.
PivH -
If there are two consecutive Pivot Hs then the one which is Bigger should be highlighted with Green dot and the smaller one should be dropped off / not considered any more.

PivL -
If there are two consecutive Pivot Ls then the one which is Smaller should be highlighted with Red dot and the Bigger one should be dropped off / not considered any more.

--------------------------------------------------

I am not a techie but keep trying to code. And below is what i came up with to solve the issue. Although, my solution is INCOMPLETE and could be raw or outright incorrect.

PHP:
x = 0;
pivh = H > Ref(H,-1) AND H > Ref(H,-2) AND H > Ref(H,-3) AND H > Ref(H,1) AND H > Ref(H,2) ;
pivl = L < Ref(L,-1) AND L < Ref(L,-2) AND L < Ref(L,-3) AND L < Ref(L,1) AND L < Ref(L,2) ;
a = pivh;
b = pivl;
a=Flip(a,b);
b=Flip(b,a);
x = IIf(a,x=1,IIf(b,x= -1,0));

pivhnew = IIf(pivh AND (Ref(x,-1) == 1), ValueWhen(

PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12); 
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
Hence, i request for help.

Could someone please help me complete this code ?

Would be grateful for your help.

Regards.
Requirement one can be fulfilled with ExRem, but not flip;

pivh = H > Ref(H,-1) AND H > Ref(H,-2) AND H > Ref(H,-3) AND H > Ref(H,1) AND H > Ref(H,2) ;
pivl = L < Ref(L,-1) AND L < Ref(L,-2) AND L < Ref(L,-3) AND L < Ref(L,1) AND L < Ref(L,2) ;
pivh = ExRem(pivh, pivl);
pivl = ExRem(pivl, pivh);
PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12);
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
Requirement two and three will need a loop to actually sift thru data.

To keep it simple and to avoid reinventing the wheel, use

pivh = Peak(High, 0.5, 1);
pivl = Trough(Low, 0.5, 1);
PlotShapes(IIf(pivh, shapeSmallCircle, shapeNone),ParamColor("Normal Up Arrow Color",colorWhite), 0,H, Offset=12);
PlotShapes(IIf(pivl, shapeSmallCircle, shapeNone),ParamColor("Normal Dn Arrow Color",colorYellow), 0,L, Offset=-12);
 
#3
If i am not wrong, the Zig approach is just to define Pivots using Percent Change. And, i can go with either approaches. But i think that is not the problem i am facing..

Whether by Zig Approach or by "H > Ref(H, ... " approach could someone please help with point 1 and 2.. please..

Regards.
 
#4
If i am not wrong, the Zig approach is just to define Pivots using Percent Change. And, i can go with either approaches. But i think that is not the problem i am facing..

Whether by Zig Approach or by "H > Ref(H, ... " approach could someone please help with point 1 and 2.. please..

Regards.
i made something similar as ZIG but using ATR instead. When used with intraday data you can also define the timeframe. See for further modifications the Parameter window. You need the latest version of Amibroker

PHP:
// E.M.Pottasch, Jan 2015
x=BarIndex();Lx=LastValue(x); 
per1=Param("Length ATR",20,1,150,1); // ATR length
fac1=Param("Chandelier Factor",2,1,10,0.1); // chandelier factor
tog1=ParamToggle("Trail value","Close|High&Low",0);
tf=Param("Time Frame (min)",1,1,1440,1);tfrm=in1Minute*tf;
tog2=ParamToggle("Plot Trail","No|Yes",0);
tog3=ParamToggle("Plot Valid High/Low","No|Yes",1);
rightStrength_mini=Param("Right Strength (Mini Pivot)",5,1,50,1);
leftStrength_mini=Param("Left Strength (Mini Pivot)",5,1,50,1);

function pkID(rightStrength,leftStrength)
{
	pk=H>Ref(HHV(H,leftStrength),-1) AND H>=Ref(HHV(H,rightStrength),rightStrength);
	return pk;
}
function trID(rightStrength,leftStrength)
{
	tr=L<Ref(LLV(L,leftStrength),-1) AND L<=Ref(LLV(L,rightStrength),rightStrength);
	return tr;
}
///////////// Chandelier code by Geoff Mulhall
function aChandelierCl(AtrARRAY, AtrMult) {
// Skip empty values
i = 0;
do {result[i] = Null;
	i++;
	} 
while( i < BarCount AND (IsNull(O[i]) OR IsNull(H[i]) OR IsNull(L[i]) OR
IsNull(C[i]) ) ); 
First = i;

if (i < BarCount - 1) {
	HHC[First]    = C[First];
	LLC[First]    = C[First];

	if (C[First + 1] > HHC[First]) {
		HHC[First + 1] = C[First + 1];
		LLC[First + 1] = LLC[First];
		result[First] = C[First] - AtrMult * AtrARRAY[First];
		iTrade = "LT";
	}
	else {
		if (C[First + 1] < LLC[First]) {
			HHC[First = 1] = HHC[First];
			LLC[First + 1] = LLC[First + 1];
			result[First] = C[First] + AtrMult * AtrARRAY[First];
			iTrade = "ST";
		}
		else {
			HHC[First + 1] = C[First + 1];
			LLC[First + 1] = C[First + 1];
			result[First] = C[First] - AtrMult * AtrARRAY[First];
			iTrade = "LT";
		}
	}

	for( i = First; i < BarCount; i++ ) {
		if (iTrade == "LT") {
			if (C[i] >= result[i-1]) {        // Long Trade is continuing
				if (C[i] > C[i-1]) {
					HHC[i] = C[i];
				}
				else { 
					HHC[i] = HHC[i-1];
				}
				result[i] = HHC[i] - AtrMult * AtrARRAY[i];
				if (result[i] < result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                            // Long trade Exit triggered
				iTrade = "ST";
				LLC[i] = C[i];
				result[i] = C[i] + AtrMult * AtrARRAY[i];
			}
		}
		else {                               // Short trade
			if (C[i] <= result[i-1]) {
				if (C[i] <= C[i-1]) {        // Short Trade is continuing
					LLC[i] = C[i];
				}
				else {
					LLC[i] = LLC[i-1];
				}
				result[i] = LLC[i] + AtrMult * AtrARRAY[i];
				if (result[i] > result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                           //Short Trade Exit is triggered
				iTrade = "LT";
				HHC[i]  = C[i];
				result[i] = C[i] - AtrMult * AtrARRAY[i];
			}
		}
	}
}
return result;
}
function aChandelierHL(AtrARRAY, AtrMult) {
// Skip empty values
i = 0;
do {result[i] = Null;
	i++;
	} 
while( i < BarCount AND (IsNull(O[i]) OR IsNull(H[i]) OR IsNull(L[i]) OR
IsNull(C[i]) ) ); 
First = i;

if (i < BarCount - 1) {
	HHC[First]    = H[First];
	LLC[First]    = L[First];

	if (H[First + 1] > HHC[First]) {
		HHC[First + 1] = H[First + 1];
		LLC[First + 1] = LLC[First];
		result[First] = H[First] - AtrMult * AtrARRAY[First];
		iTrade = "LT";
	}
	else {
		if (L[First + 1] < LLC[First]) {
			HHC[First = 1] = HHC[First];
			LLC[First + 1] = LLC[First + 1];
			result[First] = L[First] + AtrMult * AtrARRAY[First];
			iTrade = "ST";
		}
		else {
			HHC[First + 1] = C[First + 1];
			LLC[First + 1] = C[First + 1];
			result[First] = H[First] - AtrMult * AtrARRAY[First];
			iTrade = "LT";
		}
	}

	for( i = First; i < BarCount; i++ ) {
		if (iTrade == "LT") {
			if (C[i] >= result[i-1]) {        // Long Trade is continuing
				if (H[i] > H[i-1]) {
					HHC[i] = H[i];
				}
				else { 
					HHC[i] = HHC[i-1];
				}
				result[i] = HHC[i] - AtrMult * AtrARRAY[i];
				if (result[i] < result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                            // Long trade Exit triggered
				iTrade = "ST";
				LLC[i] = L[i];
				result[i] = L[i] + AtrMult * AtrARRAY[i];
			}
		}
		else {                               // Short trade
			if (C[i] <= result[i-1]) {
				if (L[i] <= L[i-1]) {        // Short Trade is continuing
					LLC[i] = L[i];
				}
				else {
					LLC[i] = LLC[i-1];
				}
				result[i] = LLC[i] + AtrMult * AtrARRAY[i];
				if (result[i] > result[i-1]) {
					result[i] = result[i-1];
				}
			}
			else {                           //Short Trade Exit is triggered
				iTrade = "LT";
				HHC[i]  = H[i];
				result[i] = H[i] - AtrMult * AtrARRAY[i];
			}
		}
	}
}
return result;
}
///////////// end Chandelier code by Geoff Mulhall

TimeFrameSet(tfrm);
	if(tog1) trailArray=aChandelierHL(ATR(per1),fac1);
	else trailArray=aChandelierCl(ATR(per1),fac1);
	ts=IIf(trailArray>C,trailArray,Null);
	tl=IIf(trailArray<C,trailArray,Null);	
TimeFrameRestore();
ts=TimeFrameExpand(ts,tfrm,expandlast);
tl=TimeFrameExpand(tl,tfrm,expandlast);

lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll;
ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl)));
qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq;
tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception

hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh;
ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts)));
sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss;
pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception

pkValid=IsEmpty(tl) AND !IsEmpty(Ref(tl,-1)) AND !IsEmpty(ts) AND IsEmpty(Ref(ts,-1));// valid at bar close
trValid=!IsEmpty(tl) AND IsEmpty(Ref(tl,-1)) AND IsEmpty(ts) AND !IsEmpty(Ref(ts,-1));// valid at bar close
pkHigh1=ValueWhen(pkValid,ValueWhen(pk,H,1),1);
trLow1=ValueWhen(trValid,ValueWhen(tr,L,1),1);

px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);

ll=tr AND tl1<tl2;
hl=tr AND tl1>tl2;
hh=pk AND ph1>ph2;
lh=pk AND ph1<ph2;
dt=pk AND ph1==ph2;
db=tr AND tl1==tl2;

pkm=pkID(rightStrength_mini,leftStrength_mini);
trm=trID(rightStrength_mini,leftStrength_mini);

GraphXSpace=5;SetChartBkColor(colorBlack);SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ColorRGB(0,75,0),IIf(C<=O,ColorRGB(75,0,0),colorLightGrey)));
Plot(C,"",IIf(C>O,ColorRGB(0,255,0),IIf(C<=O,ColorRGB(255,0,0),colorLightGrey)),64,Null,Null,0,0,1);

if(tog2)
{
	Plot(ts,"",colorRed,styleStaircase|styleNoRescale,Null,Null,0,1,1);
	Plot(llls,"",colorRed,styleDashed|styleNoRescale,Null,Null,0,0,1);
	Plot(tl,"",colorGreen,styleStaircase|styleNoRescale,Null,Null,0,1,1);
	Plot(hhhs,"",colorGreen,styleDashed|styleNoRescale,Null,Null,0,0,1);
}
if(tog3)
{
	Plot(pkHigh1,"",colorBlue,styleLine|styleNoRescale,Null,Null,0,0,1);
	Plot(trLow1,"",colorRed,styleLine|styleNoRescale,Null,Null,0,0,1);
}

PlotShapes(shapeCircle*tr,IIf(ValueWhen(tr,x,1)<=ValueWhen(trValid,x,0),ColorRGB(0,255,0),ColorRGB(255,255,255)),0,L,-10);
PlotShapes(shapeCircle*pk,IIf(ValueWhen(pk,x,1)<=ValueWhen(pkValid,x,0),ColorRGB(255,0,0),ColorRGB(255,255,255)),0,H,10);
PlotShapes(shapeSmallCircle*trm,IIf(Lx-ValueWhen(trm,x)>rightStrength_mini,ColorRGB(0,70,0),colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pkm,IIf(Lx-ValueWhen(pkm,x)>rightStrength_mini,ColorRGB(70,0,0),colorWhite),0,H,10);

fvb=Status("firstvisiblebarindex");
lvb=Min(Lx,Status("lastvisiblebarindex"));
for(i=fvb;i<=lvb;i++) 
{
	if(ll[i]) PlotTextSetFont("LL","Arial Black",8,i,L[i],colorGreen,colorDefault,-30); 
	if(hl[i]) PlotTextSetFont("HL","Arial Black",8,i,L[i],colorGreen,colorDefault,-30); 
	if(db[i]) PlotTextSetFont("DB","Arial Black",8,i,L[i],colorLightBlue,colorDefault,-30); 
	if(hh[i]) PlotTextSetFont("HH","Arial Black",8,i,H[i],colorRed,colorDefault,20); 
	if(lh[i]) PlotTextSetFont("LH","Arial Black",8,i,H[i],colorRed,colorDefault,20); 
	if(dt[i]) PlotTextSetFont("DT","Arial Black",8,i,H[i],colorOrange,colorDefault,20);
}
 

Similar threads