Download MetaTrader 5

Sort array by value

To add comments, please log in or register
Share the links to products and make your profit
Mindexperiment
87
Mindexperiment 2014.05.16 16:43 

Hi guys,


I need to sort an array by values... or at least find the 2 minimun/maximun values of a range of data...


I have the array (series array) daLow[100] correctly populated with the last 100 Low.


This is what I have done so far:

   int iLowest1, iLowest2, iHighest1, iHighest2;

   iLowest1 = ArrayMinimum( daLows, (iRange/2)+iPeriod, iRange-iPeriod );
   iLowest2 = ArrayMinimum( daLows, iPeriod, (iRange/2)-iPeriod );
   iHighest1 = ArrayMaximum( daHighs, (iRange-iPeriod)/2, iRange-iPeriod );
   iHighest2 = ArrayMaximum( daHighs, 0, (iRange-iPeriod)/2+iPeriod );

   sObjName = sId+"Support";
   ObjectMove( 0, sObjName, 0, daTimes[iLowest1], daLows[iLowest1] );
   ObjectMove( 0, sObjName, 1, daTimes[iLowest2], daLows[iLowest2] );

   sObjName = sId+"Resistence";
   ObjectMove( 0, sObjName, 0, daTimes[iHighest1], daHighs[iHighest1] );
   ObjectMove( 0, sObjName, 1, daTimes[iHighest2], daHighs[iHighest2] );


obviously daLows, daHighs and daTimes is correctly populated but as you can see this is not an elegant way to work.

How to improve this code?

"read all the array and gimme the two minimun/maximun values (and index)"

Rodrigo Malacarne
Moderator
9889
Rodrigo Malacarne 2014.05.16 17:44  
Mindexperiment:

Hi guys,

How to improve this code?

"read all the array and gimme the two minimun/maximun values (and index)"

Hi Mindexperiment,

Have you seen the function ArraySort

Documentação sobre MQL5: Funções para Array / ArraySort
Documentação sobre MQL5: Funções para Array / ArraySort
  • www.mql5.com
Funções para Array / ArraySort - Referência sobre algorítimo/automatização de negociação na linguagem para MetaTrader5
Mindexperiment
87
Mindexperiment 2014.05.16 18:08  

Yess but


   ArraySort( daLows );
   ArrayResize( daLows, 2 );

   sObjName = sId+"Support";
   ObjectMove( 0, sObjName, 0, daTimes[14], daLows[1] );
   ObjectMove( 0, sObjName, 1, daTimes[52], daLows[0] );

I miss the index for the time


How can I  get the low index?

Mindexperiment
87
Mindexperiment 2014.05.16 18:37  

I wrote this function


//+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"

double ArraySortByValues( double &A[] ) {
   double B[];
   double R[2][2];

   int size = ArraySize( A );
   ArrayCopy( B, A );
   ArraySort( B );

   for ( int i=0; i<size; i++ ) {
      if ( A[i] = B[0] ) {
         R[0][0] = i;
         R[0][1] = A[i];
      }
      if ( A[i] = B[1] ) {
         R[1][0] = i;
         R[1][1] = A[i];
      }
   }

   return R;
}   


I'm having error on "return R"....... probably this is not the right sintax, which is the right one?

Artem Temnikov
1026
Artem Temnikov 2014.05.16 20:43  
Mindexperiment:

I wrote this function



I'm having error on "return R"....... probably this is not the right sintax, which is the right one?

You can not return arrays.
Mindexperiment
87
Mindexperiment 2014.05.17 12:13  

Ok, without array


//+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"

struct strTline {
   double dLeftPrice;
   int iLeftIndex;
   double dRightPrice;
   int iRightIndex;
};

strTline ArraySortByValues( double &A[] ) {
   double B[];
   strTline tLine;

   int size = ArraySize( A );
   ArrayCopy( B, A );
   ArraySort( B );

   for ( int i=0; i<size; i++ ) {
      if ( A[i] == B[0] ) {
         tLine.iLeftIndex = i;
         tLine.dLeftPrice = A[i];
      }
      if ( A[i] == B[1] ) {
         tLine.iRightIndex = i;
         tLine.dRightPrice = A[i];
      }
   }

   return tLine;
}

Now I have this error:

"possible use of uninitialized variable 'tLine'    ArraySortByValues.mqh    35    11" on "return tLine;"

Artem Temnikov
1026
Artem Temnikov 2014.05.17 12:29  
Mindexperiment:

Ok, without array


Now I have this error:

"possible use of uninitialized variable 'tLine'    ArraySortByValues.mqh    35    11" on "return tLine;"

Rid of the warning, you can:

 //+------------------------------------------------------------------+
//|                                            ArraySortByValues.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property  copyright "Copyright 2014, MetaQuotes Software Corp."
#property  link        "http://www.mql5.com"

struct strTline {
   double dLeftPrice;
   int iLeftIndex;
   double dRightPrice;
   int iRightIndex;
};

strTline ArraySortByValues( double &A[] ) {
   double B[];
   strTline tLine={0};

   int size = ArraySize ( A );
   ArrayCopy ( B, A );
   ArraySort ( B );

   for ( int i= 0 ; i<size; i++ ) {
       if ( A[i] == B[ 0 ] ) {
         tLine.iLeftIndex = i;
         tLine.dLeftPrice = A[i];
      }
       if ( A[i] == B[ 1 ] ) {
         tLine.iRightIndex = i;
         tLine.dRightPrice = A[i];
      }
   }

   return tLine;
}
To add comments, please log in or register