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!!
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:
In C language it is quite simple:
Play videoPlease edit your post.
For large amounts of code, attach it.
- Why not?
- 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 - 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
thanks to all !
I will use quicksort !
MarcoNeri: I will use quicksort ! |
What quicksort? Alphabetic Index of MQL4 Functions (600+) - MQL4 forum |
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
- What part of using SRC was unclear?
- Unless you have more than 16 orders, the Insertion sort will be faster.
Sorry for using of SRC :
class Dummy
{
};
Now I get it. :-D
Thank you again.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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:
Any tips ?
Thank you