Best way to keep a list of orders ordered by profit?

 

Hi there, in one of my EAs I'm trying to keep a grid with hedged positions and I want to keep two lists with longs and shorts ordered by current profit. Iterating through MT4 pool of orders/trades gives me the orders ordered by creation order, and as far as I know there is no way to get them ordered by profit. At the moment I'm implementing a generic list, but I'm facing two options:

  1. load all the trades on each tick and at the end of the tick process dispose the list.
  2. keep the list across the ticks and update it with active orders, removing orders that are not active anymore, which involves a bit more of coding.

With C++ using the standard library is quite straight forward to sort lists, but I don't want to jump into the complexity of building a DLL for MT4, at least not yet.

Any other suggestion? Thanks in advance.

 

Hi,

you can use a two dimensional array, first dimension the order profit and second the ticket number, and use ArraySort() to sort it: https://docs.mql4.com/array/arraysort

Regards. 

 
xiruko:

Hi,

you can use a two dimensional array, first dimension the order profit and second the ticket number, and use ArraySort() to sort it: https://docs.mql4.com/array/arraysort

Regards. 

 

Mate you've saved me a lot of work. I'll try it this evening after work but it looks like you hit the nail.

Let me ask another thing in terms of performance. In order to finally have the orders in an array with the correct dimension, which option would you go for?

  1. Would you run twice the OrderTotal() loop? One to count orders for the Array dimension and another one to populate the newly created array (with the right size) with the orders.
  2. Would you create a big Array, populate it with the orders and then resize it to the number of orders added.
I think I'll go for #1, as I suspect that ArrayResize might have a bit of overhead.

Thanks in advance! 

 
fonx: I think I'll go for #1, as I suspect that ArrayResize might have a bit of overhead.
Use the third argument.
double[][2] A;  int n=0;
for(...) if(...){ 
   ArrayResize(A, n+1, 100); 
   A[n][0]=...; 
   ++n;
}
 
WHRoeder:
Thanks a lot, I should have read the documentation in depth. Very clever the initial allocation of memory.
Reason: