Forex Forum - EarnForex
Serving Traders Since 2005
 

Go Back   Forex Forum - EarnForex > MetaTrader > MetaTrader 5

MetaTrader 5 MT5 is already out of its beta stage, but the development is ongoing. Some brokers already offer real accounts and the interest of traders is growing. You can discuss anything related to MetaTrader 5 in this forum.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27th September 2011, 23:57
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mode

Hi

I searched the docs. for no avail. Is there a function which returns the mode, i.e. the value that occurs most frequently in an array?

thx
Reply With Quote
  #2 (permalink)  
Old 28th September 2011, 10:06
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

I don't know such function, but it can be coded:

MQL5 Code:
// Returns the value of the mode of the array a
//  a - array
//  n - size
int mode(const int &a[], uint n)
{
    uint frequency[];
    int index[];
    bool found;
    uint k = 0; // Index for index array
   
    ArrayResize(frequency, n);
    ArrayResize(index, n);
    ArrayInitialize(frequency, 0);
    ArrayInitialize(index, 0);
    // Cycle through initial array
    for (uint i = 0; i < n; i++)
    {
        // Find the current element in the index array
        found = false;
        for (uint j = 0; j <= i; j++)
        {
            if (index[j] == a[i])
            {
                frequency[j]++;
                found = true;
                break;
            }
        }
        // If the element wasn't found in the index array, add it to the index array
        if (!found)
        {
            index[k] = a[i];
            frequency[k] = 1;
            k++;
        }
    }
   
    return index[ArrayMaximum(frequency)];
}

Probably not the optimal way of doing it, but it works. Please let me know if you find a bug or if you find a better solution.
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
  #3 (permalink)  
Old 28th September 2011, 13:58
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thx for that Enivid.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:21.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Inactive Reminders By Icora Web Design

SEO by vBSEO 3.3.2