Output array with label AFTER sort?

Forums ProRealTime English forum ProBuilder support Output array with label AFTER sort?

Viewing 15 posts - 1 through 15 (of 31 total)
  • #184936

    Hi

    I think I know the answer to this already but is it possible to associate a label with array entries so that they stay in the correct place after an arraysort?

    In the hypothetical example below, it works until I sort the array and then you would lose the label order obviously.

    Is there a way this could be achieved?

    Thanks

    Rob

    #184939

    You need to use two arrays, 1 for data and 1 for labels, then sort them using custom code (such as bubble sort), provided arrays arrays are not too large.

     

    1 user thanked author for this post.
    #185303

    Hi

    I have bubble sorted my array and realised I have duplicates to remove which I have kind of done by using a second array.

    However, I would like to use the same array but I end up with ‘n/a’ displaying (after drawtext) where the duplicates have been removed.

    Could someone please help? Been going round in circles!

    Thanks, Rob

     

    #185312

    It’s because I and J retain different values.

    It may happen that in line 7 $temp[i] has not yet been created,

     

    #185314

    Thanks Roberto.

    Is it possible to just use the same array, I would like to try example 2 of this pseudo code but have struggled to translate it in ProBuilder?

    https://www.codesdope.com/blog/article/remove-duplicate-elements-from-sorted-array/

    Thanks again.

     

     

    #185332

    EDITED
    This code is incorrect. The correct one is at
    https://www.prorealcode.com/topic/output-array-with-label-after-sort/page/2/#post-236336

    This is the Bubble Sort snippet:

    EDITED
    The code for the BUBBLE SORT is incorrect. The correct one is at
    https://www.prorealcode.com/topic/output-array-with-label-after-sort/page/2/#post-236336

    This an indicator (example) to be added ON the price chart:

    1 user thanked author for this post.
    #185338

    Perfect!

    Thank you.

    #185347

    Sorry, I just realized there’s an error in my pic and in the example (it’s an error concerning display only, the sorting code is correct). This is correct:

    the error was in DRAWTEXT. The second block prints the correct (sorted) data, but the wrong labels (as they shall NOT sorted, as they follow the sorted data).

     

    1 user thanked author for this post.
    #185367

    Thanks, is it possible to remove duplicates after the array/s have been sorted?

    I’m guessing the non duplicates may need to go into a new array or can we reset the original array and then copy the non dupliate values in? This is the bit I’m struggling with. I thought I was there earlier and then tried it on some different values and my code clearly doesn’t work.

    Thanks again. Rob

    #185443

    This version removes duplicates (I created two new, separate, arrays):

    2 users thanked author for this post.
    #185461

    Thanks so much Roberto!

    I’ve just wasted the morning doing it old school without arrays and then I see this!

    Hopefully it will get added to the code snippet spreadsheet so others can benefit.

    Really appreciate this a lot.

    Thank you.

     

     

    2 users thanked author for this post.
    #185530

    Link to code above added as Log 313 here …

    Snippet Link Library

    1 user thanked author for this post.
    #236294

    Hi @robertogozzi

    I’m having some troubles with the above bubble sort code.

    I’m trying to use an array for $da[k].

    So, as per the code below, I’m using an array to sum up ($da[k]) the number of crosses at close of moving average k back to calculateonlastbars, with the moving average period k also being stored for identification in $la[k].

    What seems to be happening, is that if I delete all of the bubble sort and remove duplicates code, $da[k] seems to fill with the correct data, and work as it should. The code I’m using for that is below, to show that $da[k] is filling correctly, and giving an answer of 2 or so, which is what you would expect.

     

    For this, have a look at the attached pic, MA crossover array dump no bubble sort

    The code with the bubble sort and duplicate removal that I am using is below:

    averageline = average[k](close)

    if (close>averageline and open<averageline) or (closeaverageline) 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-20,low – range)
    for k = 0 to MaxElements
    x = $da[k]
    y = $la[k]
    drawtext(“close[#y#]: #x#”,BarIndex-20,high + (Offset * k))
    next
    //////////////////////////////////////////////////////////////////
    // (Bubble Sort)
    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(“close[#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
    NewMaxElements = NewMaxElements + 1
    ENDIF
    NEXT
    //////////////////////////////////////////////////////////////////
    // plot new Arrays (without duplicates)
    temp = NewMaxElements
    drawtext(“Elements #temp#”,BarIndex,low – range)
    FOR k = 0 to NewMaxElements – 1
    x = $dx[k]
    y = $lx[k]
    drawtext(“close[#y#]: #x#”,BarIndex,high + (Offset * k))
    NEXT

    return

     

    For the results of this, have a look at the attached pic, MA crossover with bubble sort code applied

    It seems to be that the bubble sort/duplicate removal is causing an error in the initial correct filling of $da[k], and from this the wrong answer is then given.

    Is there any way that this could be made to work as intended, so that the bubble sort will actually give the correct period with the maximum number of cross overs of the average?

    Many thanks,

    Finning

    PS – try as I might, the insert code button doesn’t seem to be working!!

    #236304

    As you can see from the attached pic, all seems to be plotted correctly.

     

    1 user thanked author for this post.
    #236309

    Hi Roberto,

    thanks for having a look.

    Yes, I agree, your code is working properly, and is plotting correctly.

    However, that is not the problem that I’m having.

    The problem is, is that the bubble sort and/or the duplication removal code, is changing the result of what I am trying the $da[k] array to store in the first place.

    The bubble sort is changing what is making its way into the $da[k] array, and this is the problem that I need help with.

    I have attached 2 picture output examples, using my code posted above (sorry I couldn’t get the above code to display properly despite trying) to show this, and I slightly changed where the plot offsets were on the screen to make it all fit, and changed the loop size to 20, to make it all fit.

    One pic is of the $da[k] with the bubble sort/duplication removal code present, and the other pic is $da[k] the bubble sort/duplication removal code NOT present (deleted).

    This shows the problem that I am having with the bubble sort/duplication removal code affecting what is stored in $da[k]. This is the problem I need help with please.

    In the pic above that you sent me, the bubble sort/duplication deletion was working fine, yes, the only problem is the data that it was sorting stored in $da[k] was wrong.

    I hope I have made the problem I am having a bit more clear, and I sure do hope that you could help!

    Many thanks,

    Finning

     

Viewing 15 posts - 1 through 15 (of 31 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login