Download MetaTrader 5
To add comments, please log in or register
You can test an application before buying it. Find out how to do that!
Marco Neri
16
Marco Neri 2014.10.03 09:29 

Hi,

I have an array of Pointers like  

Order* m_orders[]

and there is a problem:

 Order is a class containg informations about TP, SL, open time and so on.

I want to sort this array by open time in a function but mql(4) accepts only arrays by reference as parameter so I can't do that.

In C language it is quite simple:

void Sort(Order *pointer, int size)
{
    Order *i, *j, temp;

    for(i = pointer; i < pointer + size; i++)
   {
        for(j = i + 1; j < pointer + size; j++)
        {
            if((*j).OrderDate < (*i).OrderDate)
            {
                temp = *j;
                *j = *i;
                *i = temp;
            }
        }
    }
}

 

 Any tips ?

Thank you 

Carl Schreiber
5962
Carl Schreiber 2014.10.03 09:51  

What about using struct and an array of structs?

struct _Orders {
    datetime OrderDate;
    double SL,TG,..;
    int Ticket;
    ...
};
_Orders ArrOrders[];
...
ArrayResize(ArrOrders,5);
...
void sort(_Orders& arr) {
    ...
    if (arr[x].OrderDate < arr[y].OrderDate ) { ... }
}
// NOT TESTED!!
whroeder1
13644
whroeder1 2014.10.03 12:04  
MarcoNeri: I want to sort this array by open time in a function but mql(4) accepts only arrays by reference as parameter so I can't do that.
In C language it is quite simple:
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Why not?
  3. It's just as simple in mt4. Even easier and much more efficient if you use an insertion sort. (Never use bubble sort - ever.)
    Not tested
    class Order{
     public: 
       datetime OrderDate(void){return 0;}
    };
    //////////////////////////////////////
    void Sort(Order* &Array[], int size){
       for(int i=1; i < size; ++i){
          Order*   temp = Array[i];
          datetime od   = temp.OrderDate();
          while(i>0 && od < Array[i-1].OrderDate() ) Array[i] = Array[--i];
          Array[i] = temp;
       }
    }
    Not tested
  4. A generic approach is to templatize it and provide a comparator.
    Not compiled, not tested
    class OrderByDateAscending{
     public:
       bool IsLess(const Order& LHS, const Order& RHS){
          return LHS.OrderDate() < RHS.OrderDate();
       }
    }
    //////////////////////////////////////
    template <typename C, typename BinaryPredicate>
    void Sort(C* &Array[], int size, BinaryPredicate& comp){
       for(int i=1; i < size; ++i){
          C* temp = Array[i];
          while(i>0 && comp.IsLess(temp, Array[i-1]) ) Array[i] = Array[--i];
          Array[i] = temp;
       }
    }
    Not compiled, not tested
Marco Neri
16
Marco Neri 2014.10.03 13:10  

thanks to all !

I will use quicksort !

whroeder1
13644
whroeder1 2014.10.03 14:40  
MarcoNeri: I will use quicksort !
What quicksort? Alphabetic Index of MQL4 Functions (600+) - MQL4 forum
Marco Neri
16
Marco Neri 2014.10.03 14:54  
WHRoeder:
MarcoNeri: I will use quicksort !
What quicksort? Alphabetic Index of MQL4 Functions (600+) - MQL4 forum

This one:

 Not Tested

void QuickSort(Order* &arr[], uint left, uint right)
{

  uint i = left, j = right;
  Order* tmp;
  Order* pivot = arr[MathAbs((left + right) / 2)];

  /* partition */

  while (i <= j) {
        while (arr[i].GetOrderOpenTime() < pivot.GetOrderOpenTime())
              i++;
        while (arr[j].GetOrderOpenTime() > pivot.GetOrderOpenTime())
              j--;

        if (i <= j) {
              tmp = arr[i];
              arr[i] = arr[j];
              arr[j] = tmp;
              i++;
              j--;
        }
  }

  /* recursion */

  if (left < j)
     QuickSort(arr, left, j);

  if (i< right)
     QuickSort(arr, i, right);

}

 

 Not tested

whroeder1
13644
whroeder1 2014.10.03 19:46  
  1. What part of using SRC was unclear?
  2. Unless you have more than 16 orders, the Insertion sort will be faster.
Marco Neri
16
Marco Neri 2014.10.03 22:36  

Sorry for using of SRC :

 

class Dummy
{
};

 Now I get it. :-D 

Thank you again. 

/
To add comments, please log in or register