Output array with label AFTER sort?

Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #236316 quote
    robertogozzi
    Moderator
    Master

    I can’t see anything wrong. I am afraid I completed your code incorrrctly as Copy & Paste could not work.

    Please attach your code as a complete text files or, which is preferred, the ITF you used.

    #236318 quote
    Finning
    Participant
    Veteran

    HI Roberto,

    please find attached. One itf with the bubble sort, and one without the bubble sort. These are the exact files that I used to make the previous pics showing the difference in $da[k].

    The exact same code is present in both to fill up $da[k].

    I did my testing on Brent Crude Oil Full1024 with daily bars, and I am using a PRT demo account.

    I had a bit of a play around, and it doesn’t seem to be the duplicate removal portion of the code from what I can tell that’s causing the problem, so it must have something to do with the bubble sort process?

    Cheers,

    Finning

    #236336 quote
    robertogozzi
    Moderator
    Master

    I could finally get the hang of it… the BUBBLE SORT code is incorrect. These are the correct lines:

    //////////////////////////////////////////////////////////////////
    // (Bubble Sort)
    FOR i = 0 TO MaxElements -1
       FOR j = 0 TO MaxElements - i
          IF $da[j] > $da[j + 1] THEN
             // swap data
             temp   = $da[j]
             $da[j] = $da[j + 1]
             $da[j + 1] = temp
             // swap labels
             temp   = $la[j]
             $la[j] = $la[j + 1]
             $la[j + 1] = temp
          ENDIF
       NEXT
    NEXT
    //////////////////////////////////////////////////////////////////

    sorry for the inconvenience!

    Finning thanked this post
    #236344 quote
    Finning
    Participant
    Veteran

    Hi Roberto,

    thank you for that!! Just travelling at the moment, will have a look at it as soon as I can.

    Cheers,

    Finning

    #236368 quote
    Finning
    Participant
    Veteran

    Hi Roberto,

    I copied that code into the itf that I sent you with the bubble sort present and tired it, and it doesn’t look like it has worked? The same issue is still there.

    Maybe there isn’t a problem with the bubble sort?

    Maybe the summation of the array $da[k] isn’t working because of the loops in the bubble sort are throwing it out?

    #236372 quote
    robertogozzi
    Moderator
    Master

    Try this modified version, with the new bubble sort and a few more changes:

    // Data array  $da     ($dx is new array without duplicates)
    // Label Array $la     ($lx is new array without duplicates)
    //
    defparam drawonlastbaronly = true
    defparam calculateonlastbars=100
    // fill up the array with CLOSE and Bar ID (label)
    for k = 1 to 20
    
    averageline = average[k](close)
    
    if (close>averageline and open<averageline) or (close<averageline and open>averageline)  then
    
    newcount = 1
    else
    newcount=0
    
    endif
    
    once $da[k]=0
    
    $da[k] = newcount+$da[k]
    
    $la[k] = k
    
    next
    
    
    MaxElements = lastset($da)
    //////////////////////////////////////////////////////////////////
    // plot unsorted arrays
    temp = MaxElements + 1
    Offset = average[10](range[1])
    drawtext("Elements #temp#",BarIndex+10,low - range)
    for k = 0 to MaxElements
    x = $da[k]
    y = $la[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+10,high + (Offset * k))
    next
    //////////////////////////////////////////////////////////////////
    // (Bubble Sort)
    
    FOR i = 0 TO MaxElements -1
    FOR j = 0 TO MaxElements - i
    IF $da[j] > $da[j + 1] THEN
    // swap data
    temp   = $da[j]
    $da[j] = $da[j + 1]
    $da[j + 1] = temp
    // swap labels
    temp   = $la[j]
    $la[j] = $la[j + 1]
    $la[j + 1] = temp
    ENDIF
    NEXT
    NEXT
    
    //FOR i = 0 TO MaxElements
    //FOR j = 0 TO MaxElements
    //IF $da[j] > $da[i] THEN
    //// swap data
    //temp   = $da[j]
    //$da[j] = $da[i]
    //$da[i] = temp
    //// swap labels
    //temp   = $la[j]
    //$la[j] = $la[i]
    //$la[i] = temp
    //ENDIF
    //NEXT
    //NEXT
    //////////////////////////////////////////////////////////////////
    // plot Sorted arrays
    for k = 0 to MaxElements
    x = $da[k]
    y = $la[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+15,high + (Offset * k))
    next
    //////////////////////////////////////////////////////////////////
    // remove duplicates by comparing the current element to the next one (creating 2 new arrays)
    NewMaxElements = 0
    FOR i = 0 TO MaxElements
    IF ($da[i] <> $da[i + 1]) OR (i = MaxElements) THEN
    $dx[NewMaxElements] = $da[i]          //save datum to new array, when different
    $lx[NewMaxElements] = $la[i]          //save label, too
    if (i = MaxElements) then
    break
    endif
    NewMaxElements      = NewMaxElements + 1
    ENDIF
    NEXT
    //////////////////////////////////////////////////////////////////
    // plot new Arrays (without duplicates)
    temp = NewMaxElements
    drawtext("Elements #temp#",BarIndex+20,low - range)
    FOR k = 0 to NewMaxElements
    x = $dx[k]
    y = $lx[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+20,high + (Offset * k))
    NEXT
    
    //FOR k = 0 to 20
    //xx = $da[k]
    //yy = $la[k]
    //drawtext("# of crossovers[#yy#]:  #xx#",BarIndex+5, 25 + (yy*2))
    //next
    
    return
    Finning thanked this post
    #236394 quote
    Finning
    Participant
    Veteran

    Hi Rob,

    I imported your ITF file, tried it, and the same problem is still there. The bubble sort is still having the effect of corrupting the array fill.

    The duplicate removal feature still works if you remove the bubble sort component, and it will remove duplicate values out of $da[k], even if it’s not bubble sorted.

    I’ve attached 2 pics so you can see what I’m seeing of your code. With your original unmodified code, and, with the bubble sort deleted (the duplication removal is still present).

    The bubble sort code is interfering with storage of the correct values in $da[k]. That’s what is happening, or that is at least what looks to be happening.

    What must be happening is that the bubble sort is scrambling the values – sorting them – and when $da[k] is looping in its summation process, it is writing it’s values into the wrong index, so then summing up incorrectly. The bubble sort sorts it every bar, and then the array summation of $da[k] puts it in the wrong index again. This might then explain why the results with the bubble sort are semi-homogenous and slightly trending up too.

    It’s sort of like you need to copy $da[k] (and $la[k]) into a new disposable array(s) – each bar – the bubble sort sorts those temp arrays – and that is your answer. And when I mean temporary disposable array(s), I mean before the bubble sort process, to be clear, because the bubble sort process itself uses temporary arrays, not to be mistaken with them.

    The first part of the code can keep on producing $da[k], and the bubble sort never “sees” it, it only works on copied disposable arrays that would need to be created each bar.

    I don’t think it’s a problem necessarily with the bubble sort code itself, but how the bubble sort is interacting with the filling of $da[k] when an array summation as per below is used in its creation.

     

    once $da[k]=0
     
    $da[k] = newcount+$da[k]

     

    Many thanks Rob for your perseverance and patience,

    Finning

    #236412 quote
    robertogozzi
    Moderator
    Master

    There are two issues:

    1. arrays are not historicized, so $var[1] is element 1 of the array $var, not the previous value of $var; this implies, unlike variables, that elements are updated each tick and, if conditions are not met at the closing of a bar, their original value cannot be restored because it’s lost
    2. the same conditions is added multiple times while a bar is formed, thus retaining incorrect values.

    I have added a new 1-element array to be used as a flag to detect any change in the opening time of a bar, so we can tell when a new bar has formed to update the array (lines 7, 8 and 9). The IF in line 8 is closesd in line 69.

    Give it a try.

    // Data array  $da     ($dx is new array without duplicates)
    // Label Array $la     ($lx is new array without duplicates)
    //
    defparam drawonlastbaronly = true
    defparam calculateonlastbars=100
    // fill up the array with CLOSE and Bar ID (label)
    ONCE $TimeFlag[0] = 0
    IF $TimeFlag[0] <> OpenTime THEN
    $TimeFlag[0] = OpenTime
    for k = 1 to 20
    averageline = average[k](close)
    if (close>averageline and open<averageline) or (close<averageline and open>averageline)  then
    newcount = 1
    else
    newcount=0
    endif
    once $da[k]=0
    $da[k] = newcount+$da[k]
    $la[k] = k
    next
    MaxElements = lastset($da)
    //////////////////////////////////////////////////////////////////
    // plot unsorted arrays
    temp = MaxElements + 1
    Offset = average[10](range[1])
    drawtext("Elements #temp#",BarIndex+5,low - range)
    for k = 0 to MaxElements
    x = $da[k]
    y = $la[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+5,high + (Offset * k))
    next
    //////////////////////////////////////////////////////////////////
    // (Bubble Sort)
    
    FOR i = 0 TO MaxElements -1
    FOR j = 0 TO MaxElements - i
    IF $da[j] > $da[j + 1] THEN
    // swap data
    temp   = $da[j]
    $da[j] = $da[j + 1]
    $da[j + 1] = temp
    // swap labels
    temp   = $la[j]
    $la[j] = $la[j + 1]
    $la[j + 1] = temp
    ENDIF
    NEXT
    NEXT
    //////////////////////////////////////////////////////////////////
    // plot Sorted arrays
    for k = 0 to MaxElements
    x = $da[k]
    y = $la[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+10,high + (Offset * k))
    next
    //////////////////////////////////////////////////////////////////
    // remove duplicates by comparing the current element to the next one (creating 2 new arrays)
    NewMaxElements = 0
    FOR i = 0 TO MaxElements
    IF ($da[i] <> $da[i + 1]) OR (i = MaxElements) THEN
    $dx[NewMaxElements] = $da[i]          //save datum to new array, when different
    $lx[NewMaxElements] = $la[i]          //save label, too
    if (i = MaxElements) then
    break
    endif
    NewMaxElements      = NewMaxElements + 1
    ENDIF
    NEXT
    ENDIF
    //////////////////////////////////////////////////////////////////
    // plot new Arrays (without duplicates)
    temp = NewMaxElements
    drawtext("Elements #temp#",BarIndex+15,low - range)
    FOR k = 0 to NewMaxElements
    x = $dx[k]
    y = $lx[k]
    drawtext("# of crossovers[#y#]:  #x#",BarIndex+15,high + (Offset * k))
    NEXT
    return
    Finning thanked this post
    #236536 quote
    Finning
    Participant
    Veteran

    Hi Rob,

    sorry have been on the road again the last few days.

    First of all, yes I am aware that arrays are not historicized – though the array values are kept from bar to bar, unless something is done to change them.

    As to your 2nd point Rob, I tried your code above, with the timeflag, and when I got rid of drawonlastbaronly = true, it showed that the timeflag = 0 for all bars. It seemed to be that there was no problem with a time change in the opening of the bar, when this timeflag array was plotted against just the straight opentime for that bar, of which all results for all bars said 0.

    I’ve continued my line of enquiry with the theory I had above, how the looping of the bubble sort was damaging the initial $da[k], and how if a temporary $da[k] was used, this might negate that problem.

    In short, it looks like the idea of a temporary array has worked to solve this problem. I’ve attached an ITF of how I got it to work. The other thing with it too, I unset the array at the start of every bar, so the temp array gives an up to date version of the cross count values in the $da[k] array.

    Many thanks for the ideas Rob. Great to have someone to talk to about this stuff, it’s a bit hard on your own.

    Cheers,

    Finning.

    robertogozzi thanked this post
    #236603 quote
    Finning
    Participant
    Veteran

    Hi Rob,

    with the duplicate removal tool, is there any way that it can be made to store the smallest of the duplicate values?

    For example, in the data above, moving average periods 15, 16, and 20, all have 11 crossovers.

    The current duplicate removal code keeps the 20 period.

    Is there any way that it could keep the 15 period value instead? So 15 would show up from the duplicate removal, and not 20.

    I tried

    For i = Maxelements downto 0

    but it only returned the largest singular value of the bubble sort.

    Any ideas?

    Thanks,

    Finning

    #236606 quote
    robertogozzi
    Moderator
    Master

    Add this snippet at the end, just before RETURN:

    LowestDA = 9999999
    LowestLA = 0
    FOR k = 0 to NewMaxElements
       xx = $da[k]
       yy = $la[k]
       IF (xx < LowestDA) AND (xx > 0) THEN
          LowestDA = xx
          LowestLA = yy
       ENDIF
    NEXT
    drawtext("# of crossovers[#LowestLA#]:  #LowestDA#",BarIndex+25, high + (yy*2))
    
    #236607 quote
    Finning
    Participant
    Veteran

    Hi Roberto,

    no that didn’t seem to work.

    Another way of saying the above, what I was looking for, is to keep the lower duplicate value (based on the $la[k] array) from the duplicate removal process.

    The duplicate removal process currently keeps the higher $la[k] array and corresponding $da[k].

    Have attached another pic to show this, crossing out the higher values, and keeping the lower values.

    Thanks,

    Finning

    #236658 quote
    robertogozzi
    Moderator
    Master

    Try these lines (I am attaching the ITF file, as well):

    LowestDA = 9999999
    LowestLA = 0
    FOR k = 0 to NewMaxElements
       xx = $datemp[k]
       yy = $latemp[k]
       IF (xx < LowestDA) AND (xx > 0) THEN
          LowestDA = xx
          LowestLA = yy
       ENDIF
    next
    IF LowestLA <> 0 THEN
       drawtext("# of crossovers[#LowestLA#]:  #LowestDA#",BarIndex+25, high + (yy*2))
    ENDIF

    it seems to work for me.

    #236673 quote
    Finning
    Participant
    Veteran

    Hi Rob,

    I tried your code, and it didn’t give the intended result. Please see attached pic of me running this code.

    Rob, I’m sorry if I haven’t explained myself properly.

    With the bubble sort result, if you have a look at the numbers in the brackets [x], these are the periods that the moving average crossovers were recorded in.

    The number to the right of them, is the count of the number of moving average crossovers.

    What is happening in the duplicate removal, is that it is keeping the result with the highest number in the brackets [x].

    The problem that I am trying to solve is I need the duplicate removal code to give the smallest [x] value for duplicates.

    So, instead of the duplicate removal keeping the highest duplicates, I need it to keep the lowest duplicates.

    If I run both of these duplicate removal processes together, I will have a highest and a lowest value (I am only interested in the biggest values of the bubble sort too), and I can then take a midpoint of these two numbers for use as a variable.

    Please find the attached pic to explain this.

    Thanks,

    Finning.

    #237007 quote
    Finning
    Participant
    Veteran

    Hi Rob,

    did you have any further luck in looking at this?

    Thanks..

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

Output array with label AFTER sort?


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
robdav @robdav Participant
Summary

This topic contains 30 replies,
has 4 voices, and was last updated by robertogozzi
1 year, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 01/09/2022
Status: Active
Attachments: 22 files
Logo Logo
Loading...