Mxsortrows function

Hello friends,

I have created function to club volumes of all equal close values in day. The total data points per day per symbol approximately 22500.The code is below.

function TotalVolumeAtEqualClose(Fib,Lab)
{
	for(k=fib;k<=lab;k++)
	{
	
	fb=fib;lb=k;
	local dsmx1,mxx;
	
	dsmx=Matrix((lb-fb)+1,3,Null);// Main Matrix
	d=0;
	for(i=fb;i<=lb;i++)//Day matrix
	{
		dsmx[d][0]=C[i];
		dsmx[d][1]=V[i];
		dsmx[d][2]=i;//barindex
		d=d+1;
	}
	
	dsmx=MxSortRows(dsmx,True,0);// Sorted matrix to find number of records after finding equal close values
	bins=MxGetSize(dsmx,0);
	d=0;maxivol=0;localvol=0;localcl=0;
			for(j=0;j<bins;,j++)//finding total data points after equal close entries
			{
			if(j==0){d=d+1;}
			if(j>0 && dsmx[j][0]!=dsmx[j-1][0]){d=d+1;}
			}
	
	newmx=Matrix(d+1,3,Null);//Experiment Three columns close,volume and bar number
	d=0;
	
	dsmx1=dsmx;
	d=0;
	
			for(j=0;j<bins;,j++)//building new matrix
			{
			
			if(j==0)//first bar of the sorted Day matrix
			{
			localcl=dsmx1[j][0];
			localvol=dsmx1[j][1];
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j][2];
			d=d+1;
			}
			
			if(j>0 && j!=bins-1 && dsmx1[j][0]==dsmx1[j-1][0])
			{
			localvol=localvol+dsmx1[j][1];
			localcl=dsmx1[j][0];
			}
			if(j>0 && j!=bins-1 && dsmx1[j][0]!=dsmx1[j-1][0])
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j-1][2];
			d=d+1;
			localcl=dsmx1[j][0];
			localvol=dsmx1[j][1];
			}
			
			if(j>0 && j==bins-1 && dsmx1[j][0]!=dsmx1[j-1][0])
			{
			if(j==1)
			{
			newmx[d][0]=dsmx1[j][0];//localcl;
			newmx[d][1]=dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			else
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j-1][2];
			d=d+1;
			newmx[d][0]=dsmx1[j][0];
			newmx[d][1]=dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			}
			if(j>0 && j==bins-1 && dsmx1[j][0]==dsmx1[j-1][0])
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol+dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			}
			mxx= MxSortRows(newmx,True,1);
			bins=MxGetSize(mxx,0);
	
		//mxx=EqualClose(Fb,k);
		//bins=MxGetSize(mxx,0);
	
	
	
		
	
	}
}

Codecheck

Here I am enclosing code check profile also. Please suggest a better code.

regds
DSARAO

You don't need looping.
Use MxSetBlock and MxGetBlock and convert to 1-dim array after sorting.
Then you can use array functions.

Hello Sir,

Thank you for your quick reply. But the problem I see is at Mxsortrows. It takes almost 10 minutes to plot. The running close values of maximum volume points after clubbing. I have added some code for above

function TotalVolumeAtEqualClose(Fib,Lab)
{
	for(k=fib;k<=lab;k++)
	{
	
	fb=fib;lb=k;
	local dsmx1,mxx;
	
	dsmx=Matrix((lb-fb)+1,3,Null);// Main Matrix
	d=0;
	for(i=fb;i<=lb;i++)//Day matrix
	{
		dsmx[d][0]=C[i];
		dsmx[d][1]=V[i];
		dsmx[d][2]=i;//barindex
		d=d+1;
	}
	
	dsmx=MxSortRows(dsmx,True,0);// Sorted matrix to find number of records after finding equal close values
	bins=MxGetSize(dsmx,0);
	d=0;maxivol=0;localvol=0;localcl=0;
			for(j=0;j<bins;,j++)//finding total data points after equal close entries
			{
			if(j==0){d=d+1;}
			if(j>0 && dsmx[j][0]!=dsmx[j-1][0]){d=d+1;}
			}
	
	newmx=Matrix(d+1,3,Null);//Experiment Three columns close,volume and bar number
	d=0;
	
	dsmx1=dsmx;
	d=0;
	
			for(j=0;j<bins;,j++)//building new matrix
			{
			
			if(j==0)//first bar of the sorted Day matrix
			{
			localcl=dsmx1[j][0];
			localvol=dsmx1[j][1];
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j][2];
			d=d+1;
			}
			
			if(j>0 && j!=bins-1 && dsmx1[j][0]==dsmx1[j-1][0])
			{
			localvol=localvol+dsmx1[j][1];
			localcl=dsmx1[j][0];
			}
			if(j>0 && j!=bins-1 && dsmx1[j][0]!=dsmx1[j-1][0])
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j-1][2];
			d=d+1;
			localcl=dsmx1[j][0];
			localvol=dsmx1[j][1];
			}
			
			if(j>0 && j==bins-1 && dsmx1[j][0]!=dsmx1[j-1][0])
			{
			if(j==1)
			{
			newmx[d][0]=dsmx1[j][0];//localcl;
			newmx[d][1]=dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			else
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol;
			newmx[d][2]=dsmx1[j-1][2];
			d=d+1;
			newmx[d][0]=dsmx1[j][0];
			newmx[d][1]=dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			}
			if(j>0 && j==bins-1 && dsmx1[j][0]==dsmx1[j-1][0])
			{
			newmx[d][0]=localcl;
			newmx[d][1]=localvol+dsmx1[j][1];
			newmx[d][2]=dsmx1[j][2];
			}
			}
			mxx= MxSortRows(newmx,True,1);
			bins=MxGetSize(mxx,0);
	
		//mxx=EqualClose(Fb,k);
		//bins=MxGetSize(mxx,0);
	
	//Added NEW
	HVC1[k]=mxx[bins-1][0];
	//eqvolcl1[k]=mxx[bins-2][0];
	HVC2[k]=mxx[bins-2][0];
	//HVC3[k]=mxx[bins-3][0];
	//volatcl[k]=mxx[bins-1][1];
	HV1[k]=mxx[bins-1][1];
	//volatcl1[k]=mxx[bins-2][1];
	HV2[k]=mxx[bins-2][1];
		
	
	}
}

Your advise is very much needed

With regds
DSARAO

@fxshrat wrote you already - DON'T CALL mxSortRows inside loop. The screenshot is too small and unreadable but apparently your code is calling MxSortRows inside the loop 30000+ times and that is absurd.

1 Like

Hello Sir,

I am enclosing the images of the same chart with loop and with out loop.

Withoutloop
Withloop1

Is it possible to create withloop1 image type graph with out calling Mxsortrows multiple times?.

regds
DSARAO

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

You have to describe the GOAL, not focus on technical details. Unless THE GOAL is clear to anyone on this forum, people won't be able to help. Don't expect people to decipher your code. You need to describe goal in plain english.

Sir,
How can I send you a personal message?

Continuing the discussion from Mxsortrows function:
Hello Sir,

I would like to send you a personal message. Can I do it?

regds
DSA RAO

The following is the code using Mxsetblock and Mxgetblock. I have mentioned the time taken on top of the code. Please suggest any other better solution for tick data base.

//Works Good with 1 minute data 0.30 sec 05/08/2022 last modified on 05/08/2022
// On tick data base 1385.68 sec
// on 20 tick data	1.17 sec
SetBarsRequired(sbrall,1);
Varp=Param("DAYS BACK",0,0,5,1);
bi=BarIndex();
event=Day()!=Ref(Day(),1);
hvc=hvv=hvc2=hvv2=hvc3=hvv3=Null;
mr=BarCount;//Total rows
//global newmx1;

gblmxx=Null;gblmxx1=Null;
tt1=tt2=cnt=0;
fr=0;
lr=BarCount-1;
Mainmatrix=Matrix(mr,4,Null );
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,0,0,C);//Populating matrix all rows of first column with close values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,1,1,V);//Populating matrix all rows of second column with volume values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,2,2,Null);//Populating matrix all rows of third column with Null values

for(i=0;i<BarCount;i++)
{		if(i>0 && event[i])
		{
		cnt=cnt+1;
		}
}
Reqnum=cnt;
for(i=0;i<BarCount;i++)//Populating matrix all rows of third column with bar numbers
{
	Mainmatrix[i][2]=i;
}


function RunningEqClVol(locmat1,locmat2)//Updating Mainmatrix with cumulative volume values of equal close prices
{
			localclo=localvol=indx=0;
		
			dsmx=locmat1;//MxGetBlock(dsmxext,0,Lab-Fib,0,3,False);
			
			bins=MxGetSize(dsmx,0);
			
			d=0;
			
			newmx=locmat2;
			for(j=0;j<bins;,j++)//Updating newmx matrix with data points of equal close values
			{
			
			
			if(j==0 )
			{
			if(bins>1)
			{
			localclo=dsmx[j][0];
			localvol=dsmx[j][1];
			indx=dsmx[j][2];			
			}
			else
			{
			newmx[d][0]=dsmx[j][0];
			newmx[d][1]=dsmx[j][1];
			newmx[d][2]=dsmx[j][2];
			}
			}
							
			
			
			if(j>0 && j!=bins-1)
			{
				if(dsmx[j][0]== dsmx[j-1][0])
				{
					localvol=localvol+dsmx[j][1];
					localclo=dsmx[j][0];
					indx=dsmx[j][2];
				}
				else
				{
					newmx[d][0]=localclo;
					newmx[d][1]=localvol;
					newmx[d][2]=indx;//dsmx[j][];
					localvol=dsmx[j][1];
					localclo=dsmx[j][0];
					indx=dsmx[j][2];
					d=d+1;
				}
			}
			
					
		
			
			if(j>0 && j==bins-1)
			{
				if(dsmx[j][0]== dsmx[j-1][0])
				{
				newmx[d][1]=localvol+dsmx[j][1];
				newmx[d][0]=dsmx[j][0];
				newmx[d][2]=dsmx[j][2];
				}
				else
				{
				newmx[d][0]=localclo;
				newmx[d][1]=localvol;
				newmx[d][2]=indx;
				newmx[d+1][1]=dsmx[j][1];
				newmx[d+1][0]=dsmx[j][0];
				newmx[d+1][2]=dsmx[j][2];
				}
			}
			
			
			
		}
			return newmx;
		
		
		
}


//New Fuction 05/08/2022
function PlotClAtVolAtEqCl(Fib,Lab)
{
		
		//for(k=fib;k<=lab;k++)
		{
			k=lab;
			
			dsmx1=MxGetBlock(Mainmatrix,Fib,lab,0,3,False);
			dsmx1=MxSortRows(dsmx1,True,0,2);
			
			
			bins=MxGetSize(dsmx1,0);
			
			d=0;local mxx;
			for(j=0;j<bins;,j++)
			{
			{
			if(j==0){d=d+1;}
			if(j>0 && dsmx1[j][0]!=dsmx1[j-1][0]){d=d+1;}
			}
			}
			mxx=Matrix(d,3,Null);
			locx1=dsmx1;
			
			newmx1=RunningEqClVol(dsmx1,mxx);
			
			newmx1=MxSortRows(newmx1,True,1);
			bin2=MxGetSize(newmx1,0);
			if(bin2==1)
			{
			HVC[k]=newmx1[bin2-1][0];
			HVV[k]=newmx1[bin2-1][1];
			}
			if(bin2==2)
			{
				if(newmx1[bin2-2][1]==newmx1[bin2-1][1])
				{
					if(newmx1[bin2-2][2]>newmx1[bin2-1][2])
					{
					HVC[k]=newmx1[bin2-2][0];
					HVV[k]=newmx1[bin2-2][1];
					HVC2[k]=newmx1[bin2-1][0];
					HVV2[k]=newmx1[bin2-1][1];
					}
					else
					{
					HVC[k]=newmx1[bin2-1][0];
					HVV[k]=newmx1[bin2-1][1];
					HVC2[k]=newmx1[bin2-2][0];
					HVV2[k]=newmx1[bin2-2][1];
					}
				}
				else
				{
					HVC[k]=newmx1[bin2-1][0];
					HVV[k]=newmx1[bin2-1][1];
					HVC2[k]=newmx1[bin2-2][0];
					HVV2[k]=newmx1[bin2-2][1];
				}	
			}
			if(bin2>2)
			{
				if(newmx1[bin2-2][1]==newmx1[bin2-1][1])
				{
					if(newmx1[bin2-2][2]>newmx1[bin2-1][2])
					{
					HVC[k]=newmx1[bin2-2][0];
					HVV[k]=newmx1[bin2-2][1];
					HVC2[k]=newmx1[bin2-1][0];
					HVV2[k]=newmx1[bin2-1][1];
					HVC3[k]=newmx1[bin2-3][0];
					HVV3[k]=newmx1[bin2-3][1];
					}
					else
					{
					HVC[k]=newmx1[bin2-1][0];
					HVV[k]=newmx1[bin2-1][1];
					HVC2[k]=newmx1[bin2-2][0];
					HVV2[k]=newmx1[bin2-2][1];
					HVC3[k]=newmx1[bin2-3][0];
					HVV3[k]=newmx1[bin2-3][1];
					}
				}
				else
				{
					HVC[k]=newmx1[bin2-1][0];
					HVV[k]=newmx1[bin2-1][1];
					HVC2[k]=newmx1[bin2-2][0];
					HVV2[k]=newmx1[bin2-2][1];
					HVC3[k]=newmx1[bin2-3][0];
					HVV3[k]=newmx1[bin2-3][1];
				}
			}
			
		}

}
cnt=0;

for(g=0;g<BarCount;g++)
{
	if(g==0)
	{
	fb[g]=g;lb[g]=g;
	}
	if(g>0 && !event[g])
	{
	fb[g]=fb[g-1];lb[g]=g;
	if(Reqnum-cnt<=Varp)
	{
	PlotClAtVolAtEqCl(fb[g],g);
	}
	}
	if(g>0 && event[g])
	{
	cnt=cnt+1;
	fb[g]=fb[g-1];lb[g]=g;
	if(Reqnum-cnt<=Varp)
	{
	//PlotClAtVolAtEqCl(fb[g],g);
	}
	fb[g]=g+1;
	}
	if(g==BarCount-1)
	{
	fb[g]=fb[g-1];lb[g]=g;
	PlotClAtVolAtEqCl(fb[g],g);
	
	}


}


RequestTimedRefresh(1,True);

My laptop configuration is Pentium 1.9GHz,Windows 10Pro , 8 GB RAM. Is it better to change system configuration?.

Thank you.

Hello fxshrat
@fxshrat as you said I have created a new code which is enclosed below

SetBarsRequired(5000,1);

newday=Day()!=Ref(Day(),-1);
hvc=hvv=hvc2=hvv2=hvc3=hvv3=Null;

fb[0]=0;lb[0]=0;ahcvv[0]=Null;

mr=BarCount;//Total rows

fr=0;
lr=BarCount-1;
Mainmatrix=Matrix(mr,5,Null );
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,0,0,C);//Populating matrix all rows of first column with close values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,1,1,V);//Populating matrix all rows of second column with volume values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,2,2,Null);//Populating matrix all rows of third column with Null values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,3,3,Null);//Populating matrix all rows of fourth column with Null values
Mainmatrix=MxSetBlock(Mainmatrix,fr,lr,4,4,Null);//Populating matrix all rows of fifth column with Null values


for(i=0;i<BarCount;i++)//Populating matrix all rows 
{
	
	if(i==0)
	{
		fb[i]=i;
		lb[i]=i;
		Mainmatrix[i][2]=i;//Third column's row barindex
		Mainmatrix[i][3]=i;//fourth column's row index
		Mainmatrix[i][4]=i;//fifth column's row index
	}
	
	if(i>0 && !newday[i])
	{
		fb[i]=fb[i-1];
		lb[i]=i;
		Mainmatrix[i][2]=i;
		Mainmatrix[i][3]=fb[i];
		Mainmatrix[i][4]=i;
	
	}
	
	if(i>0 && newday[i])
	{
		fb[i]=i;//fb[i-1];
		lb[i]=i;
		Mainmatrix[i][2]=i;
		Mainmatrix[i][3]=fb[i];
		Mainmatrix[i][4]=i;
	
	}
}


function RunningEqClVol(locmat1,locmat2)//Updating Mainmatrix with cumulative volume values of equal close prices
{
			localclo=localvol=indx=0;
			dsmx=locmat1;//MxGetBlock(dsmxext,0,Lab-Fib,0,3,False);
			bins=MxGetSize(dsmx,0);
			d=0;
			newmx=locmat2;
			for(j=0;j<bins;,j++)//Updating newmx matrix with data points of equal close values
			{
			if(j==0 )
			{
			if(bins>1)
			{
			localclo=dsmx[j][0];
			localvol=dsmx[j][1];
			indx=dsmx[j][2];			
			}
			else
			{
			newmx[d][0]=dsmx[j][0];
			newmx[d][1]=dsmx[j][1];
			newmx[d][2]=dsmx[j][2];
			}
			}
			if(j>0 && j!=bins-1)
			{
				if(dsmx[j][0]== dsmx[j-1][0])
				{
					localvol=localvol+dsmx[j][1];
					localclo=dsmx[j][0];
					indx=dsmx[j][2];
				}
				else
				{
					newmx[d][0]=localclo;
					newmx[d][1]=localvol;
					newmx[d][2]=indx;//dsmx[j][];
					localvol=dsmx[j][1];
					localclo=dsmx[j][0];
					indx=dsmx[j][2];
					d=d+1;
				}
			}
			if(j>0 && j==bins-1)
			{
				if(dsmx[j][0]== dsmx[j-1][0])
				{
				newmx[d][1]=localvol+dsmx[j][1];
				newmx[d][0]=dsmx[j][0];
				newmx[d][2]=dsmx[j][2];
				}
				else
				{
				newmx[d][0]=localclo;
				newmx[d][1]=localvol;
				newmx[d][2]=indx;
				newmx[d+1][1]=dsmx[j][1];
				newmx[d+1][0]=dsmx[j][0];
				newmx[d+1][2]=dsmx[j][2];
				}
			}
		}
			return newmx;
}


selbar=SelectedValue(BarIndex());//4
x=MxGetBlock(Mainmatrix,Mainmatrix[selbar][3],Mainmatrix[selbar][4],0,4,False);//4
dsmx1=MxSortRows(x,True,0,2);
		bins=MxGetSize(dsmx1,0);
			d=0;local mxx;
			
			for(j=0;j<bins;,j++)
			{	
				if(j==0){d=d+1;}
				if(j>0 && dsmx1[j][0]!=dsmx1[j-1][0]){d=d+1;}
				
			}
			mxx=Matrix(d,3,Null);
			newmx1=RunningEqClVol(dsmx1,mxx);
			newmx1=MxSortRows(newmx1,True,1);
		
			bin2=MxGetSize(newmx1,0);
			if(bin2==1)
			{
			HVC=newmx1[bin2-1][0];
			HVV=newmx1[bin2-1][1];
			}
			if(bin2==2)
			{
				if(newmx1[bin2-2][1]==newmx1[bin2-1][1])
				{
					if(newmx1[bin2-2][2]>newmx1[bin2-1][2])
					{
					HVC=newmx1[bin2-2][0];
					HVV=newmx1[bin2-2][1];
					HVC2=newmx1[bin2-1][0];
					HVV2=newmx1[bin2-1][1];
					}
					else
					{
					HVC=newmx1[bin2-1][0];
					HVV=newmx1[bin2-1][1];
					HVC2=newmx1[bin2-2][0];
					HVV2=newmx1[bin2-2][1];
					}
				}
				else
				{
					HVC=newmx1[bin2-1][0];
					HVV=newmx1[bin2-1][1];
					HVC2=newmx1[bin2-2][0];
					HVV2=newmx1[bin2-2][1];
				}	
			}
			if(bin2>2)
			{
				if(newmx1[bin2-2][1]==newmx1[bin2-1][1])
				{
					if(newmx1[bin2-2][2]>newmx1[bin2-1][2])
					{
					HVC=newmx1[bin2-2][0];
					HVV=newmx1[bin2-2][1];
					HVC2=newmx1[bin2-1][0];
					HVV2=newmx1[bin2-1][1];
					HVC3=newmx1[bin2-3][0];
					HVV3=newmx1[bin2-3][1];
					}
					else
					{
					HVC=newmx1[bin2-1][0];
					HVV=newmx1[bin2-1][1];
					HVC2=newmx1[bin2-2][0];
					HVV2=newmx1[bin2-2][1];
					HVC3=newmx1[bin2-3][0];
					HVV3=newmx1[bin2-3][1];
					}
				}
				else
				{
					HVC=newmx1[bin2-1][0];
					HVV=newmx1[bin2-1][1];
					HVC2=newmx1[bin2-2][0];
					HVV2=newmx1[bin2-2][1];
					HVC3=newmx1[bin2-3][0];
					HVV3=newmx1[bin2-3][1];
				}
			}
			
			
			
			
Plot( HVC, "HiVolCl1", colorRed, styleStaircase|styleThick );
Plot( HVV, "VolAtCl1", colorRed, styleNoDraw|styleNoLabel|styleNoLine|styleNoRescale );
Plot( HVC2, "HiVolCl2", colorBrightGreen, styleStaircase|styleThick );
Plot( HVV2, "VolAtCl2", colorRed, styleNoDraw|styleNoLabel|styleNoLine|styleNoRescale );
Plot( HVC3, "HiVolCl3", colorBlue, styleStaircase|styleThick );
Plot( HVV3, "VolAtCl3", colorBlue, styleNoDraw|styleNoLabel|styleNoLine|styleNoRescale );


and the screen shot of its manifestation.

Tickchart

In this the lines are of last values. When I select a bar on the chart the plots shows the values at the bar. My problem is how to get the plots bar by bar without using loop. Your advise is highly appreciated.

With regds
DSARAO

@appasri, waiting for a better answer, you can try the following code snippet that should be equivalent to your 'Barcount' loop:

bi = BarIndex();
lb = bi;
mainMatrix = MxSetBlock( mainMatrix, 0, lr, 2, 2, bi );
mainMatrix = MxSetBlock( mainMatrix, 0, lr, 4, 4, bi );
newday = Day() != Ref( Day(), -1 ); // line moved here 
fb = iif( newDay, bi, NZ( ValueWhen( newDay, bi ) ) );
fb[0] = 0; // fill the missing initial value
mainMatrix = MxSetBlock( mainMatrix, 0, lr, 3, 3, fb );

I do not have tick data to test, so on my system with 1M the speed gain is limited.

2 Likes

@beppe, Thank You for the reply. As @fxshrat advised in the above post I tried to create above code. There is no issue with the speed of the AFL. Please try with bar replay. The values of plot lines change as selected/current bar changes. What I am trying to do is in the below chart. The Chart should look like that. I have other code which uses mxsortrows function, which is dead slow but works. And @Tomasz suggested not to use nxsortrows in a loop. Thanks once again for your guidance.

With regds
DSARAO

Tickchart

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.