MQL5 - How to get LastEntryPrice and BarsSinceLastEntry?

johnnybegoode

Trader
Jul 19, 2016
53
0
22
43
MQL5 - How to get LastEntryPrice and BarsSinceLastEntry?

i.e.
I had opened sell and buy positions for EUR/USD
How do I get

1) LastLongEntryPrice
2) LastShortEntryPrice
3) BarsSinceLastLongEntry
4) BarsSinceLastShortEntry

Thank you!
 

Enivid

Administrator
Staff member
Nov 30, 2008
13,216
553
144
Odessa
www.earnforex.com
You can browse the deals using HistorySelect(), HistoryDealsTotal(), HistoryDealGetTicket(), and HistoryDealGetDouble() functions - the built-in reference in MQL5 Editor has great examples for these functions.

To calculate the number of bars since the entry, you just get the deal's time and use the extended Bars() function call to find the number of bars that has passed since.
 

johnnybegoode

Trader
Jul 19, 2016
53
0
22
43
This code could possibly get the LastEntryPrice but it does not differentiate between DealType (Buy/Sell).
The tricky part is that the LastShortEntryPrice / LastLongEntryPrice may not be the last deal.

i.e.
I opened 3 sell orders and then 5 buy orders after.
LastShortEntryPrice is 6th deals ago and LastLongEntryPrice is 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"?

MQL5:
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);
  }
 

johnnybegoode

Trader
Jul 19, 2016
53
0
22
43
I get "undeclared identifier" error
when I tried to compare
if (last_price_buy >= ask_Symbol_1[0])
Print last_price_buy return 0.0
even with open positions


MQL5:
double last_price_sell=0, last_price_buy=0;
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;
      }
   }
}
Print("@@@@@@@@@@@@@@@@@@@last_price_buy: ",last_price_buy);
//************
 

johnnybegoode

Trader
Jul 19, 2016
53
0
22
43
With
HistorySelect(0,TimeCurrent());
Print last_price_buy return values :)

However, I am still unable to compare

if (last_price_buy >= 0)
"undeclared identifier" error
Screen Shot 2019-04-10 at 2.15.07 AM.png