ProRealCode - Trading & Coding with ProRealTime™
Hello,
I copied the code posted by Nicolas in his arrays post and tried to improve it. Unfortunately I couldn’t;)
In detail, I modified 3 points:
Does someone knows how to fix the third point? I think the error is around the part:
for x=0 to lastset($overlap) do
if abs(($BOTy[y]+$TOPy[i])/2 – $overlap[x])>percent*2 then
count=count+1
endif
if count=lastset($overlap) then
….
defparam calculateonlastbars=200
barlimit = 200
percent=0.1
//the fractal code to find top/bottoms
if low[1]>low[4] and low[2]>low[4] and low[3]>low[4] and low[4]<low[5] and low[4]<low[6] and low[4]<low[7] then
$BOTy[lastset($BOTy)+1] = close[4] //store fractal value
$BOTx[lastset($BOTx)+1] = barindex[4] //stire fractal barindex
endif
if high[1]<high[4] and high[2]<high[4] and high[3]<high[4] and high[4]>high[5] and high[4]>high[6] and high[4]>high[7] then
$TOPy[lastset($TOPy)+1] = close[4] //store fractal value
$TOPx[lastset($TOPx)+1] = barindex[4] //store fractal barindex
endif
if(islastbarupdate and isset($topy[0]) and isset($boty[0])) then
for i = 0 to lastset($TOPy) do //loop through the tops
for y = 0 to lastset($BOTy) do //check each bottom
change=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between a top and a bottom
if change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then
if isset($overlap[0]) then //if the array is not empty
count=0
for x=0 to lastset($overlap) do // loop through the array of level
if abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])>percent*2 then // if the new level has enough space with those existing, I increment
count=count+1
endif
if count=lastset($overlap) then
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
if close>min($BOTy[y],$topy[i]) then //define the color
r=0
g=255
else
r=255
g=0
endif
//plot points at each tops
DRAWPOINT($topx[i],$topy[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($BOTx[y],$BOTy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
//plot the zone
drawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
next
else
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
if close>min($BOTy[y],$topy[i]) then //define the color
r=0
g=255
else
r=255
g=0
endif
//plot points at each tops
DRAWPOINT($topx[i],$topy[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($BOTx[y],$BOTy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
//plot the zone
drawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
endif
next
next
for i = 0 to lastset($BOTy) do //loop through the bottoms
for y = 0 to lastset($TOPy) do //check first bottom with other bottoms
change=abs(($TOPy[y]-$boty[i])/$boty[i]) //percent range between the 2 bottoms
if change<=percent/100 and barindex-$TOPx[y]<barlimit and $BOTx[i]<>$TOPx[y] then
if isset($overlap[0]) then
count=0
for x=0 to lastset($overlap) do
if abs(($BOTy[i]+$TOPy[y])/2 - $overlap[x])>percent*2 then
count=count+1
endif
next
if count=lastset($overlap) then
$overlap[lastset($overlap)+1]=($BOTy[i]+$TOPy[y])/2
if close<max($TOPy[y],$boty[i]) then //define the color
r=255
g=0
else
r=0
g=255
endif
DRAWPOINT($botx[i],$boty[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($TOPx[y],$TOPy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
drawrectangle(min($TOPx[y],$botx[i]),$TOPy[y],barindex,$boty[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
else
$overlap[lastset($overlap)+1]=($BOTy[i]+$TOPy[y])/2
if close<max($TOPy[y],$boty[i]) then //define the color
r=255
g=0
else
r=0
g=255
endif
DRAWPOINT($botx[i],$boty[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($TOPx[y],$TOPy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
drawrectangle(min($TOPx[y],$botx[i]),$TOPy[y],barindex,$boty[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
endif
next
next
endif
return
Your overlapping filter is a good idea! Did you try to just take a small part of the code and debug it? For instance, build a code with only support zones and check your overlap function. I know how hard it is, the first time to build PRT codes with arrays! 😉
I thought the error was around the count, so I inverted a bit the code as below. The problem is that I get an error of the type “infinite loop or too much iteration”
for x=0 to lastset($overlap) do // loop through the array of level
if abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])<percent*2 then // if the new level has enough space with those existing, I increment
count=count+1
endif
if count=0 then
what is weird is that if I cut my code by two, first I see the rectangles displayed on the chart during a few second, then the error message appears and the rectangle disappears with it…Maybe a bug remaining with this new array feature?
defparam calculateonlastbars=200
barlimit = 200
percent=0.15
if low[1]>low[4] and low[2]>low[4] and low[3]>low[4] and low[4]<low[5] and low[4]<low[6] and low[4]<low[7] then
$BOTy[lastset($BOTy)+1] = close[4] //store fractal value
$BOTx[lastset($BOTx)+1] = barindex[4] //stire fractal barindex
endif
if high[1]<high[4] and high[2]<high[4] and high[3]<high[4] and high[4]>high[5] and high[4]>high[6] and high[4]>high[7] then
$TOPy[lastset($TOPy)+1] = close[4] //store fractal value
$TOPx[lastset($TOPx)+1] = barindex[4] //store fractal 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
change=abs(($BOTy[y]-$topy[i])/$topy[i]) /
if change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then
if isset($overlap[0]) then
count=0
for x=0 to lastset($overlap) do
if abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])<percent*2 then
count=count+1
endif
if count=0 then
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
if close>min($BOTy[y],$topy[i]) then //define the color
r=0
g=255
else
r=255
g=0
endif
//plot points at each tops
DRAWPOINT($topx[i],$topy[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($BOTx[y],$BOTy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
//plot the zone
drawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
next
else
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
if close>min($BOTy[y],$topy[i]) then //define the color
r=0
g=255
else
r=255
g=0
endif
//plot points at each tops
DRAWPOINT($topx[i],$topy[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
DRAWPOINT($BOTx[y],$BOTy[y],2) COLOURED (g,r,0) BORDERCOLOR (g,r,0)
//plot the zone
drawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)
endif
endif
next
next
endif
test=lastset($overlap)
space=30
drawtext(" #test#",barindex,high+space,SansSerif,standard,10)
//test=count
//space=10
//drawtext(" #test#",barindex,high+space,SansSerif,standard,10)
//if isset($overlap[0]) then
//
//for x=0 to lastset($overlap) do
//test=round($overlap[x])
//space=x*10
//drawtext(" #test#",barindex,high+space,SansSerif,standard,10)
//next
//endif
return
The problem is that you continuously increase the index of the $overlap array in the for x/next loop, while this loop is based on the quantity of index of this specific array, so it results as an infinite loop.
You should build an array of possible support resistance zones in the first place. Then create a specific loop through this price zones array and exclude the overlapping ones in order to plot them.
Ok thx very much for your feedback. Actually, I had your solution in mind as backup but the downside is/was that you are losing some info in the process, as I’m storing only the midpoint levels.
Anyway please find here under a first draft of indicator drawing automatically what I call overlap levels (so levels that have been both support and resistance)…
DEFPARAM DRAWONLASTBARONLY =TRUE
defparam calculateonlastbars=200
barlimit = 200
//percent=0.1
if low[1]>low[4] and low[2]>low[4] and low[3]>low[4] and low[4]<low[5] and low[4]<low[6] and low[4]<low[7] then
$BOTy[lastset($BOTy)+1] = low[4] //store fractal value
$BOTx[lastset($BOTx)+1] = barindex[4] //stire fractal barindex
endif
if high[1]<high[4] and high[2]<high[4] and high[3]<high[4] and high[4]>high[5] and high[4]>high[6] and high[4]>high[7] then
$TOPy[lastset($TOPy)+1] = high[4] //store fractal value
$TOPx[lastset($TOPx)+1] = barindex[4] //store fractal 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
change=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between the 2 tops
if change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
endif
next
next
endif
if islastbarupdate and isset($overlap[0]) then
for x=0 to lastset($overlap) do
for z = 0 to lastset($overlap) do
if z=0 then
count=0
endif
if x<>z then
if abs($overlap[x]-$overlap[z])/$overlap[z]<percent/100*2 then
count=count+1
endif
endif
next
if count=0 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
else
for z = 0 to lastset($overlap) do
if abs($overlap[x]-$overlapfinal[z])/$overlapfinal[z]>percent/100*2 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
endif
next
endif
next
endif
if islastbarupdate and isset($overlapfinal[0]) then
for x=0 to lastset($overlapfinal) do
drawline(barindex-100,$overlapfinal[x],barindex,$overlapfinal[x]) coloured(100,100,50)
next
endif
//plot the zone
//test=lastset($overlap)
//test=$overlapfinal[4]
//drawtext(" #test#",barindex,high+20,SansSerif,standard,10)
return
being able to create such indicator is a huge step forward for PRT! if other participants can take this base to a higher level, this could be a cornerstone to create a profitable robot
Small improvement, drawing segment instead of line
DEFPARAM DRAWONLASTBARONLY =TRUE
defparam calculateonlastbars=200
barlimit = 200
//percent=0.1
if low[1]>=low[4] and low[2]>=low[4] and low[3]>=low[4] and low[4]<=low[5] and low[4]<=low[6] and low[4]<=low[7] then
$BOTy[lastset($BOTy)+1] = low[4] //store fractal value
$BOTx[lastset($BOTx)+1] = barindex[4] //stire fractal barindex
endif
if high[1]<=high[4] and high[2]<=high[4] and high[3]<=high[4] and high[4]>=high[5] and high[4]>=high[6] and high[4]>=high[7] then
$TOPy[lastset($TOPy)+1] = high[4] //store fractal value
$TOPx[lastset($TOPx)+1] = barindex[4] //store fractal 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
change=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between the 2 tops
if change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then
$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
$overlapx[lastset($overlapx)+1]=min($BOTx[y],$TOPx[i])
endif
next
next
endif
if islastbarupdate and isset($overlap[0]) then
for x=0 to lastset($overlap) do
for z = 0 to lastset($overlap) do
if z=0 then
count=0
endif
if x<>z then
if abs($overlap[x]-$overlap[z])/$overlap[z]<percent/100*2 then
count=count+1
endif
endif
next
if count=0 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
$overlapfinalx[lastset($overlapfinalx)+1]=$overlapx[x]
else
for z = 0 to lastset($overlap) do
if abs($overlap[x]-$overlapfinal[z])/$overlapfinal[z]>percent/100*2 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
$overlapfinalx[lastset($overlapfinalx)+1]=$overlapx[x]
endif
next
endif
next
endif
if islastbarupdate and isset($overlapfinal[0]) then
for x=0 to lastset($overlapfinal) do
drawsegment($overlapfinalx[x],$overlapfinal[x],barindex,$overlapfinal[x]) coloured(100,100,0)
next
endif
//plot the zone
//test=lastset($overlap)
//test=$overlapfinal[4]
//drawtext(" #test#",barindex,high+20,SansSerif,standard,10)
return
Thank you for this very nice fork of the original indicator. A great addition would be to start plotting the lines (as segments) from the bar when the level is found for the first time, sounds tricky but possible.
this is what the second version I posted is doing, nope?
Yes! You replied before me, thanks 🙂
I still found and resolved major bugs in my code, but unless there are other participants willing to build on this, don’t see the point to continue this thread
I strongly encourage to continue posting your valuable input. Once a while someone will pop into the discussion. It usually happens like this in the forums. Sadly, I can’t get involved into each interesting topic created by members, due to lack of time, going from problem to another.. anyway, I’m subscribing to the thread! thanks again for giving back to the community! 🙂
Yep you right but always difficult when you have the impression that’s more a one way virtual relationship…
So anyway, I’m posting the last version I have, which worked great yesterday on the WE static data, but now that prices move again, I get the famous error message “infinite loop or too much iteration”
the error is here when I use two loops to compare one value of an array with all other values of the same array, but I can not see why I would have an infinite loop…
for x=lastset($overlap) downto 1 do
for z=lastset($overlap) downto 1 do
if x=lastset($overlap) then
count=0
endif
if x<>z then
if abs($overlap[x]-$overlap[z])<ATR then
count=count+1
endif
endif
next
......
next
DEFPARAM DRAWONLASTBARONLY =TRUE
defparam calculateonlastbars=500
barlimit = 500
ATR=averagetruerange[20](close)
//percent=0.1
cp=2
ATRfactor=2
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[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2
$overlapx1[lastset($overlapx1)+1]=min($BOTx[y],$TOPx[i])
$overlapx2[lastset($overlapx2)+1]=max($BOTx[y],$TOPx[i])
endif
endif
next
next
endif
if islastbarupdate and isset($overlap[0]) then
for x=lastset($overlap) downto 1 do
for z=lastset($overlap) downto 1 do
if x=lastset($overlap) then
count=0
endif
if x<>z then
if abs($overlap[x]-$overlap[z])<ATR then
count=count+1
endif
endif
next
//test=count
//drawtext(" #test#",barindex,high+10*count,SansSerif,standard,10)
if count=0 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
$overlapfinalx1[lastset($overlapfinalx1)+1]=$overlapx1[x]
$overlapfinalx2[lastset($overlapfinalx2)+1]=$overlapx2[x]
else
if lastset($overlapfinal)=-1 then
$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]
$overlapfinalx1[lastset($overlapfinalx1)+1]=$overlapx1[x]
$overlapfinalx2[lastset($overlapfinalx2)+1]=$overlapx2[x]
endif
for zz = 0 to lastset($overlapfinal) 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[lastset($overlapfinal)+1]=$overlap[x]
$overlapfinalx1[lastset($overlapfinalx1)+1]=$overlapx1[x]
$overlapfinalx2[lastset($overlapfinalx2)+1]=$overlapx2[x]
endif
endif
next
endif
if islastbarupdate and isset($overlapfinal[0]) then
for x=0 to lastset($overlapfinal) 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
I have not tested yet, but 2 ways to improve the loop:
1/ is it needed to start the nested loop at the beginning of the array?
for x=lastset($overlap) downto 1 do
for z=x downto 1 do
2/ while increasing the index with lastset($overlapfinal)+1, is a good idea, it sometimes overload the array without reason.
try instead to create a variable that store the index number and re-use it at each run of the code:
at start of the code and before creating the array:
myindex = 0
then, while populating the array:
myindex = myindex+1 //increase the array size
$overlap[myindex] //use the index to store value
by doing this, it will considerably reduce the array size and therefore limiting the infinite loop issue!
Support and resistance zones with variables arrays
This topic contains 23 replies,
has 4 voices, and was last updated by Aragorna
2 years, 9 months ago.
| Forum: | ProBuilder support |
| Language: | English |
| Started: | 04/08/2020 |
| Status: | Active |
| Attachments: | 1 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.