I got an Array with some numbers in it, I am trying to get the most Occuring number inside the array, not sure how to do that, please help!

 

I got an Array with some numbers in it, I am trying to get the most Occurring number inside the array, not sure how to do that, please help!


Lets say im recording the last 100 historical bars,

and im trying to count and set my Result value as the most Occurring/repeatable number inside this array,

i tried everything i could, yet no success..



//Record into the Array the last 100 bars prices:

double arr[100];

      for(int i=0; i<ArraySize(arr); i++){
         arr[i]=iClose(0,1,i);
         //printf(DoubleToString( arr[i] ,Digits));
      }


//Not sure how to go on from here.., i want to loop through all the elements inside this array, and get the most occurring value/number inside of it,

// preferably the top 3 numbers that are occurring the most.


please help!

 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
Karish:

I got an Array with some numbers in it, I am trying to get the most Occurring number inside the array, not sure how to do that, please help!


Lets say im recording the last 100 historical bars,

and im trying to count and set my Result value as the most Occurring/repeatable number inside this array,

i tried everything i could, yet no success..



//Record into the Array the last 100 bars prices:

double arr[100];

      for(int i=0; i<ArraySize(arr); i++){
         arr[i]=iClose(0,1,i);
         //printf(DoubleToString( arr[i] ,Digits));
      }


//Not sure how to go on from here.., i want to loop through all the elements inside this array, and get the most occurring value/number inside of it,

// preferably the top 3 numbers that are occurring the most.


please help!

Hello , are you building a volume profile ?

Here is how you can go about it 

create a class object that has a price and a count 

class priceWithFrequency{
public:
double price;
int times;
priceWithFrequency(void){price=0.0;times=0;}
void set(double _price){price=_price;times=1;}
void increase(){times++;}
bool matches(double _other_price,double _range){
if(_other_price>=price-range&&_other_price<=price+range){return(true);}
return(false);       
}
};

then create an array of those objects (instead of your price array)

and before you add a price check if it exists

If it does not exist add it to the array

if it exists increase the count

 
Karish: , I am trying to get the most Occurring number inside the array,
arr[i]=iClose(0,1,i);
  1. Since you are dealing with doubles., you can't just compare them. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  2. Search for Market Profile. Free download of the 'Market Profile' indicator by 'Collector' for MetaTrader 4 in the MQL5 Code Base, 2006.10.16

 
Lorentzos Roussos #:

Hello , are you building a volume profile ?

Here is how you can go about it 

create a class object that has a price and a count 

then create an array of those objects (instead of your price array)

and before you add a price check if it exists

If it does not exist add it to the array

if it exists increase the count

Hi thanks, No i am not building a Market Profile thing although it got some similar things in what i am trying to do,


My goal to achieve is to record the last 100 bars into array,

then,

rank them via Print statement of Top 5 most occurring prices out of those 100 price bars.


thats pretty much it, thanks for the helpers,


i got a look into the market profile indicator, kinda does something that i want but i got all confused by reviewing the code,

it would be much appreciated if someone could help me with just ranking the Top 5 occurred prices from the last 100 price bars in terms of Print statements like so for example:

Top #1: 1.23456

Top #2: 1.23468

Top #3: 1.27651

Top #4: 1.23663

Top #5: 1.21553


^ thats exactly that im looking for

 
Karish #: it would be much appreciated if someone could help me with just ranking the Top 5 occurred prices from the last 100 price bars i

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 (2013)

Therefor, all prices have the same rank (one).

What prices in your 100 price bars? OHLC is all you have.

 
William Roeder #:

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 (2013)

Therefor, all prices have the same rank (one).

What prices in your 100 price bars? OHLC is all you have.

Closed price of each bar of 100 bars that is..

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
 

Interesting.

Follow the given steps to solve the problem:

  • Use a sorting algorithm to sort the elements
  • Iterate the sorted array and construct a 2D array of elements and count
  • Sort the 2D array according to the count 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
template<typename T>
void ArraySortByFrequency(T &arr[], T &sorted[])
  {
   int n = ArraySize(arr);
//--- Use a sorting algorithm to sort the elements
   ArraySort(arr);
//--- Construct a 2D arr of sorted unique elements and count
   T counts[][2];
   int ranks = 0;
   for(int i = 0; i < n; i++)
     {
      int k = i;
      while(i < n - 1 && arr[i] == arr[i + 1])
         i++;
      ArrayResize(counts,ranks+1);
      counts[ranks][0] = i - k + 1;
      counts[ranks][1] = arr[i];
      ranks++;
     }
//--- Sort the 2D array according to the count in descending order
   ArraySort(counts);
   ArrayReverse(counts);
//--- Copy back to the sorted array
   ArrayResize(sorted,ranks);
   for(int i = 0; i < ranks; i++)
     {
      sorted[i]=counts[i][1];
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   int a[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
   int sorted[];

   ArraySortByFrequency(a, sorted);
   ArrayPrint(sorted);
  }
  
// output:
// 8       2       5      -1       6 9999999
 
amrali #:

Interesting.

Follow the given steps to solve the problem:

  • Use a sorting algorithm to sort the elements
  • Iterate the sorted array and construct a 2D array of elements and count
  • Sort the 2D array according to the count 
wow! thanks!!
Reason: