How to track Added and removed Orders or Position in an EA

 

Hello folks, 

Seeking your help, I am trying to build a simple function in one of my EA to have a comprehensive list of orders pending execution and current running positions.

One solution is to create a list where every 5 seconds, I scan orders and positions opened by ma ea, store them in a temporary list, and after that compare both the old and the new list to create one unique list. But it is performance consuming... 

But I am trying to do this without a looping mechanism, I tried to understand the ontradetransaction and ontrade function but there are a limited number of resources talking about.

The idea is: when an order is opened and added to the list, same for the position, and when they are canceled or closed, remove them from the list.

Anyone tackled this point, can you guide me toward resources were I can read and understand better how to build this in my EA ? 

https://www.mql5.com/en/docs/event_handlers/ontradetransaction

https://www.mql5.com/en/docs/event_handlers/ontrade

]Thanks in advance.

Documentation on MQL5: Event Handling / OnTradeTransaction
Documentation on MQL5: Event Handling / OnTradeTransaction
  • www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
What is the problem with a loop? Your EA need to account for a restart for whatever reason.
 
Haidar Lionel Haj Ali: when an order is opened and added to the list, same for the position, and when they are canceled or closed, remove them from the list.
  1. The list already exists, in the terminal. Use OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), with Magic number/symbol filtering.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)


  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

 

Thanks guys, you both mentioned the same point "EA recovery", and this will oblige me to go with the loop to retrieve orders as mentioned by @William Roeder so I will drop my idea to retrieve the added/deleted trades from OnTrade and OnTradeTransaction and implement a simple loop to read from Orders and Positions when onTradeTransaction happens to limit the number of time I will call the list update loop. 

Thanks





 
Haidar Lionel Haj Ali #:

Thanks guys, you both mentioned the same point "EA recovery", and this will oblige me to go with the loop to retrieve orders as mentioned by @William Roeder so I will drop my idea to retrieve the added/deleted trades from OnTrade and OnTradeTransaction and implement a simple loop to read from Orders and Positions when onTradeTransaction happens to limit the number of time I will call the list update loop. 

Thanks





You want to do it on every tick, OnTrade  or onTradeTransaction does not get called on a restart. Performance wise it is not a big deal, literally talking microseconds on mediocre hardware with 100+ positions. Just don't compare strings (== Symbol), they are slow.

 

Hello, 

I still have some problems retrieving the orders and positions. I am using mql5, Here is what I did: 

1- I created a function that gets me back the count of orders and updates an array with the order tickets:

GetArrayOrders(string symbol,ulong &TicketArray[])
{
   int ticketCount = Count(symbol);
      if(ticketCount == 0)
     {
       return ticketCount;
     } else
         {
            ArrayResize(TicketArray,ticketCount);
            int count=OrdersTotal();
            int j = ticketCount;
            for(int i=count-1; i>=0; i--)
            {
            if(OrderGetTicket(i) > 0)
                if(COrderInfo::Symbol()==symbol) 
                     {
                        TicketArray[j-1] = COrderInfo::Ticket();
                        j = j - 1;
                     }    
            }            
         }
    return ticketCount;
}

2- I created a similar function for the positions: 

GetArrayPositions(string symbol,ulong &TicketArray[])
{
   int ticketCount = Count(symbol);
      if(ticketCount == 0)
     {
         return ticketCount;
     } else
         {
            ArrayResize(TicketArray,ticketCount);
            int count=PositionsTotal();
            int j = ticketCount;
            for(int i=count-1; i>=0; i--)
            {
            if(PositionGetTicket(i) > 0)
                if(CPositionInfo::Symbol()==symbol) 
                     {
                        TicketArray[j-1] = CPositionInfo::Ticket();
                        j = j - 1;
                     }    
            }            
        	}
    return ticketCount;

3- OnTrade() I am executing the following code:

UpdateTradesList()
   {
      ulong orderlist[];
      ulong positionlist[];
      
      ulong activetickets[];
      
      int ordercount = GetArrayOrders(Symbol(),orderlist);
      int positioncount = GetArrayPositions(Symbol(),positionlist);
      
      int activeticketsCount;
      
      if(mOrderCount != ordercount || mPositionCount != positioncount )
         {
            mOrderCount = ordercount;
            mPositionCount = positioncount;
            activeticketsCount = mOrderCount + mPositionCount;
            
            ArrayResize(activetickets,activeticketsCount);
            ArrayCopy(activetickets,orderlist,0,0,WHOLE_ARRAY);
            ArrayCopy(activetickets,positionlist,ordercount-1,0,WHOLE_ARRAY);
            
                  Print("++++++++++++++++++++++++++");            
            for(int i=0;i<activeticketsCount;i++)
              {
                  Print(activetickets[i]);
              }
                  Print("---------------------------");   
         }
   }

The problem is I have weird information coming out of the print, example below is the print and the a screen shot of the current position and orders.

When  I doing market orders (sell, buy) the list is getting well updated, once I start to add pending orders the list start to show weird numbers specifically for orders. 

I there something wrong with my approach ? 

Thanks in advance.

 
Haidar Lionel Haj Ali #:

Hello, 

I still have some problems retrieving the orders and positions. I am using mql5, Here is what I did: 

1- I created a function that gets me back the count of orders and updates an array with the order tickets:

2- I created a similar function for the positions: 

3- OnTrade() I am executing the following code:

The problem is I have weird information coming out of the print, example below is the print and the a screen shot of the current position and orders.

When  I doing market orders (sell, buy) the list is getting well updated, once I start to add pending orders the list start to show weird numbers specifically for orders. 

I there something wrong with my approach ? 

Thanks in advance.

Found the solution, but not sire why first code is not working properly. In Order listing I did the following modification:

TicketArray[j-1] = /*COrderInfo::Ticket()*/ OrderGetTicket(i);
Reason: