Download MetaTrader 5

Most frequent range of numbers in an array.

To add comments, please log in or register
DanielsRUS
14
DanielsRUS  

Hi,

I am totally new to programming and forgive me for my ignorance.
But I'm looking for code that can show me the most frequent range of numbers in an array.
Lets say there is an array with these values { 1 ,2 ,2 ,5 ,8 ,8 ,9 ,10} and the range is +-1..
 most frequent range of numbers would be 8-10.
My end result would be after inputting bar closing prices over specified period of time
 EA would display support or resistance lines.


 Is this possible? Can anyone help please?

Fernando Carreiro
3893
Fernando Carreiro  
DanielsRUS:

Hi,

I am totally new to programming and forgive me for my ignorance.
But I'm looking for code that can show me the most frequent range of numbers in an array.
Lets say there is an array with these values { 1 ,2 ,2 ,5 ,8 ,8 ,9 ,10} and the range is +-1..
 most frequent range of numbers would be 8-10.
My end result would be after inputting bar closing prices over specified period of time
 EA would display support or resistance lines.

Is this possible? Can anyone help please?

Yes, obviously it is possible. It is just simple math! However, like everything in life, you have to develop the skills in order to do it.

In this case, you either have someone else do it for you, but then you don't really learn to develop your skills; or you you sit down and work out step by step how to implement it in code.

Learning to code is not something you do in one weekend. You have to study and practice, and to become a pro, requires years of that.

Just think about the steps:

  1. You have to have another array where you keep track of a hit count or frequency of the elements being tested.
  2. As you run through you main array, you increment the count for that value in another array.
  3. In the end, you can evaluate the frequency array to find the highest counts, either by using a running comparison, or by first sorting it.

This is all basic coding skills, which you need to develop first, before you even attempt to code your own EAs.

I would suggest visiting the many tutorial sites out there that teach basic coding, for other languages, such a C, C++ or any other languages you feel comfortable with.

DanielsRUS
14
DanielsRUS  
Fernando Carreiro:

Yes, obviously it is possible. It is just simple math! However, like everything in life, you have to develop the skills in order to do it.

In this case, you either have someone else do it for you, but then you don't really learn to develop your skills; or you you sit down and work out step by step how to implement it in code.

Learning to code is not something you do in one weekend. You have to study and practice, and to become a pro, requires years of that.

Just think about the steps:

  1. You have to have another array where you keep track of a hit count or frequency of the elements being tested.
  2. As you run through you main array, you increment the count for that value in another array.
  3. In the end, you can evaluate the frequency array to find the highest counts, either by using a running comparison, or by first sorting it.

This is all basic coding skills, which you need to develop first, before you even attempt to code your own EAs.

I would suggest visiting the many tutorial sites out there that teach basic coding, for other languages, such a C, C++ or any other languages you feel comfortable with.

 Thank you. I had a feeling that this was more C++ oriented.
As of now i can only code EA to show me the most frequent number in an array.


   int a[5]={5,5,9,9,9};

   int max_count=0;

   for(int i=0;i<5;i++)
     {
      int count=1;
      for(int j=i+1;j<5;j++)
         if(a[i]==a[j])
            count++;
      if(count>max_count)
         max_count=count;
     }

   for(int i=0;i<5;i++)
     {
      int count=1;
      for(int j=i+1;j<5;j++)
         if(a[i]==a[j])
            count++;
      if(count==max_count)
         Print(a[i]); // the most frequent number is 9
     }
Fernando Carreiro
3893
Fernando Carreiro  
DanielsRUS: Thank you. I had a feeling that this was more C++ oriented. As of now i can only code EA to show me the most frequent number in an array.
No, you do not need OOP or C++ for this! It can useful to know OOP and C++, but not necessary for this!
nicholishen
1371
nicholishen  

As Fernando says OOP can be useful, in fact, very useful. This is a simple permutation of a collection-class called a "Set" where instead of a single data point (the number) you have two data points (the number and the count of number instances). In order implement this without OOP and the std library would be a very long and tedious process.


#property strict
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
class NumCnt : public CObject
{
public:
   int num,count;
   NumCnt(int number):num(number),count(1){}
   int Compare(const CObject *node,const int mode=0)const
   {
      NumCnt *other = (NumCnt*)node; 
      if(this.count > other.count)return -1;
      if(this.count < other.count)return 1;
      return 0;
   }
};
//+------------------------------------------------------------------+
class NumSet : public CArrayObj
{
public:
   NumCnt* operator[](const int i)const{return At(i);}
   bool Add(int number)
   {
      for(int i=0;i<Total();i++)
      {
         if(number==this[i].num)
         {
            this[i].count++;
            return true;
         }
      }
      return CArrayObj::Add(new NumCnt(number));
   }
};
//+------------------------------------------------------------------+
void OnStart()
{
   NumSet list;
   srand(GetTickCount());
   for(int i=0;i<100000;i++)
      list.Add((rand()%100)+1);
   list.Sort();
   
   string decending = "";
   for(int i=0;i<3;i++)
      decending+="list["+string(i)+"] = "+string(list[i].num)+" cnt= "+string(list[i].count)+" | ";
   Print("The top three numbers are: ",decending);
}
//+------------------------------------------------------------------+
DanielsRUS
14
DanielsRUS  
nicholishen:

As Fernando says OOP can be useful, in fact, very useful. This is a simple permutation of a collection-class called a "Set" where instead of a single data point (the number) you have two data points (the number and the count of number instances). In order implement this without OOP and the std library would be a very long and tedious process.


Wow, thank you so much! Really appreciate it :)

I have one question if I may:
It shows me this line each time I run the script:
"The top three numbers are: list[0] = 41 cnt= 1090 | list[1] = 4 cnt= 1061 | list[2] = 14 cnt= 1057 |"
"The top three numbers are: list[0] = 50 cnt= 1082 | list[1] = 44 cnt= 1059 | list[2] = 63 cnt= 1054 |"

Can you please elaborate from where dose the script
gets his numbers or where do I put in my parameters?

Thank you in advance!
Alain Verleyen
Moderator
34470
Alain Verleyen  
nicholishen:

As Fernando says OOP can be useful, in fact, very useful. This is a simple permutation of a collection-class called a "Set" where instead of a single data point (the number) you have two data points (the number and the count of number instances). In order implement this without OOP and the std library would be a very long and tedious process.


Of course OOP is useful.

What we (I agree with Fernando) are trying to say since a while (so not only on this topic) : you provided OOP code, the OP can use it, he is happy, but 99% of the time he doesn't understand it, can't change it, as you can see from his question. Isn't better to provide explanation and/or code that people can understand more easily and learn something...instead of just getting what they want "now".

To add comments, please log in or register