pivotlength=3
//Check if pivotlength is odd
if (pivotlength-1) mod 2 = 0 then
pivotlength=pivotlength
else
pivotlength=pivotlength+1
endif
atrmultiplier=2
atr=averagetruerange[(pivotlength-1)/2]
once $pivotIndices[0]=barindex
once $pivotValues[0]=high
once $pivotTypes[0]=1
once z=0
//-----------------------------------------------------//
//-----Looking for at least 1 instance where a pivot cannot be created
noHighPivot=0//a pivot can be created
noLowPivot=0//a pivot can be created
for i=pivotlength-1 to 1 do
//check left side bar
if i>(pivotlength-1)/2 then
if high[i]>high[i-1] then
noHighPivot=1//a pivot cannot be created
endif
if low[i]<low[i-1] then
noLowPivot=1//a pivot cannot be created
endif
endif
//check middle and right side bar
if i<=(pivotlength-1)/2 then
if high[i]<high[i-1] then
noHighPivot=1//a pivot cannot be created
endif
if low[i]>low[i-1] then
noLowPivot=1//a pivot cannot be created
endif
endif
next
//-----------------------------------------------------//
updatelast=0
if isset($pivotindices[z]) then
if $pivotTypes[z]=1 then
if not noHighpivot and high[(pivotlength-1)/2]>$pivotValues[z] then
$pivotIndices[z]=barindex-((pivotLength-1)/2)
$pivotValues[z]=high[(pivotLength-1)/2]
$pivotVolume[z]=summation[max(1,($pivotIndices[z]-$pivotIndices[max(0,z-1)]))](volume)
updatelast=1
endif
if not noLowPivot and abs(low[(pivotlength-1)/2]-$pivotValues[z])>=atr*atrMultiplier and (barindex-(pivotlength-1)/2) <> $pivotIndices[z] and low[(pivotlength-1)/2]<$pivotValues[z] then
$pivotIndices[z+1]=barindex-((pivotLength-1)/2)
$pivotValues[z+1]=LOW[(pivotLength-1)/2]
$pivotTypes[z+1]=-1
$pivotVolume[z+1]=summation[($pivotIndices[z+1]-$pivotIndices[z])](volume)
z=z+1
endif
else
if not noLowpivot and low[(pivotlength-1)/2]<$pivotValues[z] then
$pivotIndices[z]=barindex-((pivotLength-1)/2)
$pivotValues[z]=low[(pivotLength-1)/2]
$pivotVolume[z]=summation[max(1,($pivotIndices[z]-$pivotIndices[max(0,z-1)]))](volume)
updatelast=1
endif
if not noHighPivot and abs(high[(pivotlength-1)/2]-$pivotValues[z])>=atr*atrMultiplier and (barindex-(pivotlength-1)/2) <> $pivotIndices[z] and high[(pivotlength-1)/2]>$pivotValues[z] then
$pivotIndices[z+1]=barindex-((pivotLength-1)/2)
$pivotValues[z+1]=high[(pivotLength-1)/2]
$pivotTypes[z+1]=1
$pivotVolume[z+1]=summation[($pivotIndices[z+1]-$pivotIndices[z])](volume)
z=z+1
endif
endif
else //If it will be the first Pivot
if not noHighPivot then
$pivotIndices[z+1]=barindex-((pivotLength-1)/2)
$pivotValues[z+1]=high[(pivotLength-1)/2]
$pivotTypes[z+1]=1
$pivotVolume[z+1]=summation[($pivotIndices[z+1]-$pivotIndices[z])](volume)
z=z+1
endif
if not noLowPivot then
$pivotIndices[z+1]=barindex-((pivotLength-1)/2)
$pivotValues[z+1]=LOW[(pivotLength-1)/2]
$pivotTypes[z+1]=-1
$pivotVolume[z+1]=summation[($pivotIndices[z+1]-$pivotIndices[z])](volume)
z=z+1
endif
endif
//Get Trend
if z>=2 then
if $pivotTypes[z]=1 and $pivotValues[z] > $pivotValues[z-2] then
$trend[z]=2
elsif $pivotTypes[z]=1 and $pivotValues[z] <= $pivotValues[z-2] then
$trend[z]=1
elsif $pivotTypes[z]=-1 and $pivotValues[z] < $pivotValues[z-2] then
$trend[z]=-2
elsif$pivotTypes[z]=-1 and $pivotValues[z] >= $pivotValues[z-2] then
$trend[z]=-1
endif
else
$trend[z]=0
endif
//-----------------------------------------------------//
if islastbarupdate then
for j=z downto 1 do
vol=$pivotVolume[j]
if $pivotTypes[j]=1 and $trend[j]=2 then
drawtext("HH",$pivotIndices[j],$pivotValues[j]+atr)
drawtext("#vol#",$pivotIndices[j],$pivotValues[j]+2*atr)
drawsegment($pivotIndices[j-1],$pivotValues[j-1],$pivotIndices[j],$pivotValues[j])style(line,2)coloured("orange")
elsif $pivotTypes[j]=1 and $trend[j]=1 then
drawtext("LH",$pivotIndices[j],$pivotValues[j]+atr)
drawtext("#vol#",$pivotIndices[j],$pivotValues[j]+2*atr)
drawsegment($pivotIndices[j-1],$pivotValues[j-1],$pivotIndices[j],$pivotValues[j])style(line,2)coloured("orange")
elsif $pivotTypes[j]=-1 and $trend[j]=-2 then
drawtext("LL",$pivotIndices[j],$pivotValues[j]-atr)
drawtext("#vol#",$pivotIndices[j],$pivotValues[j]-2*atr)
drawsegment($pivotIndices[j-1],$pivotValues[j-1],$pivotIndices[j],$pivotValues[j])style(line,2)coloured("orange")
elsif $pivotTypes[j]=-1 and $trend[j]=-1 then
drawtext("HL",$pivotIndices[j],$pivotValues[j]-atr)
drawtext("#vol#",$pivotIndices[j],$pivotValues[j]-2*atr)
drawsegment($pivotIndices[j-1],$pivotValues[j-1],$pivotIndices[j],$pivotValues[j])style(line,2)coloured("orange")
endif
next
endif
//-----------------------------------------------------//
return