Output array with label AFTER sort?
Forums › ProRealTime English forum › ProBuilder support › Output array with label AFTER sort?
- This topic has 11 replies, 3 voices, and was last updated 1 year ago by
GraHal.
Tagged: array, bubble, bubble sort, sort, sorting
-
-
01/09/2022 at 12:59 PM #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.
123456789101112131415161718192021222324252627282930313233343536373839404142defparam drawonlastbaronly = trueema5 = exponentialaverage[5](close)ema10 = exponentialaverage[10](close)ema21 = exponentialaverage[21](close)sma50 = average[50](close)sma100 = average[100](close)sma200 = average[200](close)// create ma array$ma[1] = ema5$ma[2] = ema10$ma[3] = ema21$ma[4] = sma50$ma[5] = sma100$ma[6] = sma200// output ma arrayfor j = 1 to lastset($ma)mymas = $ma[j]drawtext("#mymas#",barindex+50,j*0.1) coloured("blue")next// create ma label array$malabel[1] = 5$malabel[2] = 10$malabel[3] = 21$malabel[4] = 50$malabel[5] = 100$malabel[6] = 200// output may label arrayfor j = 1 to lastset($malabel)mymalabel = $malabel[j]drawtext("ma #mymalabel#",barindex,j*0.1) coloured("blue")nextreturnIs there a way this could be achieved?
Thanks
Rob
01/09/2022 at 1:36 PM #184939You 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.
01/12/2022 at 8:04 PM #185303Hi
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
12345678j = 0for i = 0 to lastset($ma)if $ma[i] <> $ma[i+1] then$temp[j] = $ma[i]j = j + 1endif$ma[i] = $temp[i]next01/12/2022 at 11:52 PM #185312It’s because I and J retain different values.
It may happen that in line 7 $temp[i] has not yet been created,
01/13/2022 at 8:02 AM #185314Thanks 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.
01/13/2022 at 12:15 PM #185332This is the Bubble Sort snippet:
123456789101112131415161718// Data array $da// Label Array $la//MaxElement = lastset($da)FOR i = 0 TO MaxElementFOR j = 0 TO MaxElementIF $da[j] > $da[i] THEN// swap datatemp = $da[j]$da[j] = $da[i]$da[i] = temp// swap labelstemp = $la[j]$la[j] = $la[i]$la[i] = tempENDIFNEXTNEXTThis an indicator (example) to be added ON the price chart:
1234567891011121314151617181920212223242526272829303132333435363738// Data array $da// Label Array $la//defparam drawonlastbaronly = true// fill up both arraysfor k = 0 to 7$da[k] = close[k] //data (CLOSE from the current one backwards to the previous 7 bars)$la[k] = k //labels (index reference)nextMaxElement = lastset($da)temp = MaxElement+1drawtext("Elements #temp#",BarIndex,low - range)for k = 0 to MaxElementx = $da[k]drawtext("close[#k#]: #x#",BarIndex-10,high + (range * k))next// Bubble SortFOR i = 0 TO MaxElementFOR j = 0 TO MaxElementIF $da[j] > $da[i] THEN// swap datatemp = $da[j]$da[j] = $da[i]$da[i] = temp// swap labelstemp = $la[j]$la[j] = $la[i]$la[i] = tempENDIFNEXTNEXTtemp = MaxElement+1drawtext("Elements #temp#",BarIndex,low - range)for k = 0 to MaxElementx = $da[k]drawtext("close[#k#]: #x#",BarIndex,high + (range * k))nextreturn1 user thanked author for this post.
01/13/2022 at 12:49 PM #18533801/13/2022 at 2:39 PM #185347Sorry, 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:
123456789101112131415161718192021222324252627282930313233343536373839// Data array $da// Label Array $la//defparam drawonlastbaronly = truefor k = 0 to 7$da[k] = close[k]$la[k] = knextMaxElements = lastset($da)temp = MaxElements+1drawtext("Elements #temp#",BarIndex,low - range)for k = 0 to MaxElementsx = $da[k]y = $la[k]drawtext("close[#y#]: #x#",BarIndex-10,high + (range * k))next//sortingFOR i = 0 TO MaxElementsFOR j = 0 TO MaxElementsIF $da[j] > $da[i] THEN// swap datatemp = $da[j]$da[j] = $da[i]$da[i] = temp// swap labelstemp = $la[j]$la[j] = $la[i]$la[i] = tempENDIFNEXTNEXTtemp = MaxElements+1drawtext("Elements #temp#",BarIndex,low - range)for k = 0 to MaxElementsx = $da[k]y = $la[k]drawtext("close[#y#]: #x#",BarIndex,high + (range * k))nextreturnthe 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.
01/13/2022 at 8:33 PM #185367Thanks, 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
01/14/2022 at 1:04 PM #185443This version removes duplicates (I created two new, separate, arrays):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263// Data array $da ($dx is new array without duplicates)// Label Array $la ($lx is new array without duplicates)//defparam drawonlastbaronly = true// fill up the array with CLOSE and Bar ID (label)for k = 0 to 7$da[k] = round((close[k] - 0.5))$la[k] = knextMaxElements = lastset($da)//////////////////////////////////////////////////////////////////// plot unsorted arraystemp = MaxElements + 1Offset = average[10](range[1])drawtext("Elements #temp#",BarIndex-20,low - range)for k = 0 to MaxElementsx = $da[k]y = $la[k]drawtext("close[#y#]: #x#",BarIndex-20,high + (Offset * k))next//////////////////////////////////////////////////////////////////// (Bubble Sort)FOR i = 0 TO MaxElementsFOR j = 0 TO MaxElementsIF $da[j] > $da[i] THEN// swap datatemp = $da[j]$da[j] = $da[i]$da[i] = temp// swap labelstemp = $la[j]$la[j] = $la[i]$la[i] = tempENDIFNEXTNEXT//////////////////////////////////////////////////////////////////// plot Sorted arraysfor k = 0 to MaxElementsx = $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 = 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, tooNewMaxElements = NewMaxElements + 1ENDIFNEXT//////////////////////////////////////////////////////////////////// plot new Arrays (without duplicates)temp = NewMaxElementsdrawtext("Elements #temp#",BarIndex,low - range)FOR k = 0 to NewMaxElements - 1x = $dx[k]y = $lx[k]drawtext("close[#y#]: #x#",BarIndex,high + (Offset * k))NEXTreturn1 user thanked author for this post.
01/14/2022 at 3:46 PM #185461Thanks 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.
1 user thanked author for this post.
01/15/2022 at 11:19 AM #185530Link to code above added as Log 313 here …
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on