Mxsortrows Outout

I have the following code (part) which distributes volume between high and low of the bar. The purpose of the code is to sort the bar price from lowest to highest then bar number wise. Output is also enclosed. First column is price second one is volume and the third one is bar number.

for(q=x;q<bcount;q++)
{
	////tisi =0.05
	Mx1=MxGetBlock(Base_Mattrix,Weeklymx[q][0],Weeklymx[q][1],0,15,False);
	
	fib=Weeklymx[q][0];
	
	lab=Weeklymx[q][1];
	
	bins=MxGetSize(Mx1,0);	
	
	Var1=0; cnt=0;
	
	for(j=0;j<bins;j++)
	{
		var1=var1+Mx1[j][8];
	}
	
	Tx1= Matrix(var1,5,0);	
	
	for(j=0;j<bins;j++)
	{
		for(n=0;n<Mx1[j][8];n++)///Mx1[j][8] is number of price bins in each bar
		{
		
			//if(AlmostEqual(Mx1[j][4],Mx1[j][3]+int(tisi*n)/100))
			if(Mx1[j][4]==Mx1[j][3]+int(tisi*n)/100)
			{
				Tx1[cnt][0]=Mx1[j][3]+int(tisi*n)/100;
				Tx1[cnt][1]=Mx1[j][9]+Mx1[j][10];/// Distributed Volume At close
				Tx1[cnt][2]=(Mx1[j][0]);//// Barnumber
			}
			else
			{
				Tx1[cnt][0]=Mx1[j][3]+int(tisi*n)/100;
				Tx1[cnt][1]=Mx1[j][9];/// Distributed Volume At Price bin
				Tx1[cnt][2]=(Mx1[j][0]);//// Barnumber
			}
			
			cnt=cnt+1;
		}	
	}
	
	
	Tx11=MxSortRows(Tx1,1,0,2);
	
	
	_TRACE("Sorted Matrix :" +MxToString(Tx11));
}

Asper above output sorting by bar number (ascending) is not carried out properly.
Where I have gone wrong. Anybody pleasee suggest. Thank You.

Did you check the function help of mxsortrows ?


MxSortRows( mx, ascending = True, col1 = 0, col2 = -1, col3 = -1 ) 

EXAMPLE m = MxFromString("[ 9, 1, 6; 40, 30, 20; 8, 7, 3; 3, 5, 1 ]"); 

printf("Input matrix "); 

printf( MxToString( m ) + " " ); 

printf("Rows %g, Cols %g ", MxGetSize( m, 0 ), MxGetSize( m, 1 ) ); 

printf("Sorting every row separately "); 
 m2 = MxSort( m, 0 ) ; 

printf( MxToString( m2 ) + " " ); 

printf("Sorting every column separately "); 
 m3 = MxSort( m, 1 ) ; 

printf( MxToString( m3 )+ " "); 

printf("Sorting rows by contents of first column "); 
 m4 = MxSortRows( m, True, 0 ) ; 

printf(MxToString( m4 )+ " "); 

printf("Sorting rows by contents of second column "); 
 m5 = MxSortRows( m, True, 1 ) ; 

printf(MxToString( m5 )+ " ");  

@mmha Thank you for your reply.
My idea is to sort the matrix first by price (from low to high) and then by bar number (from low to high). Please note the marked area of trace out put. Prices sorted as intended . But the higher bar came first instead of lower one. I could not figure out the error.

Because sorting is based only on one column ,the additional parameter for column are used when first column is identical

Chatgpt suggested two solution

// 1. Sort by bar first (secondary key)
mx1 = MxSortRows(mx, True, 1);

// 2. Then sort by price (primary key)
mx2 = MxSortRows(mx1, True, 0);

The other solution is based on creating new column of price *factor +bar

Say factor is greater than largest bar count of your db

Sorts the rows of the matrix in ascending/descending order of the col1 column. When the col1 column has equal values, SortRows sorts according to the col2 and col3 columns in succession (if col2 and col3 are specified and >= 0 ).Column numbers are zero based.

I add some explanation to understand better.

// The values at Mx[j][8] are pricebins
//// (H-L) High of the bar - Low Of the bar
//price bin = (h-L)/tisi (Tick size 0.01,0.05,0.1,0.25 etc)
//if H-L=0 price bin =1
///For example if (H-L)=2 and tick size =0.05 and low =100.05
//Calculation of bins from low(L) to High(H)
/////1) L=  100.05 first bin
/////2) L=L+0.05 (100.05+0.05) = 100.10 2 bin and so on up to high of the bar
//// total bins will be 2/0.05 =40+1 =41 per bar
////  Mx[j][4] is close price of the bar
////  Mx[j][3] is low price of the bar
////  Mx[j][0] is the barnumber

for(q=x;q<bcount;q++)
{
	////tisi =0.05
	Mx1=MxGetBlock(Base_Mattrix,Weeklymx[q][0],Weeklymx[q][1],0,15,False);
	
	fib=Weeklymx[q][0];
	
	lab=Weeklymx[q][1];
	
	bins=MxGetSize(Mx1,0);	
	
	Var1=0; cnt=0;
	
	for(j=0;j<bins;j++)
	{
		var1=var1+Mx1[j][8];
	}
	
	Tx1= Matrix(var1,5,0);	
	
	for(j=0;j<bins;j++)
	{
		for(n=0;n<Mx1[j][8];n++)///Mx1[j][8] is number of price bins in each bar
		{
		
			//if(AlmostEqual(Mx1[j][4],Mx1[j][3]+int(tisi*n)/100))
			if(Mx1[j][4]==Mx1[j][3]+int(tisi*n)/100)
			{
				Tx1[cnt][0]=Mx1[j][3]+int(tisi*n)/100;
				Tx1[cnt][1]=Mx1[j][9]+Mx1[j][10];/// Distributed Volume At close
				Tx1[cnt][2]=(Mx1[j][0]);//// Barnumber
			}
			else
			{
				Tx1[cnt][0]=Mx1[j][3]+int(tisi*n)/100;
				Tx1[cnt][1]=Mx1[j][9];/// Distributed Volume At Price bin
				Tx1[cnt][2]=(Mx1[j][0]);//// Barnumber
			}
			
			cnt=cnt+1;
		}	
	}
	
	
	Tx11=MxSortRows(Tx1,1,0,2);
	
	
	_TRACE("Sorted Matrix :" +MxToString(Tx11));
}

After populating matrix I tried to multi column sorting (ascending order and first by price wise then by bar number wise) . As I mentioned in my previous post higher bar number comes first inspite of mxsortrows function 2 value is ascending. I appreciate any help by the wise.

Admin Sir,

Please change the post title as "Mxsortrows output".

Friends,

The problem is solved by myself.

[quote="appasri, post:5, topic:40400"]
`Tx1[cnt][0]=Mx1[j][3]+int(tisi*n)/100;`///This changed to

Tx1[cnt][0]=int(Mx1[j][3]*100+tisi*n)/100;
[/quote]