Support and resistance zones with variables arrays

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #125286 quote
    stefou102
    Participant
    Veteran

    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

    ….

    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
    
    Guibourse thanked this post
    #125295 quote
    Nicolas
    Keymaster
    Master

    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! 😉

    #125300 quote
    stefou102
    Participant
    Veteran

    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
    #125305 quote
    stefou102
    Participant
    Veteran

    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
    
    #125389 quote
    Nicolas
    Keymaster
    Master

    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.

     

    #125562 quote
    stefou102
    Participant
    Veteran

    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
    
    #125564 quote
    stefou102
    Participant
    Veteran

    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

    Oliviertrader2020 thanked this post
    #125566 quote
    stefou102
    Participant
    Veteran

    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
    
    #125568 quote
    Nicolas
    Keymaster
    Master

    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.

    #125572 quote
    stefou102
    Participant
    Veteran

    this is what the second version I posted is doing, nope?

    #125576 quote
    Nicolas
    Keymaster
    Master

    Yes! You replied before me, thanks 🙂

    #125711 quote
    stefou102
    Participant
    Veteran

    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

    alessiopippo thanked this post
    #125822 quote
    Nicolas
    Keymaster
    Master

    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! 🙂

    alessiopippo thanked this post
    #125901 quote
    stefou102
    Participant
    Veteran

    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
    
    #125913 quote
    Nicolas
    Keymaster
    Master

    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!

Viewing 15 posts - 1 through 15 (of 24 total)
  • You must be logged in to reply to this topic.

Support and resistance zones with variables arrays


ProBuilder support

New Reply
Author
author-avatar
stefou102 @stefou102 Participant
Summary

This topic contains 23 replies,
has 4 voices, and was last updated by Aragorna
2 years, 9 months ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 04/08/2020
Status: Active
Attachments: 1 files
Logo Logo
Loading...