Output array with label AFTER sort?
Forums › ProRealTime English forum › ProBuilder support › Output array with label AFTER sort?
- This topic has 30 replies, 4 voices, and was last updated 2 months ago by robertogozzi.
Tagged: array, bubble, bubble sort, sort, sorting
-
-
08/10/2024 at 5:01 PM #236316
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.
08/11/2024 at 12:04 PM #236318HI 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
08/12/2024 at 10:47 AM #236336I could finally get the hang of it… the BUBBLE SORT code is incorrect. These are the correct lines:
1234567891011121314151617//////////////////////////////////////////////////////////////////// (Bubble Sort)FOR i = 0 TO MaxElements -1FOR j = 0 TO MaxElements - iIF $da[j] > $da[j + 1] THEN// swap datatemp = $da[j]$da[j] = $da[j + 1]$da[j + 1] = temp// swap labelstemp = $la[j]$la[j] = $la[j + 1]$la[j + 1] = tempENDIFNEXTNEXT//////////////////////////////////////////////////////////////////sorry for the inconvenience!
1 user thanked author for this post.
08/12/2024 at 2:05 PM #23634408/13/2024 at 2:47 PM #236368Hi 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?
08/13/2024 at 4:18 PM #236372Try this modified version, with the new bubble sort and a few more changes:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107// Data array $da ($dx is new array without duplicates)// Label Array $la ($lx is new array without duplicates)//defparam drawonlastbaronly = truedefparam calculateonlastbars=100// fill up the array with CLOSE and Bar ID (label)for k = 1 to 20averageline = average[k](close)if (close>averageline and open<averageline) or (close<averageline and open>averageline) thennewcount = 1elsenewcount=0endifonce $da[k]=0$da[k] = newcount+$da[k]$la[k] = knextMaxElements = lastset($da)//////////////////////////////////////////////////////////////////// plot unsorted arraystemp = MaxElements + 1Offset = average[10](range[1])drawtext("Elements #temp#",BarIndex+10,low - range)for k = 0 to MaxElementsx = $da[k]y = $la[k]drawtext("# of crossovers[#y#]: #x#",BarIndex+10,high + (Offset * k))next//////////////////////////////////////////////////////////////////// (Bubble Sort)FOR i = 0 TO MaxElements -1FOR j = 0 TO MaxElements - iIF $da[j] > $da[j + 1] THEN// swap datatemp = $da[j]$da[j] = $da[j + 1]$da[j + 1] = temp// swap labelstemp = $la[j]$la[j] = $la[j + 1]$la[j + 1] = tempENDIFNEXTNEXT//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 arraysfor k = 0 to MaxElementsx = $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 = 0FOR i = 0 TO MaxElementsIF ($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, tooif (i = MaxElements) thenbreakendifNewMaxElements = NewMaxElements + 1ENDIFNEXT//////////////////////////////////////////////////////////////////// plot new Arrays (without duplicates)temp = NewMaxElementsdrawtext("Elements #temp#",BarIndex+20,low - range)FOR k = 0 to NewMaxElementsx = $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))//nextreturn1 user thanked author for this post.
08/14/2024 at 12:26 PM #236394Hi 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.
123once $da[k]=0$da[k] = newcount+$da[k]Many thanks Rob for your perseverance and patience,
Finning
08/15/2024 at 11:19 AM #236412There are two issues:
- 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
- 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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// Data array $da ($dx is new array without duplicates)// Label Array $la ($lx is new array without duplicates)//defparam drawonlastbaronly = truedefparam calculateonlastbars=100// fill up the array with CLOSE and Bar ID (label)ONCE $TimeFlag[0] = 0IF $TimeFlag[0] <> OpenTime THEN$TimeFlag[0] = OpenTimefor k = 1 to 20averageline = average[k](close)if (close>averageline and open<averageline) or (close<averageline and open>averageline) thennewcount = 1elsenewcount=0endifonce $da[k]=0$da[k] = newcount+$da[k]$la[k] = knextMaxElements = lastset($da)//////////////////////////////////////////////////////////////////// plot unsorted arraystemp = MaxElements + 1Offset = average[10](range[1])drawtext("Elements #temp#",BarIndex+5,low - range)for k = 0 to MaxElementsx = $da[k]y = $la[k]drawtext("# of crossovers[#y#]: #x#",BarIndex+5,high + (Offset * k))next//////////////////////////////////////////////////////////////////// (Bubble Sort)FOR i = 0 TO MaxElements -1FOR j = 0 TO MaxElements - iIF $da[j] > $da[j + 1] THEN// swap datatemp = $da[j]$da[j] = $da[j + 1]$da[j + 1] = temp// swap labelstemp = $la[j]$la[j] = $la[j + 1]$la[j + 1] = tempENDIFNEXTNEXT//////////////////////////////////////////////////////////////////// plot Sorted arraysfor k = 0 to MaxElementsx = $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 = 0FOR i = 0 TO MaxElementsIF ($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, tooif (i = MaxElements) thenbreakendifNewMaxElements = NewMaxElements + 1ENDIFNEXTENDIF//////////////////////////////////////////////////////////////////// plot new Arrays (without duplicates)temp = NewMaxElementsdrawtext("Elements #temp#",BarIndex+15,low - range)FOR k = 0 to NewMaxElementsx = $dx[k]y = $lx[k]drawtext("# of crossovers[#y#]: #x#",BarIndex+15,high + (Offset * k))NEXTreturn1 user thanked author for this post.
08/20/2024 at 10:54 AM #236536Hi 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.
1 user thanked author for this post.
08/22/2024 at 10:31 AM #236603Hi 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
1For i = Maxelements downto 0but it only returned the largest singular value of the bubble sort.
Any ideas?
Thanks,
Finning
08/22/2024 at 10:57 AM #236606Add this snippet at the end, just before RETURN:
1234567891011LowestDA = 9999999LowestLA = 0FOR k = 0 to NewMaxElementsxx = $da[k]yy = $la[k]IF (xx < LowestDA) AND (xx > 0) THENLowestDA = xxLowestLA = yyENDIFNEXTdrawtext("# of crossovers[#LowestLA#]: #LowestDA#",BarIndex+25, high + (yy*2))08/22/2024 at 11:27 AM #236607Hi 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
08/23/2024 at 5:07 PM #236658Try these lines (I am attaching the ITF file, as well):
12345678910111213LowestDA = 9999999LowestLA = 0FOR k = 0 to NewMaxElementsxx = $datemp[k]yy = $latemp[k]IF (xx < LowestDA) AND (xx > 0) THENLowestDA = xxLowestLA = yyENDIFnextIF LowestLA <> 0 THENdrawtext("# of crossovers[#LowestLA#]: #LowestDA#",BarIndex+25, high + (yy*2))ENDIFit seems to work for me.
08/24/2024 at 12:05 PM #236673Hi 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.
09/02/2024 at 10:32 AM #237007 -
AuthorPosts
Find exclusive trading pro-tools on