I don’t really get your point 1. My array is not sorted, so if I want to compare one item with all items, I can’t do z=x, otherwise you compare only with smaller and smaller items.
On the other hand, your second point seems indeed to solve the issue, thx I wouldn’t have found it!!!
Now the point I would like to improve is around the fractal, to filter the reversal points and retain only the most relevant one… Would need your help on that too;)
I’m noticing also that the most recent overlap is sometimes blinking with each new change of price, weird…
DEFPARAM DRAWONLASTBARONLY =TRUE
defparam calculateonlastbars=500
barlimit = 400
ATR=averagetruerange[20](close)
cp=2
ATRfactor=2
index1=0
index2=0
if high[cp] >= highest[(cp)*2+1](high) and barindex-$TOPx2[max(0,lastset($TOPx2))]>1 then //new fractal high found
$TOPy[lastset($TOPy)+1] = high[cp] //store fractal value
$TOPx[lastset($TOPx)+1] = barindex[cp] //store fractal barindex
//drawpoint(barindex[cp],high[cp],1) coloured(100,75,150) bordercolor (100,75,150)
$TOPx2[lastset($TOPx2)+1] = barindex
endif
if low[cp] <= lowest[(cp)*2+1](low) and barindex-$BOTx2[max(0,lastset($BOTx2))]>1 then //new fractal low found
$BOTy[lastset($BOTy)+1] = low[cp] //store fractal value
$BOTx[lastset($BOTx)+1] = barindex[cp] //stire fractal barindex
//drawpoint(barindex[cp],low[cp],1) coloured(100,75,150) bordercolor (100,75,150)
$BOTx2[lastset($BOTx2)+1] = barindex
endif
if(islastbarupdate and isset($topy[0]) and isset($boty[0])) then
//check points in a range of X percent
for i = 0 to lastset($TOPy) do //loop through the tops
for y = 0 to lastset($BOTy) do //check first top with other tops
if y<>i then
change=abs($BOTy[y]-$topy[i])//percent range between the 2 tops
if change<=ATR/ATRfactor and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then
$overlap[index1]=($BOTy[y]+$TOPy[i])/2
$overlapx1[index1]=min($BOTx[y],$TOPx[i])
$overlapx2[index1]=max($BOTx[y],$TOPx[i])
index1=index1+1
endif
endif
next
next
endif
//test=index1
// drawtext(" #test#",barindex,high+50,SansSerif,standard,10)
if islastbarupdate and isset($overlap[0]) then
for x=index1 downto 1 do
for z=index1 downto 1 do
if x=index1 then
count=0
endif
if x<>z then
if abs($overlap[x]-$overlap[z])<ATR then
count=count+1
endif
endif
next
if count=0 then
$overlapfinal[index2]=$overlap[x]
$overlapfinalx1[index2]=$overlapx1[x]
$overlapfinalx2[index2]=$overlapx2[x]
index2=index2+1
else
if index2=0 then
$overlapfinal[index2]=$overlap[x]
$overlapfinalx1[index2]=$overlapx1[x]
$overlapfinalx2[index2]=$overlapx2[x]
index2=index2+1
endif
for zz = 0 to index2 do
if zz=0 then
count=0
endif
if x<>zz then
if abs($overlap[x]-$overlapfinal[zz])<ATR then
count=count+1
endif
endif
next
if count=0 then
$overlapfinal[index2]=$overlap[x]
$overlapfinalx1[index2]=$overlapx1[x]
$overlapfinalx2[index2]=$overlapx2[x]
index2=index2+1
endif
endif
next
endif
if islastbarupdate and isset($overlapfinal[0]) then
for x=0 to index2 do
if $overlapfinal[x] > close then
if highest[barindex-$overlapfinalx2[x]-1](high)<$overlapfinal[x]+ATR/ATRfactor then
drawsegment($overlapfinalx1[x],$overlapfinal[x],barindex,$overlapfinal[x]) coloured(0,75,150) STYLE (dottedline,3)
drawpoint($overlapfinalx2[x],$overlapfinal[x],2) coloured(0,75,150) bordercolor (0,75,150)
endif
else
if lowest[barindex-$overlapfinalx2[x]-1](low)>$overlapfinal[x]-ATR/ATRfactor then
drawsegment($overlapfinalx1[x],$overlapfinal[x],barindex,$overlapfinal[x]) coloured(0,75,150) STYLE (dottedline,3)
drawpoint($overlapfinalx2[x],$overlapfinal[x],2) coloured(0,75,150) bordercolor (0,75,150)
endif
endif
//drawsegment($overlapfinalx1[x],$overlapfinal[x],barindex,$overlapfinal[x]) coloured(0,75,150) STYLE (dottedline,3)
//drawpoint($overlapfinalx2[x],$overlapfinal[x],2) coloured(0,75,150) bordercolor (0,75,150)
next
endif
//plot the zone
return