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

Back to topics list To post a new topic, please log in or register
avatar
22
fonx 2015.05.19 00:33
 

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.

avatar
16
xiruko 2015.05.19 02:01 #
 

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. 

 

 

avatar
22
fonx 2015.05.19 08:54 #
 
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! 

avatar
13009
WHRoeder 2015.05.19 17:52 #
 
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;
}
avatar
22
fonx 2015.05.19 19:07 #
 
WHRoeder:
Thanks a lot, I should have read the documentation in depth. Very clever the initial allocation of memory.
Back to topics list  

To add comments, please log in or register