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.
defparam drawonlastbaronly = true
defparam calculateonlastbars=100
// fill up the array with CLOSE and Bar ID (label)
for k = 1 to 40
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
FOR k = 0 to 40
x = $da[k]
y = $la[k]
drawtext("close[#y#]: #x#",BarIndex+2, (y*2))
next
return
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:
defparam drawonlastbaronly = true
defparam calculateonlastbars=100
// fill up the array with CLOSE and Bar ID (label)
for k = 1 to 40
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!!