Move OrderHistory to multidimensional Array

 

Hi, Can you give some advice here... The preliminary goal is to extract pairs of ticketnumber & orderprofit from history, and copy them to multidimensional array for sorting ascending by ticket.

This I think is the most reliable way to 'get hold of' the last N orders for further investigation.

//-----------------------------------------------------------
int BalanceCalc()
   {//Throttle down the lots if the Balance graph is pointing the wrong way!
   //int MAper = 16;//External filter
   static int Htotal;     
   if(OrdersHistoryTotal() == Htotal) return(0);//for speedup         
   Htotal =OrdersHistoryTotal();//Got one more since last visit here
   int Bdir;
   double History[][];//Each order's Ticket and Profit
   
   //Copy all orderhistory to array, then sort array by ticketnumber (highest ticket is last order):
   for(int Order = OrdersHistoryTotal() - 1; Order >= 0; Order--)
      {
      OrderSelect(Order, SELECT_BY_POS, MODE_HISTORY);
         {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            {
            History[Order] = {OrderTicket(), OrderProfit()};//uh...?

            }
         }
      }
      
   //Sort array by 1st dimension (Ticketnumber) here:
   //ArraySort(History,WHOLE_ARRAY,0,MODE_DESCEND);

   return(Bdir);
   }
//-----------------------------------------------------------   
 
  1. double History[][];//Each order's Ticket and Profit
    Must specify the second dimention and use ArrayResize for the first.
  2. History[Order] = {OrderTicket(), OrderProfit()};//uh...?
    maybe
    #define TICKET 0
    #define PROFIT 1
    #define HISTORY_MAX 2
    double History[][HISTORY_MAX];
    :
    ArrayResize(History, Order+1);
    History[Order][TICKET] = OrderTicket();
    History[Order][PROFIT] = OrderProfit();
    Order++;
  3. You are already reading the orders from newest to oldest and have all the information available at that time. Why sort by ticket? Why store in an array at all?
 

Thank I check this out.

Well the idea is to grab eg. the last 20 orders from history, and be relatively sure that those are chronologically the last 20 orders.

Otherwise if the user mouse clicks and sorts the history everything gets messed up?

Also the highest ticket number is ALWAYS the last order in history (if 'All History' is set) right ?

 

I don't beleave that the order in the GUI has any affect to the orderSelect loop. But some are paranoid.

Why not just create an array of ticket numbers, sort them. Then do an OrderSelect(ticket[i],SELECT_BY_TICKET)

I think the history list will be sorted by close time not by ticket number - matters only if you open multiple orders. In that case you must impliment the sort yourself

untested

int ticket[];   datetime OCT[];     int count=0;
for(int iPos=OrdersHistoryTotal()-1; iPos >= 0 ; iPos--) if (
    OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)      // Only orders w/
&&  OrderMagicNumber() == Magic.Number                  // my magic number
&&  OrderSymbol()      == chart.symbol                  // and my pair.
&&  OrderType()        <= OP_SELL// Avoid cr/bal forum.mql4.com/32363#325360
){
    datetime newOCT = OrderCloseTime();
    ArrayResize(ticket,     count+1);
    ArrayResize(OOT,        count+1);
    for (int idx=count; idx>0; idx--){          // Insertion sort.
        if (newOCT >= OOT[idx-1])   break;
        ticket[idx] = ticket[idx-1];    OCT[idx] = OCT[idx-1];
    }
    ticket[count] = OrderTicket();  
       OCT[count] = newOCT; count++;           // Insert.
}
// Loop by OrderCloseTime() Decending
for (idx = count-1; idx >= 0; idx--){
    if (!OrderSelect(ticket[idx], SELECT_BY_TICKET)) Alert(...);
    Print("ticket ",OrderTicket(), " closed on ", TimeToStr(OrderCloseTime()),
          "at ", PriceToStr(OrderClosePrice()));
}
Reason: