Download MetaTrader 5

How to get last order in trading history - mql5

To add comments, please log in or register
enur
15
enur  

I am trying to write an expert advicer but I am stuck with a simple problem:

How do I get the type ( buy or sell) and price for the last executed order in the trading history?

Thanks in advance.

Jose Miguel Soriano
5003
Jose Miguel Soriano  
enur:

I am trying to write an expert advicer but I am stuck with a simple problem:

How do I get the type ( buy or sell) and price for the last executed order in the trading history?

Thanks in advance.

You can read https://www.mql5.com/en/articles/211
Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
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.
Jose Miguel Soriano
5003
Jose Miguel Soriano  
enur:

I am trying to write an expert advicer but I am stuck with a simple problem:

How do I get the type ( buy or sell) and price for the last executed order in the trading history?

Thanks in advance.

Examples...

Best regards form Spain. 

enur
15
enur  
Thank you for the examples. I will look into it.
enur
15
enur  

Hello

I have been trying to obtain the price for last trade without luck. I can get other properties but the price and volume returns either nothing or a big number with a #.

I get this when testing in Strategy tester:

2014.03.02 23:49:59   Deal: #193 opened by order: #270 with ORDER_MAGIC: 312 was in position: #269 price: #-448050988 volume:


Any suggestions?



double last_deal()
  {
  double deal_price;
// --- time interval of the trade history needed
   datetime end=TimeCurrent();                 // current server time
   datetime start=end-PeriodSeconds(PERIOD_D1);// decrease 1 day
//--- request of trade history needed into the cache of MQL5 program
   HistorySelect(start,end);
//--- get total number of deals in the history
   int deals=HistoryDealsTotal();
//--- get ticket of the deal with the last index in the list
   ulong deal_ticket=HistoryDealGetTicket(deals-1);
   if(deal_ticket>0) // deal has been selected, let's proceed ot
     {
      //--- ticket of the order, opened the deal
      ulong order=HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
      long order_magic=HistoryDealGetInteger(deal_ticket,DEAL_MAGIC);
      long pos_ID=HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
           deal_price=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
           double deal_volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
      PrintFormat("Deal: #%d opened by order: #%d with ORDER_MAGIC: %d was in position: #%d price: #%d volume:",
                  deals-1,order,order_magic,pos_ID,deal_price,deal_volume);

     }
   else              // error in selecting of the deal
     {
      PrintFormat("Total number of deals %d, error in selection of the deal"+
                  " with index %d. Error %d",deals,deals-1,GetLastError());
     }
   return(deal_price);
  }
Rogerio Figurelli
Moderator
41907
Rogerio Figurelli  
enur:

Hello

I have been trying to obtain the price for last trade without luck. I can get other properties but the price and volume returns either nothing or a big number with a #.

I get this when testing in Strategy tester:

2014.03.02 23:49:59   Deal: #193 opened by order: #270 with ORDER_MAGIC: 312 was in position: #269 price: #-448050988 volume:


Any suggestions?



Hi, take a look at your PrintFormat string, you forgot the volume type and is using an integer as precision type for the price. Try something like ...

"Deal: #%d opened by order: #%d with ORDER_MAGIC: %d was in position: #%d price: #%f volume: #%f"
enur
15
enur  
Thank you, my expert works well now.
To add comments, please log in or register