How to get last order in trading history - mql5

 

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.

 
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.
 
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. 

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

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);
  }
 
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"
 
Thank you, my expert works well now.
 

How would you modify the function to get

1) LastLongEntryPrice()
2) LastShortEntryPrice()

i.e.

I opened 3 sell orders and then 5 buy orders after.
LastShortEntryPrice is 6th deals ago and not the last deal.

How could I modify this code to make it also scan through the "deal type" to return the "last of its kind"?

 
Chee Chua:

How would you modify the function to get

1) LastLongEntryPrice()
2) LastShortEntryPrice()

i.e.

I opened 3 sell orders and then 5 buy orders after.
LastShortEntryPrice is 6th deals ago and not the last deal.

How could I modify this code to make it also scan through the "deal type" to return the "last of its kind"?

You need to loop over the selection from back to front and remember the first entry that matches the wanted direction:

double last_price_sell, last_price_buy;
bool buy_found=false;
bool sell_found=false;

for(int deal=HistoryDealsTotal()-1; deal>=0; deal--) {
   ulong deal_ticket=HistoryDealGetTicket(deal);

   if(deal_ticket>0) {
      ENUM_DEAL_TYPE deal_type=(ENUM_DEAL_TYPE)HistoryDealGetInteger(deal_ticket,DEAL_TYPE);
      if(deal_type==DEAL_TYPE_BUY && !buy_found) {
         last_price_buy=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
         buy_found=true;
      }
      if(deal_type==DEAL_TYPE_SELL && !sell_found) {
         last_price_sell=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
         sell_found=true;
      }
   }
}
Note you might also want to check the proper deal direction, IN or OUT, do this with: HistoryDealGetInteger(deal_ticket,DEAL_ENTRY)==DEAL_ENTRY_IN
 

Thank you!

 
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.

It is difficult, but my idea is to write your own temporary file to keep informations you want. You can also save a file in CSV format to view it on Excell.

Reason: