Support and resistance zones with variables arrays
Forums › ProRealTime English forum › ProBuilder support › Support and resistance zones with variables arrays
- This topic has 23 replies, 4 voices, and was last updated 1 year ago by Aragorna.
Tagged: array, resistance, support, support and resistance
-
-
04/08/2020 at 4:54 PM #125286
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:
- the fractal to find the top and bottoms, but it’s a detail
- I’m mixing bottoms and tops to find overlap, instead of classic support and resistance
- to avoid having multiple zone mixing each other around the same price, I tried to add a third array, in order to store the levels found, and then displayed only those which have enough space space between them. However, this is the part bugging…
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
….
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120defparam calculateonlastbars=200barlimit = 200percent=0.1//the fractal code to find top/bottomsif 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 barindexendifif 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 barindexendifif(islastbarupdate and isset($topy[0]) and isset($boty[0])) thenfor i = 0 to lastset($TOPy) do //loop through the topsfor y = 0 to lastset($BOTy) do //check each bottomchange=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between a top and a bottomif change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] thenif isset($overlap[0]) then //if the array is not emptycount=0for x=0 to lastset($overlap) do // loop through the array of levelif abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])>percent*2 then // if the new level has enough space with those existing, I incrementcount=count+1endifif count=lastset($overlap) then$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2if close>min($BOTy[y],$topy[i]) then //define the colorr=0g=255elser=255g=0endif//plot points at each topsDRAWPOINT($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 zonedrawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)endifnextelse$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2if close>min($BOTy[y],$topy[i]) then //define the colorr=0g=255elser=255g=0endif//plot points at each topsDRAWPOINT($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 zonedrawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)endifendifnextnextfor i = 0 to lastset($BOTy) do //loop through the bottomsfor y = 0 to lastset($TOPy) do //check first bottom with other bottomschange=abs(($TOPy[y]-$boty[i])/$boty[i]) //percent range between the 2 bottomsif change<=percent/100 and barindex-$TOPx[y]<barlimit and $BOTx[i]<>$TOPx[y] thenif isset($overlap[0]) thencount=0for x=0 to lastset($overlap) doif abs(($BOTy[i]+$TOPy[y])/2 - $overlap[x])>percent*2 thencount=count+1endifnextif count=lastset($overlap) then$overlap[lastset($overlap)+1]=($BOTy[i]+$TOPy[y])/2if close<max($TOPy[y],$boty[i]) then //define the colorr=255g=0elser=0g=255endifDRAWPOINT($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)endifelse$overlap[lastset($overlap)+1]=($BOTy[i]+$TOPy[y])/2if close<max($TOPy[y],$boty[i]) then //define the colorr=255g=0elser=0g=255endifDRAWPOINT($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)endifendifnextnextendifreturn1 user thanked author for this post.
04/08/2020 at 5:07 PM #125295Your 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! 😉
04/08/2020 at 5:45 PM #125300I 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”
123456for x=0 to lastset($overlap) do // loop through the array of levelif abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])<percent*2 then // if the new level has enough space with those existing, I incrementcount=count+1endifif count=0 then04/08/2020 at 6:06 PM #125305what 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?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788defparam calculateonlastbars=200barlimit = 200percent=0.15if 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 barindexendifif 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 barindexendifif(islastbarupdate and isset($topy[0]) and isset($boty[0])) then//check points in a range of X percentfor i = 0 to lastset($TOPy) do //loop through the topsfor y = 0 to lastset($BOTy) dochange=abs(($BOTy[y]-$topy[i])/$topy[i]) /if change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] thenif isset($overlap[0]) thencount=0for x=0 to lastset($overlap) doif abs(($BOTy[y]+$TOPy[i])/2 - $overlap[x])<percent*2 thencount=count+1endifif count=0 then$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2if close>min($BOTy[y],$topy[i]) then //define the colorr=0g=255elser=255g=0endif//plot points at each topsDRAWPOINT($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 zonedrawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)endifnextelse$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2if close>min($BOTy[y],$topy[i]) then //define the colorr=0g=255elser=255g=0endif//plot points at each topsDRAWPOINT($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 zonedrawrectangle(min($BOTx[y],$topx[i]),$BOTy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)endifendifnextnextendiftest=lastset($overlap)space=30drawtext(" #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//endifreturn04/09/2020 at 10:28 AM #125389The 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.
04/10/2020 at 11:46 AM #125562Ok 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)…
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677DEFPARAM DRAWONLASTBARONLY =TRUEdefparam calculateonlastbars=200barlimit = 200//percent=0.1if 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 barindexendifif 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 barindexendifif(islastbarupdate and isset($topy[0]) and isset($boty[0])) then//check points in a range of X percentfor i = 0 to lastset($TOPy) do //loop through the topsfor y = 0 to lastset($BOTy) do //check first top with other topschange=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between the 2 topsif change<=percent/100 and barindex-$BOTx[y]<barlimit and $topx[i]<>$BOTx[y] then$overlap[lastset($overlap)+1]=($BOTy[y]+$TOPy[i])/2endifnextnextendifif islastbarupdate and isset($overlap[0]) thenfor x=0 to lastset($overlap) dofor z = 0 to lastset($overlap) doif z=0 thencount=0endifif x<>z thenif abs($overlap[x]-$overlap[z])/$overlap[z]<percent/100*2 thencount=count+1endifendifnextif count=0 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]elsefor z = 0 to lastset($overlap) doif abs($overlap[x]-$overlapfinal[z])/$overlapfinal[z]>percent/100*2 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]endifnextendifnextendifif islastbarupdate and isset($overlapfinal[0]) thenfor x=0 to lastset($overlapfinal) dodrawline(barindex-100,$overlapfinal[x],barindex,$overlapfinal[x]) coloured(100,100,50)nextendif//plot the zone//test=lastset($overlap)//test=$overlapfinal[4]//drawtext(" #test#",barindex,high+20,SansSerif,standard,10)return04/10/2020 at 11:55 AM #125564being 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
1 user thanked author for this post.
04/10/2020 at 12:05 PM #125566Small improvement, drawing segment instead of line
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980DEFPARAM DRAWONLASTBARONLY =TRUEdefparam calculateonlastbars=200barlimit = 200//percent=0.1if 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 barindexendifif 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 barindexendifif(islastbarupdate and isset($topy[0]) and isset($boty[0])) then//check points in a range of X percentfor i = 0 to lastset($TOPy) do //loop through the topsfor y = 0 to lastset($BOTy) do //check first top with other topschange=abs(($BOTy[y]-$topy[i])/$topy[i]) //percent range between the 2 topsif 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])endifnextnextendifif islastbarupdate and isset($overlap[0]) thenfor x=0 to lastset($overlap) dofor z = 0 to lastset($overlap) doif z=0 thencount=0endifif x<>z thenif abs($overlap[x]-$overlap[z])/$overlap[z]<percent/100*2 thencount=count+1endifendifnextif count=0 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]$overlapfinalx[lastset($overlapfinalx)+1]=$overlapx[x]elsefor z = 0 to lastset($overlap) doif abs($overlap[x]-$overlapfinal[z])/$overlapfinal[z]>percent/100*2 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]$overlapfinalx[lastset($overlapfinalx)+1]=$overlapx[x]endifnextendifnextendifif islastbarupdate and isset($overlapfinal[0]) thenfor x=0 to lastset($overlapfinal) dodrawsegment($overlapfinalx[x],$overlapfinal[x],barindex,$overlapfinal[x]) coloured(100,100,0)nextendif//plot the zone//test=lastset($overlap)//test=$overlapfinal[4]//drawtext(" #test#",barindex,high+20,SansSerif,standard,10)return04/10/2020 at 12:11 PM #12556804/10/2020 at 12:38 PM #12557204/10/2020 at 1:08 PM #12557604/11/2020 at 4:33 PM #125711I 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
1 user thanked author for this post.
04/12/2020 at 7:10 PM #125822I 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! 🙂
1 user thanked author for this post.
04/13/2020 at 11:42 AM #125901Yep 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…
123456789101112131415for x=lastset($overlap) downto 1 dofor z=lastset($overlap) downto 1 doif x=lastset($overlap) thencount=0endifif x<>z thenif abs($overlap[x]-$overlap[z])<ATR thencount=count+1endifendifnext......next123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121DEFPARAM DRAWONLASTBARONLY =TRUEdefparam calculateonlastbars=500barlimit = 500ATR=averagetruerange[20](close)//percent=0.1cp=2ATRfactor=2if 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] = barindexendifif 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] = barindexendifif(islastbarupdate and isset($topy[0]) and isset($boty[0])) then//check points in a range of X percentfor i = 0 to lastset($TOPy) do //loop through the topsfor y = 0 to lastset($BOTy) do //check first top with other topsif y<>i thenchange=abs($BOTy[y]-$topy[i])//percent range between the 2 topsif 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])endifendifnextnextendifif islastbarupdate and isset($overlap[0]) thenfor x=lastset($overlap) downto 1 dofor z=lastset($overlap) downto 1 doif x=lastset($overlap) thencount=0endifif x<>z thenif abs($overlap[x]-$overlap[z])<ATR thencount=count+1endifendifnext//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]elseif lastset($overlapfinal)=-1 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]$overlapfinalx1[lastset($overlapfinalx1)+1]=$overlapx1[x]$overlapfinalx2[lastset($overlapfinalx2)+1]=$overlapx2[x]endiffor zz = 0 to lastset($overlapfinal) doif zz=0 thencount=0endifif x<>zz thenif abs($overlap[x]-$overlapfinal[zz])<ATR thencount=count+1endifendifnextif count=0 then$overlapfinal[lastset($overlapfinal)+1]=$overlap[x]$overlapfinalx1[lastset($overlapfinalx1)+1]=$overlapx1[x]$overlapfinalx2[lastset($overlapfinalx2)+1]=$overlapx2[x]endifendifnextendifif islastbarupdate and isset($overlapfinal[0]) thenfor x=0 to lastset($overlapfinal) doif $overlapfinal[x] > close thenif highest[barindex-$overlapfinalx2[x]-1](high)<$overlapfinal[x]+ATR/ATRfactor thendrawsegment($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)endifelseif lowest[barindex-$overlapfinalx2[x]-1](low)>$overlapfinal[x]-ATR/ATRfactor thendrawsegment($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)endifendif//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)nextendif//plot the zonereturn04/13/2020 at 12:18 PM #125913I 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?
12for x=lastset($overlap) downto 1 dofor z=x downto 1 do2/ 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:
1myindex = 0then, while populating the array:
123myindex = myindex+1 //increase the array size$overlap[myindex] //use the index to store valueby doing this, it will considerably reduce the array size and therefore limiting the infinite loop issue!
-
AuthorPosts
Find exclusive trading pro-tools on