MQL5 - How to get LastEntryPrice and BarsSinceLastEntry?

Discussion in 'MetaTrader 5' started by johnnybegoode, Apr 4, 2019.

  1. johnnybegoode

    johnnybegoode Trader

    53
    0
    22
    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!
     
  2. Enivid

    Enivid Administrator Staff Member

    11,002
    404
    114
    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.
     
  3. johnnybegoode

    johnnybegoode Trader

    53
    0
    22
    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"?

    Code (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);
      }
     
  4. Enivid

    Enivid Administrator Staff Member

    11,002
    404
    114
    You can get deal type by calling HistoryDealGetInteger() for DEAL_TYPE property and then comparing it to DEAL_TYPE_BUY, DEAL_TYPE_SELL, or any other deal type you want to filter.
     
  5. johnnybegoode

    johnnybegoode Trader

    53
    0
    22
    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


    Code (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);
    //************    
     
  6. Enivid

    Enivid Administrator Staff Member

    11,002
    404
    114
    You forget to call HistorySelect() before doing anything with history at all.
     
  7. johnnybegoode

    johnnybegoode Trader

    53
    0
    22
    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
     
  8. Enivid

    Enivid Administrator Staff Member

    11,002
    404
    114
    Perhaps, you are trying to use it outside this variable's scope? For example, you declare it inside one function and then try to use inside a different function?
     
Tags:

Share This Page