How to make a function to know the ticket number

 
Welcome, please help
I want a function that shows me the ticket number
For profitable deals, the order of profit from largest to smallest
And the same idea in losing deals, from bigger to smaller

like this picture


 

You could put the profit and ticket number into an array and then sort? This would sort by profit ascending 

double tickets[1,2];
ArrayResize(tickets,OrdersHistoryTotal()-1);
int ct=0;
 for(int i=0;i<OrdersHistoryTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderType()!=6){
    tickets[ct,0]=OrderProfit();
    tickets[ct,1]=OrderTicket();
    ct++;
 }
 ArraySort(tickets,0,0,MODE_ASCEND);
 
ArrayResize(tickets,OrdersHistoryTotal()-1);
Do not assume history has only closed orders.
 
Hello
Thanks for help
When experimenting the expert does not work
You can review the code and experiment
I only want open deals
Ranking from largest to smallest profit
And the loss from the largest to the smallest arrangement
Thank you
Expert in attachments
Files:
arman_EA.mq4  19 kb
 
Samir Arman:
Hello
Thanks for help
When experimenting the expert does not work
You can review the code and experiment
I only want open deals
Ranking from largest to smallest profit
And the loss from the largest to the smallest arrangement
Thank you
Expert in attachments

Hi Samir.

I took the term "profit deals" to mean closed orders; after all, there's no profit - or loss - until the order is closed...;)

You'd get an error, because the array size in OrdersHistoryTotal() is one less than total orders in History, because of the "Balance" entry. Not the case in OrdersTotal().

ArrayResize(tickets,OrdersTotal()-1);

Also, you should filter out any pending orders from the array using OrderType()..

 
William Roeder:
Do not assume history has only closed orders.
What do you mean ? That history could have open orders ? I don't get it.
 
Alain Verleyen: What do you mean ? That history could have open orders ? I don't get it.
Not open orders. Closed orders. Deleted pending orders. Balance adjustments and credits.
enum Trade_Operation{            // Same as OP_xxx except a proper enumeration.
      TO_BUY, TO_SELL, TO_BUYLIMIT, TO_SELLLIMIT, TO_BUYSTOP, TO_SELLSTOP,
      TO_BALANCE,                         ///< (6) May occur in history pool.
      TO_CREDIT};                         ///< (7) May occur in history pool.
 
William Roeder:
Not open orders. Closed orders. Deleted pending orders. Balance adjustments and credits.
Ah ok, of course. Thanks.
Reason: