Download MetaTrader 5

OrdersTotal in MQL5

To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
Gopalakrishna R
416
Gopalakrishna R 2013.07.03 13:17 

Hi Friends,

          I am trying to port my EA from MQL4 to MQL5. I want to create an array having open prices of open trades at that moment. Codes and output of both MQL4 and 5 are given below.

In MQL5 if I add an order manually it’s been added to the previously opened orders of the same symbol and number of open trades remains 1. Have look on screenshots.  But I need the count as got in the MQL4. Please suggest me a method to do so.

 Note: From this array I am finding out the highest and lowest values.

MQL4

MQL5

int init()

  {

   ArrayResize(arr_open_prices,(OrdersTotal()));

     Print("OrdersTotal : ",OrdersTotal());

     for(i=0; i<OrdersTotal(); i++)

     {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         {

            Print("Order Type : ",OrderType());

            if(OrderType()== OP_BUY)

               {

                     arr_open_prices[i] = OrderOpenPrice();

               }

            else if(OrderType()==OP_SELL)

               {

               arr_open_prices[i] = OrderOpenPrice();

               //Print("s of ",i," = ",arr_open_prices[i]);

               }

         }

         else Print("Error : ",GetLastError());

      }

      for(i=0; i<OrdersTotal();i++) Print("b of ",i," = ",arr_open_prices[i]);

   return(0);

  }

void OnTick()

  {

   ArrayResize(arr_open_prices,OrdersTotal(),0);//OrdersTotal(),0);

     for(int i=0; i<OrdersTotal(); i++)

     {

         if(OrderGetTicket(i)>0)

         {

            if(OrderGetInteger(ORDER_TYPE)  == ORDER_TYPE_BUY)

               arr_open_prices[i] =  ORDER_PRICE_OPEN

            else if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL)

               arr_open_prices[i] = ORDER_PRICE_OPEN;

         }

     }

     Print("Position    :",PositionsTotal());

     Print("OrdersTotal : ",OrdersTotal());

     for(int j=0; j<OrdersTotal(); j++)

     {

         Print(j," : ",arr_open_prices[j]);

     }

  }

2013.07.03 13:47:05         testiing EURUSD,H1: b of 4 = 1.293

2013.07.03 13:47:05         testiing EURUSD,H1: b of 3 = 1.2932

2013.07.03 13:47:05         testiing EURUSD,H1: b of 2 = 1.2933

2013.07.03 13:47:05         testiing EURUSD,H1: b of 1 = 1.2937

2013.07.03 13:47:05         testiing EURUSD,H1: b of 0 = 1.2936 


2013.07.03 14:01:39         test (EURUSD,H1)            OrdersTotal : 1

 

Other values are 0.

 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Files:
Alain Verleyen
Moderator
30737
Alain Verleyen 2013.07.03 13:36  
krishna_gopal_2:

Hi Friends,

          I am trying to port my EA from MQL4 to MQL5. I want to create an array having open prices of open trades at that moment. Codes and output of both MQL4 and 5 are given below.

In MQL5 if I add an order manually it’s been added to the previously opened orders of the same symbol and number of open trades remains 1. Have look on screenshots.  But I need the count as got in the MQL4. Please suggest me a method to do so.

 Note: From this array I am finding out the highest and lowest values.

MQL4

MQL5

int init()

  {

   ArrayResize(arr_open_prices,(OrdersTotal()));

     Print("OrdersTotal : ",OrdersTotal());

     for(i=0; i<OrdersTotal(); i++)

     {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         {

            Print("Order Type : ",OrderType());

            if(OrderType()== OP_BUY)

               {

                     arr_open_prices[i] = OrderOpenPrice();

               }

            else if(OrderType()==OP_SELL)

               {

               arr_open_prices[i] = OrderOpenPrice();

               //Print("s of ",i," = ",arr_open_prices[i]);

               }

         }

         else Print("Error : ",GetLastError());

      }

      for(i=0; i<OrdersTotal();i++) Print("b of ",i," = ",arr_open_prices[i]);

   return(0);

  }

void OnTick()

  {

   ArrayResize(arr_open_prices,OrdersTotal(),0);//OrdersTotal(),0);

     for(int i=0; i<OrdersTotal(); i++)

     {

         if(OrderGetTicket(i)>0)

         {

            if(OrderGetInteger(ORDER_TYPE)  == ORDER_TYPE_BUY)

               arr_open_prices[i] =  ORDER_PRICE_OPEN

            else if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL)

               arr_open_prices[i] = ORDER_PRICE_OPEN;

         }

     }

     Print("Position    :",PositionsTotal());

     Print("OrdersTotal : ",OrdersTotal());

     for(int j=0; j<OrdersTotal(); j++)

     {

         Print(j," : ",arr_open_prices[j]);

     }

  }

2013.07.03 13:47:05         testiing EURUSD,H1: b of 4 = 1.293

2013.07.03 13:47:05         testiing EURUSD,H1: b of 3 = 1.2932

2013.07.03 13:47:05         testiing EURUSD,H1: b of 2 = 1.2933

2013.07.03 13:47:05         testiing EURUSD,H1: b of 1 = 1.2937

2013.07.03 13:47:05         testiing EURUSD,H1: b of 0 = 1.2936 


2013.07.03 14:01:39         test (EURUSD,H1)            OrdersTotal : 1

 

Other values are 0.

 

You have to understand how orders, positions and deals are working in MQL5.

For the code you provide, you have to use HistoryDealsTotal() and then select the appropriate deals (DEAL_TYPE & DEAL_ENTRY).

Gopalakrishna R
416
Gopalakrishna R 2013.07.03 14:23  
angevoyageur:

You have to understand how orders, positions and deals are working in MQL5.

For the code you provide, you have to use HistoryDealsTotal() and then select the appropriate deals (DEAL_TYPE & DEAL_ENTRY).

   I got it partially. But how to differentiate OPEN DEALS from CLOSED DEALS. In MQL4 we simply access order pool using MODE_TRADE or we can check order close time whether it is zero or not.

But how here? How to use DEAL_ENTRY? Can you please give me an example.

Alain Verleyen
Moderator
30737
Alain Verleyen 2013.07.03 14:45  
krishna_gopal_2:

   I got it partially. But how to differentiate OPEN DEALS from CLOSED DEALS. In MQL4 we simply access order pool using MODE_TRADE or we can check order close time whether it is zero or not.

But how here? How to use DEAL_ENTRY? Can you please give me an example.

https://www.mql5.com/en/articles/644
MQL5 Cookbook: The History of Deals And Function Library for Getting Position Properties
MQL5 Cookbook: The History of Deals And Function Library for Getting Position Properties
  • 2013.05.07
  • Anatoli Kazharski
  • www.mql5.com
It is time to briefly summarize the information provided in the previous articles on position properties. In this article, we will create a few additional functions to get the properties that can only be obtained after accessing the history of deals. We will also get familiar with data structures that will allow us to access position and symbol properties in a more convenient way.
Gopalakrishna R
416
Gopalakrishna R 2013.07.04 14:38  

  Useful article. But little bit confusing.

Can you please tell me Am I Right? If I'm wrong please explain it.

1. Placing a trade (BUY/SELL) is an individual ORDER and TP is also an individual order having different order reference numbers. Am I right?

2. If above statement is right there is no ORDER CLOSING time (Like the one in MQL4)

3. While having 'n' no of lots in Buying means,  If I place selling order with lot size 1, total lot size will be 'n-1' and I'm having no Sell positions only 'n-1' Buy positions. Am I right?

4.  To create the array as I asked earlier, What I have to do.

Alain Verleyen
Moderator
30737
Alain Verleyen 2013.07.04 14:59  
krishna_gopal_2:

  Useful article. But little bit confusing.

Can you please tell me Am I Right? If I'm wrong please explain it.

1. Placing a trade (BUY/SELL) is an individual ORDER and TP is also an individual order having different order reference numbers. Am I right?

2. If above statement is right there is no ORDER CLOSING time (Like the one in MQL4)

3. While having 'n' no of lots in Buying means,  If I place selling order with lot size 1, total lot size will be 'n-1' and I'm having no Sell positions only 'n-1' Buy positions. Am I right?

4.  To create the array as I asked earlier, What I have to do.

1. Yes and no, it depends how you place your TP.

2. Yes

3. Yes

4. Continue to learn.

Gopalakrishna R
416
Gopalakrishna R 2013.07.04 16:08  
angevoyageur:

1. Yes and no, it depends how you place your TP. 

   "Yes", if I am placing order with TP mentioned in it.

   "NO", if I am placing order without TP. 

    Am I right? 

angevoyageur:

4. Continue to learn.

    Just give me a clue, how to separate closed trades from Open trades.

Alain Verleyen
Moderator
30737
Alain Verleyen 2013.07.04 16:39  
krishna_gopal_2:

   "Yes", if I am placing order with TP mentioned in it.

   "NO", if I am placing order without TP. 

    Am I right? 

    Just give me a clue, how to separate closed trades from Open trades.

I already give you some clues. Show your attempt and you will receive help.
Gopalakrishna R
416
Gopalakrishna R 2013.07.04 18:13  
angevoyageur:
I already give you some clues. Show your attempt and you will receive help.

 

int OnInit()
  {

   int orders=OrdersTotal();
   int position = PositionsTotal();
   HistorySelect(0,TimeCurrent());
   int HisDeal = HistoryDealsTotal();
   Print("Orders   : ", orders);
   Print("Position : ", position);
   Print("HisDeal  : ",HisDeal);
//--- proceed all orders
   for(int i=1;i<HisDeal && position > 0; i++)
     {
      ResetLastError();
      HistorySelect(0,TimeCurrent());
      //--- copy the order data (by its index) into the cache
      ulong ticket=HistoryDealGetTicket(i);
      if(ticket!=0)// if the order has been successfully copied into the cache, proceed it
        {
         double price_open=HistoryDealGetDouble(ticket,DEAL_PRICE);
         datetime time_setup=HistoryDealGetInteger(ticket,DEAL_TIME);
         string symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         long Type=HistoryDealGetInteger(ticket,DEAL_ENTRY);
         long BorS=HistoryDealGetInteger(ticket, DEAL_TYPE);
         PrintFormat("i: %d, Order: #%d, Price: %f, Time: %s, Symbol: %s, Deal: %d, Type: %d",i,ticket,price_open,TimeToString(time_setup),symbol,Type,BorS);
        }
      else         // error in call of OrderGetTicket()
        {
         PrintFormat("i: %d, Error in loading of order data into the cache. Error code: %d",i,GetLastError());
        }
     }
   return(INIT_SUCCEEDED);
  }

2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 10, Order: #331779, Price: 1.292400, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 9, Order: #331778, Price: 1.292400, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 8, Order: #331777, Price: 1.292400, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 7, Order: #331776, Price: 1.292510, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 6, Order: #331775, Price: 1.292340, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 5, Order: #331774, Price: 1.292350, Time: 2013.07.04 15:28, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 4, Order: #331773, Price: 1.292120, Time: 2013.07.04 15:27, Symbol: EURUSD, Deal: 1, Type: 1
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 3, Order: #331772, Price: 1.292070, Time: 2013.07.04 15:26, Symbol: EURUSD, Deal: 1, Type: 1
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 2, Order: #331771, Price: 1.292390, Time: 2013.07.04 15:26, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  i: 1, Order: #331770, Price: 1.292000, Time: 2013.07.04 15:25, Symbol: EURUSD, Deal: 0, Type: 0
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  HisDeal  : 11
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  Position : 1
2013.07.04 21:40:50     Demo_OrderGetTicketByIndex (EURUSD,H1)  Orders   : 1

 So far displayed correct parameters. Which one will decide whether the order is OPEN or not. 

Alain Verleyen
Moderator
30737
Alain Verleyen 2013.07.04 19:26  
krishna_gopal_2:

 

 So far displayed correct parameters. Which one will decide whether the order is OPEN or not. 

You have to check DEAL_ENTRY which can be DEAL_ENTRY_IN, DEAL_ENTRY_OUT which corresponds almost to Open and Close.

Don't mix deals and orders.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   int orders=OrdersTotal();
   int position=PositionsTotal();
   HistorySelect(0,TimeCurrent());
   int HisDeal=HistoryDealsTotal();
   Print("Orders   : ",orders);
   Print("Position : ",position);
   Print("HisDeal  : ",HisDeal);
//--- proceed all orders
   for(int i=1;i<HisDeal && position>0; i++)            // You miss the first deal (i=0), intentionally ?
     {
      ResetLastError();
//      HistorySelect(0,TimeCurrent());                 // No need to select history on each iteration
      //--- copy the order data (by its index) into the cache
      ulong ticket=HistoryDealGetTicket(i);
      if(ticket!=0)// if the order DEAL has been successfully copied into the cache, proceed it
        {
         double price_open=HistoryDealGetDouble(ticket,DEAL_PRICE);
         datetime time_setup=HistoryDealGetInteger(ticket,DEAL_TIME);
         string symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         ENUM_DEAL_ENTRY Type=HistoryDealGetInteger(ticket,DEAL_ENTRY);
         ENUM_DEAL_TYPE BorS=HistoryDealGetInteger(ticket, DEAL_TYPE);
         PrintFormat("i: %d, Order: #%d, Price: %f, Time: %s, Symbol: %s, Deal: %s, Type: %s",
                      i,ticket,price_open,TimeToString(time_setup),symbol,EnumToString(Type),EnumToString(BorS));
        }
      else         // error in call of OrderGetTicket()
        {
         PrintFormat("i: %d, Error in loading of order data into the cache. Error code: %d",i,GetLastError());
        }
     }

  }
//+------------------------------------------------------------------+
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties - Documentation on MQL5
Gopalakrishna R
416
Gopalakrishna R 2013.07.05 08:05  
angevoyageur:


// You miss the first deal (i=0), intentionally ? 

First Deal is "Balance". That's why I neglected it. Is it wrong?

angevoyageur:

You have to check DEAL_ENTRY which can be DEAL_ENTRY_IN, DEAL_ENTRY_OUT which corresponds almost to Open and Close.


Yes. I checked the DEAL_ENTRY and found IN and OUT and the TYPE too.

Where I am struggling is using these parameters how could I find which entry is closed which is not?

and What is the OPEN_PRICE of latest entry (which is going to act as a pointer)?

and If the latest pointer-entry is CLOSED (profit booked) next very latest-open-entry should be the pointer-entry.

To add comments, please log in or register