How to create rank function for some values? Need Help

 
Anybody can help me how to create rank function

for example I have some double value's like theese:
a=3.0; b=4.0; c=5.0; d=2.0; e=1.0; f=6.0; g=0.0;

from higher to lower value (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)
or the value of (f,c,b,a,d,e,g)

So the rank's should be like this
rank number: (th)
f=1 ; c=2; b=3; a=4; d=5; e=6; g=7;

What I need is a function that return the rank if I input the value

int rank (double value)
{
process all value's;


return (the rank of a value from all value's)
}


I know it is easy if we just make a function like this

for example value of "a"
int rank (double value)
{
if(a>b && a>c && a>d && a>e && a>f && a>g)

return (1);


if( (a<b && a>c && a>d && a>e && a>f && a>g)||

(a<c && a>b && a>d && a>e && a>f && a>g)||

(a<d && a>b && a>c && a>e && a>f && a>g)||

(a<e && a>b && a>c && a>d && a>f && a>g)||

(a<f && a>b && a>c && a>d && a>e && a>g)||

(a<g && a>b && a>c && a>d && a>e && a>f) )

return (2);

.

.
.
.

if(a<b && a<c && a<d && a<e && a<f && a<g)
return(7);
}

The Problem is when the variations of value are 2 the combination only a few, but when the variations increase for example 5,6,7 or 10 etc.. so the combination to make the rank will very very a lot..

Hope somebody can help me for this kind of problem..

Thank's berfore, Sorry for my poor english, but I'm learning..
 
metropolis:
Anybody can help me how to create rank function

for example I have some double value's like theese:
a=3.0; b=4.0; c=5.0; d=2.0; e=1.0; f=6.0; g=0.0;

from higher to lower value (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)
or the value of (f,c,b,a,d,e,g)

So the rank's should be like this
rank number: (th)
f=1 ; c=2; b=3; a=4; d=5; e=6; g=7;

What I need is a function that return the rank if I input the value

int rank (double value)
{
process all value's;


return (the rank of a value from all value's)
}

HI


Use the following function RankArray

I include an example of how to use it by calling from a dummy indicator

------------------------------------------------------------------

//Example of using RankArray routine
#property copyright "TraderSYoung"
int start() {return(0);}
int deinit() {return(0);}

int init() {
double A1[]={3.1,6.7,5.2,1.005,2.002,3.5,4.77,4.78};

int Ranked[1];

for (int i=0;i<ArrayRange(A1,0);i++) {
Print ("before ",i," ", A1[i]);
}

RankArray(A1,Ranked,true);
for ( i=0;i<ArrayRange(A1,0);i++) {
Print ("after Ascending Rank ",i+1," is value:". A1[Ranked[i]]);
}

RankArray(A1,Ranked,false);
for ( i=0;i<ArrayRange(A1,0);i++) {
Print ("after Descending Rank ",i+1," is value:". A1[Ranked[i]]);
}


return(0);
}



//+-------------------------------------------------------------

void RankArray(double InputArray[], int &Ranked[],bool sort_descending=true) {

//Purpose: Input: an array with a set of numbers that have been calculated in some fashion
// Output is array RankedArray[] such that
// RankedArray[0] points to the first member of the InputArray[]
// i.e. InputArray[Ranked[0]] is highest value
// InputArray[Ranked[1]] is next highest, ect.
// InputArray[] is unchanged
// sort_descinging optional, default- true - is for Descending Order
// false will sort in ascending order
// User can modify as necessary for two dimensional Arrays.

int InputArraySize=ArrayRange(InputArray,0);

// Create Temporary array to sort - with pointers to original element
double TempArray[1][2];
ArrayResize(TempArray,InputArraySize);

for (int i=0;i<InputArraySize;i++)
{TempArray[i][0]=InputArray[i];
TempArray[i][1]=i;
}

int sort_dir;
//Validate input values
if (!sort_descending) sort_dir=MODE_ASCEND;
else sort_dir=MODE_DESCEND;

//Sort the Array-first element only
ArraySort(TempArray,WHOLE_ARRAY,0,sort_dir);

//Create output array - ranked by pointing to orignal values in ranked order
ArrayResize(Ranked,InputArraySize);
for (i=0;i<InputArraySize;i++)
{Ranked[i]=TempArray[i][1];}
return;
}


 

hello TradersYoung and Metropolis,

I understand you have deal with problem long time ago - i am facing the same issure and I found your coding very useful to understand how MT4 handles ranking. I had a quesiton for your. what is the meaning of this part of the code "&Ranked[]". What I am not clear is what this piece of function differs from "Ranked[]".

as you can undestand I am a newbe, but catching up fast with this matter - I am a trader learning how to code....and kind of starting from scratch

appreaciate your quick input to shed some light on the matter.

Best,

MM

 

By using the ampersand (&) you are explicitly passing the array by reference .

https://www.mql5.com/en/articles/1391#1_5 

 
tradersyoung: Use the following function RankArray
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Also you can just calculate the rank on the fly
    double A1[]={3.1,6.7,5.2,1.005,2.002,3.5,4.77,4.78};
    Print("Rank of A1[2]=="+DoubleToStr(A1[2])+" is "+Rank(A1,A1[2]);
    :
    int Rank(double arr[], double value, int mode=MODE_ASCEND){
       int dir  = mode == MODE_ASCEND ? +1 : -1;
       int rank = 0;
       for(int iArr = ArraySize() - 1; iArr >= 0; --iArr)
           if( (value - Arr[iArr])*dir >= 0) ++rank;
       return rank;
    }

 
     double x[]={2.0, 1.0, 6.0, 3.0, 0.0, 5.0, 4.0};
     double s;
     int e;
     int size=ArraySize(x);
        for(int n=0; n<size; n++)
        {
         s=-90000000.0; // in the opposite direction replace with: s=90000000.0;
         for(i=n; i<size; i++)
           {
            if(s<x[i]) // in the opposite direction replace with: if(s>x[i])
              {
               s=x[i];
               e=i;
              }
           }
         x[e]=x[n];
         x[n]=s;
        }
   Alert(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);



   string w[]={"E","c","f","b","j","R","A","H","a","c"};
   int size=ArraySize(w);
   string abc[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
   string ABC[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
   int c,e,i;

   for(i=0; i<26; i++)
     {
      for(e=c; e<size; e++)
        {
         if(ABC[i]==w[e])
           {
            w[e]=w[c];
            w[c]=ABC[i];
            c++;
           }
         else
           {
            if(abc[i]==w[e])
              {
               w[e]=w[c];
               w[c]=abc[i];
               c++;
              }
           }


        }
     }
   
   Alert(w[0],w[1],w[2],w[3],w[4],w[5],w[6],w[7],w[8],w[9]);


      string w[]={"EURUSD","USDCHF","BRNQ4","AUDUSD","GOLD","AUDCHF","NZDUSD","USDCAD","SILVER","GBPUSD"};
   int size=ArraySize(w);
   string f=" ";
   string ABC[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
   int c,e,i;

   for(i=0; i<26; i++)
     {
      for(e=c; e<size; e++)
        {
         f=w[e];
         if(ABC[i]==StringSubstr(f,0,1))
           {
            w[e]=w[c];
            w[c]=f;
            c++;
           }
        }
     }

   Alert(w[0],", ",w[1],", ",w[2],", ",w[3],", ",w[4],", ",w[5],", ",w[6],", ",w[7],", ",w[8],", ",w[9]);
 

In case you only require the index of the min max value in the array: 

https://docs.mql4.com/array/arraymaximum

https://docs.mql4.com/array/arrayminimum

Reason: