ArraySort - sorting floating point variables

 

Hi, can anyone advise how to make code to sort floating point variables as simple as this example for integers? Thanks

#define nl "\n"

int OnInit()
  {
   EventSetMillisecondTimer(500);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTimer()
  {
   double eu,gu,uj,ac,an;

   eu=iRSI("EURUSD",_Period,14,PRICE_CLOSE,0);
   eu=NormalizeDouble(eu,2);
   gu=iRSI("GBPUSD",_Period,14,PRICE_CLOSE,0);
   gu=NormalizeDouble(gu,2);
   uj=iRSI("USDJPY",_Period,14,PRICE_CLOSE,0);
   uj=NormalizeDouble(uj,2);
   ac=iRSI("AUDCAD",_Period,14,PRICE_CLOSE,0);
   ac=NormalizeDouble(ac,2);
   an=iRSI("AUDNZD",_Period,14,PRICE_CLOSE,0);
   an=NormalizeDouble(an,2);

   string pairs[] = {"EURUSD", "GBPUSD", "USDJPY", "AUDCAD", "AUDNZD"};
   string sp="           ",sp2="   ",sp3="              ";
   int num_array_de[5][2];
   ArrayInitialize(num_array_de, 2);

   num_array_de[0][0] = eu;num_array_de[0][1] = 0;
   num_array_de[1][0] = gu;num_array_de[1][1] = 1;
   num_array_de[2][0] = uj;num_array_de[2][1] = 2;
   num_array_de[3][0] = ac;num_array_de[3][1] = 3;
   num_array_de[4][0] = an;num_array_de[4][1] = 4;
   ArraySort(num_array_de,WHOLE_ARRAY,0,MODE_DESCEND);

   int num_array_as[5][2];
   ArrayInitialize(num_array_as, 2);
   
   num_array_as[0][0] = eu;num_array_as[0][1] = 0;
   num_array_as[1][0] = gu;num_array_as[1][1] = 1;
   num_array_as[2][0] = uj;num_array_as[2][1] = 2;
   num_array_as[3][0] = ac;num_array_as[3][1] = 3;
   num_array_as[4][0] = an;num_array_as[4][1] = 4;
   ArraySort(num_array_as,WHOLE_ARRAY,0,MODE_ASCEND);


   Comment(nl+"RSI descending"+sp+"RSI ascending"+nl+"---------------------"+sp+"--------------------"+nl+
           pairs[num_array_de[0][1]]+sp2+num_array_de[0][0]+sp3+pairs[num_array_as[0][1]]+sp2+num_array_as[0][0]+nl+
           pairs[num_array_de[1][1]]+sp2+num_array_de[1][0]+sp3+pairs[num_array_as[1][1]]+sp2+num_array_as[1][0]+nl+
           pairs[num_array_de[2][1]]+sp2+num_array_de[2][0]+sp3+pairs[num_array_as[2][1]]+sp2+num_array_as[2][0]+nl+
           pairs[num_array_de[3][1]]+sp2+num_array_de[3][0]+sp3+pairs[num_array_as[3][1]]+sp2+num_array_as[3][0]+nl+
           pairs[num_array_de[4][1]]+sp2+num_array_de[4][0]+sp3+pairs[num_array_as[4][1]]+sp2+num_array_as[4][0]);
  }

 
losdelamos:  Hi, can anyone advise how to make code to sort floating point variables as simple as this example for integers? Thanks

Your code is your own answer—ArraySort will sort any numerical data-type, be it an int, a float, a double, or whatever. Please see the documentation.
ArraySort - Array Functions - MQL4 Reference
ArraySort - Array Functions - MQL4 Reference
  • docs.mql4.com
ArraySort - Array Functions - MQL4 Reference
 

Thank you, but the problem is when I change int to double so it reports errors

double num_array[5][2];


 

Files:
errors.jpg  92 kb
 
losdelamos #: Thank you, but the problem is when I change int to double so it reports errors

Is that not obvious? You are using the value of the "num_array_??" to serve as an index into the "pairs" array, so it has to result in a valid integer index.

Your original question was how to easily sort arrays of floating-point data-type. The answer is use "ArraySort".

Your question was not—"Can I index an array with a floating point value?" To which the answer would have been—No!

The two things are completely different and irrespective of each other.

What is it that you are trying to achieve? Explain in more detail so that we can give you advice.

 

Thank you for your quick responses.

I solved the problem, but it's my solution as a beginner not a programmer.


#define nl "\n"

int OnInit()
  {
   EventSetMillisecondTimer(500);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTimer()
  {
   double eu,gu,uj,ac,an,num_array_de2[5][2],num_array_as2[5][2];
   string pairs[]={"EURUSD", "GBPUSD", "USDJPY", "AUDCAD", "AUDNZD"};
   string sp="                ",sp1="              ",sp2="   ",sp3="              ";
   int num_array_de[5][2],num_array_as[5][2];
   
   eu=iRSI("EURUSD",_Period,14,PRICE_CLOSE,0);eu=NormalizeDouble(eu,2);
   gu=iRSI("GBPUSD",_Period,14,PRICE_CLOSE,0);gu=NormalizeDouble(gu,2);
   uj=iRSI("USDJPY",_Period,14,PRICE_CLOSE,0);uj=NormalizeDouble(uj,2);
   ac=iRSI("AUDCAD",_Period,14,PRICE_CLOSE,0);ac=NormalizeDouble(ac,2);
   an=iRSI("AUDNZD",_Period,14,PRICE_CLOSE,0);an=NormalizeDouble(an,2);
   
   ArrayInitialize(num_array_de,2);ArrayInitialize(num_array_de2,2);
   ArrayInitialize(num_array_as,2);ArrayInitialize(num_array_as2,2);
   
   num_array_de2[0][0]=eu;num_array_de[0][1]=0;num_array_as2[0][0]=eu;num_array_as[0][1]=0;
   num_array_de2[1][0]=gu;num_array_de[1][1]=1;num_array_as2[1][0]=gu;num_array_as[1][1]=1;
   num_array_de2[2][0]=uj;num_array_de[2][1]=2;num_array_as2[2][0]=uj;num_array_as[2][1]=2;
   num_array_de2[3][0]=ac;num_array_de[3][1]=3;num_array_as2[3][0]=ac;num_array_as[3][1]=3;
   num_array_de2[4][0]=an;num_array_de[4][1]=4;num_array_as2[4][0]=an;num_array_as[4][1]=4;
   
   ArraySort(num_array_de,WHOLE_ARRAY,0,MODE_DESCEND);ArraySort(num_array_de2,WHOLE_ARRAY,0,MODE_DESCEND);
   ArraySort(num_array_as,WHOLE_ARRAY,0,MODE_ASCEND);ArraySort(num_array_as2,WHOLE_ARRAY,0,MODE_ASCEND);
      
   Comment(nl+"RSI descending"+sp+"RSI ascending"+nl+"-----------------------"+sp1+"----------------------"+nl+
           pairs[num_array_de[0][1]]+sp2+num_array_de2[0][0]+sp3+pairs[num_array_as[0][1]]+sp2+num_array_as2[0][0]+nl+
           pairs[num_array_de[1][1]]+sp2+num_array_de2[1][0]+sp3+pairs[num_array_as[1][1]]+sp2+num_array_as2[1][0]+nl+
           pairs[num_array_de[2][1]]+sp2+num_array_de2[2][0]+sp3+pairs[num_array_as[2][1]]+sp2+num_array_as2[2][0]+nl+
           pairs[num_array_de[3][1]]+sp2+num_array_de2[3][0]+sp3+pairs[num_array_as[3][1]]+sp2+num_array_as2[3][0]+nl+
           pairs[num_array_de[4][1]]+sp2+num_array_de2[4][0]+sp3+pairs[num_array_as[4][1]]+sp2+num_array_as2[4][0]);
  }
 
losdelamos #: Thank you for your quick responses. I solved the problem, but it's my solution as a beginner not a programmer.

You never described your "problem" nor what you wanted to achieve, so we have no ideia what it is you "solved".

 
Sorry,  the problem was that the decimal places in rsi were not displayed.
 

While checking the ea run i found it not working properly. RSI sorts both ascending and descending, but currency symbols and RSI do not match

 
Until you are willing to properly describe in detail what it is you want to achieve, we are not able to offer you proper advice.
 

Ok, I'll explain. The first screen is a working example(the first code I posted), but it shows integers only. I need to see the decimal places and have everything correctly sorted as it is in the first screen.

The second screen shows the decimal places, but the symbols are not sorted at all (not associated with RSI values) .
Files:
 

Thank you, but the problem is when I change int to double so it reports errors

// cast the double to an int before using as an index
//
pairs[(int)num_array_de[0][1]]