Download MetaTrader 5

Count opened orders of a day (without all those partial closes)

To add comments, please log in or register
Jagg
106
Jagg  

Hi,

I want to get the number of trades from a day I've done so I loop through OrdersHistoryTotal. Problem now is I often do partial closes of an order so the total number of trades I get now from my code is way too high because of all those partial closed trades which are present in History Tab.

Example:

- buy 0.5 lots EURUSD 
- partial close/sell with 0.1 lot
- another partial close/sell with 0.2 lots 
- trade exit with reminder 0.2 lot

^^^This get counted as 3 trades. I want to get this as 1 trade counted.

Is it ok to rely on that "from #...."/"to #...." OrderComment the broker do? So count all orders which have not "to #" in the comment gives me my total opened trades (without all those partial closes)?

 if (StringFind(OrderComment(),"to #", 0) == -1)
 {
   OrdersTotalWithThatMagicNumber++;
 }

Or other there better ways to do it (because relying on that order comment is vague afaik because every broker can do their own thing here)?

whroeder1
15019
whroeder1  
  1. Sort the trades by OrderOpenTime. Sum the ones that have the same time.
  2. Not a good idea, brokers can change comments, including complete replacement.
Jagg
106
Jagg  

Thanks, done this (and looks ok)...

 

datetime OrdersSortByOrderOpenTime[]; //Array to store trade open times
dt = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));
int total = GetTradesTotalOfThatDay("", -1, MagicNumber, dt);

int GetTradesTotalOfThatDay(string sy = "", int op = -1, int mn = -1, datetime dt = 0)
{
  int OrdersTotalWithThatMagicNumber = 0;
  ArrayResize(OrdersSortByOrderOpenTime, 0); //reset array
  int j = 0;
  if (sy == "0") sy = Symbol();

  for (int i = OrdersHistoryTotal()-1; i >=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
    {
      if ((OrderSymbol() == sy || sy == "") && (op < 0 || OrderType() == op))
      {
        if (OrderType() == OP_BUY || OrderType() == OP_SELL)
        {
          if (mn < 0 || OrderMagicNumber() == mn)
          {
            if (dt < OrderCloseTime())
            {
              ArrayResize(OrdersSortByOrderOpenTime, j+1);
              OrdersSortByOrderOpenTime[j] = OrderOpenTime();
              j++;
            }
          }
        }
      }
    }
  } // for (int i = OrdersHistoryTotal()-1; i >=0; i--)
  
   datetime OldOpenTime;
   //Sort array
   int size_OrdersSortByOrderOpenTime = ArraySize(OrdersSortByOrderOpenTime);

   if (size_OrdersSortByOrderOpenTime > 0) {
      ArraySort(OrdersSortByOrderOpenTime, WHOLE_ARRAY, 0, MODE_ASCEND);
      
      for (int cc = 0; cc <= size_OrdersSortByOrderOpenTime-1; cc++)
      {
         if(OldOpenTime != OrdersSortByOrderOpenTime[cc]) {
            OldOpenTime = OrdersSortByOrderOpenTime[cc];
            OrdersTotalWithThatMagicNumber++;
         }
      }//for (int cc = 0; cc <= size_OrdersSortByOrderOpenTime-1; cc++)

   } // if (ArraySize(OrdersSortByOrderOpenTime) > 0) {
   
  return (OrdersTotalWithThatMagicNumber);
}
whroeder1
15019
whroeder1  
Uninitialized variable is random value. Set to 0 as no open order has that time.
datetime OldOpenTime;
You just wanted the count, but if you also needed the tickets then make it a 2D array
double OrdersSortByOrderOpenTime[][2]; //Array to store trade open times
:
              OrdersSortByOrderOpenTime[j][0] = OrderOpenTime();
              OrdersSortByOrderOpenTime[j][1] = OrderTicket();
:
if(OrdersSortByOrderOpenTime[i][0] == OrdersSortByOrderOpenTime[i+1][0])
  PrintFormat("Tickets %i and %i were once one",
              OrdersSortByOrderOpenTime[i][1],
              OrdersSortByOrderOpenTime[i+1][1]);
  
N extra unnecessary subtractions.
for (int cc = 0; cc <= size_OrdersSortByOrderOpenTime-1; cc++)
Use the C idiom index < count
for (int cc = 0; cc < size_OrdersSortByOrderOpenTime; cc++)
Jagg
106
Jagg  
Changed your mentioned parts... thanks again WHRoeder.
To add comments, please log in or register