Issue counting closed trades

 
I am having an issue trying to count closed trades , i just can seem to get it done . 
int ClosedOdersCount = 0;
datetime TimeNow = TimeCurrent(); 
datetime TimeBefore = TimeCurrent() - 860000;
long OrderType ;  
  
  HistorySelect(TimeBefore,TimeNow);
  if(HistoryOrdersTotal() > 0){
    for(int i=HistoryOrdersTotal()-1; i>=0 ;i--){
    
       ulong ticket = HistoryOrderGetTicket(i);
      if (ticket>0) {
       if ( HistoryOrderGetTicket(ticket) ){
         Print("ticket: ", ticket); 

        HistoryOrderGetInteger(ticket, ORDER_TYPE,OrderType);
        Print("i = ", i, ", ticket = ", ticket, ", OrderType = ", OrderType );
          ClosedOdersCount ++;     

            }  
         }
        }
       

     Print("Today's closed trades count: ", ClosedOdersCount);  
   }
It just doesnt count the closed trades correctly 
 
Mihlali Linge Dlulane:
I am having an issue trying to count closed trades , i just can seem to get it done . 
It just doesnt count the closed trades correctly 

You need to check deals, not orders.

You should search before wasting your time on something you don't master, such code is available on this site.

 
Alain Verleyen #:

You need to check deals, not orders.

You should search before wasting your time on something you don't master, such code is available on this site.

I have searched the site but didn't find anything that i could actually use 

 

Forum on trading, automated trading systems and testing trading strategies

I don't understand how to distinguish between position and order.

Fernando Carreiro, 2023.06.18 18:48

A simple way to view it ...

  • Order — the request (current and history)
  • Deal — the action (history)
  • Position — the result (current)

Read the following article ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Alain Verleyen #:

You need to check deals, not orders.

You should search before wasting your time on something you don't master, such code is available on this site.

Ive changed it from checking deals not orders and it still doesnt seem to work 

int ClosedOdersCount = 0;
datetime TimeNow = TimeCurrent(); 
datetime TimeBefore = TimeCurrent() - 860000;
long OrderType ;  
  
  HistorySelect(TimeBefore,TimeNow);
  if(HistoryDealsTotal() > 0){
    for(int i=HistoryDealsTotal()-1; i>=0 ;i--){
    
       ulong ticket = HistoryDealGetTicket(i);
      if (ticket>0) {
       if ( HistoryDealGetTicket(ticket) ){
         Print("ticket: ", ticket); 

        HistoryDealGetInteger(ticket, DEAL_ORDER,OrderType);
        Print("i = ", i, ", ticket = ", ticket, ", OrderType = ", OrderType );
          ClosedOdersCount ++;     

            }  
         }
        }
       

     Print("Today's closed trades count: ", ClosedOdersCount);  
   }
 
Mihlali Linge Dlulane #:

Ive changed it from checking deals not orders and it still doesnt seem to work 

You don't need to use HistoryDealGetTicket() 2 times.

Where is the code to check it's a "out" deal (closing) ?