getting Last Closed Order in MQL5 - page 2

 
Amirfakhredin Ghanbari :


I propose a solution through 'OnTradeTransaction': as soon as the transaction 'TRADE_TRANSACTION_DEAL_ADD' is caught -> we turn to the trading history 'HistoryDealSelect' -> if this is the deal 'DEAL_TYPE_BUY' or 'DEAL_TYPE_SELL' -> print out the data:

//+------------------------------------------------------------------+
//|                                                    Last Deal.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.118
*/
#include <Trade\DealInfo.mqh>
//---
CDealInfo      m_deal;                       // object of CDealInfo class
//+------------------------------------------------------------------+
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
         return;
      if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
        {
         //---
         string deal_type=(m_deal.DealType()==DEAL_TYPE_BUY)?"buy":"sell";

         string deal_direction;
         switch(m_deal.Entry())
           {
            case  DEAL_ENTRY_IN:
               deal_direction="in";
               break;
            case  DEAL_ENTRY_OUT:
               deal_direction="in";
               break;
            default:
               deal_direction="-/-";
               break;
           }

         long digits=5;
         if(SymbolSelect(m_deal.Symbol(),true))
            digits=SymbolInfoInteger(m_deal.Symbol(),SYMBOL_DIGITS);

         string headline="| Time               | Symbol    | Deal      | Order     | Type      | Direction | Volume    | Price     |";
         string line=StringFormat("|%-19s | %-9s | %-9d | %-9d | %-9s | %-9.2s | %-9.2f | %-9."+IntegerToString(digits)+"f |",
                                  TimeToString(m_deal.Time(),TIME_DATE|TIME_SECONDS),
                                  m_deal.Symbol(),
                                  m_deal.Ticket(),
                                  m_deal.Order(),
                                  deal_type,
                                  deal_direction,
                                  m_deal.Volume(),
                                  m_deal.Price()
                                 );
         Print(headline+"\n"+line);
        }
     }
  }
//+------------------------------------------------------------------+


Result:

and


Files:
Last_Deal.mq5  8 kb
 
Amirfakhredin Ghanbari:

seems i cant make this work all i get is this error below!


'HistoryDealGetInteger' - no one of the overloads can be applied to the function call


Can you try whether the following code works for you?

void playWithHistory(){
   HistorySelect(0,TimeCurrent());
   int TotalDeals = HistoryDealsTotal();
   datetime LatestDeal = 0;
   ulong LatestTicket = 0;
   for(int i = 0; i < TotalDeals;i++){
      ulong Ticket = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(Ticket,DEAL_ENTRY) == DEAL_ENTRY_OUT){
         datetime DealTime = HistoryDealGetInteger(Ticket,DEAL_TIME);
         if(DealTime > LatestDeal){
            LatestDeal = DealTime;
            LatestTicket = Ticket;
         }
      }
   }
   if(LatestTicket > 0){
      double LatestProfit = HistoryDealGetDouble(LatestTicket, DEAL_PROFIT);
      Alert(LatestTicket," Profit of latest deal: ",DoubleToString(LatestProfit,2));
   }
   else Alert("No ticket found.");
}//end function


This code is not optimised, just thrown together as experiment. I think it works for me.

 
Vladimir Karputov #:

I propose a solution through 'OnTradeTransaction': as soon as the transaction 'TRADE_TRANSACTION_DEAL_ADD' is caught -> we turn to the trading history 'HistoryDealSelect' -> if this is the deal 'DEAL_TYPE_BUY' or 'DEAL_TYPE_SELL' -> print out the data:


I just wanted to say that I tried your code and it works perfectly, thank you Vladimir = ).


Regards,



 

I came looking for the answer, how to find the profit of the last operation.

Since I couldn't find it, I wrote a function for it.

mtype can be DEAL_TYPE_BUY, DEAL_TYPE_SELL, -99(For All Types)

symbol can be Symbol() for the current symbol or ALL for all Symbols

magicx can be the magic number or -1 for any number

double profit = lastTrade( -99, "ALL", 12345 );

double lastTrade(int mtype, string symbol, int magicx){
   ulong    ticket=0;
   int cta=0;
   HistorySelect(0,TimeCurrent());   
   for(int i=HistoryOrdersTotal(); i>=0; i--){
      if((ticket=HistoryDealGetTicket(i))>0  ){
         if( HistoryDealGetInteger(ticket,DEAL_ENTRY) == DEAL_ENTRY_OUT ){
            string s= HistoryDealGetString(ticket,DEAL_SYMBOL);
            if( s==symbol || symbol=="ALL" ){
               long type  =HistoryDealGetInteger(ticket,DEAL_TYPE);
               if( type==mtype || mtype==-99){
                  long m  =HistoryDealGetInteger(ticket, DEAL_MAGIC);
                  if( m==magicx || magicx==-1 ){
                     double profit=HistoryDealGetDouble(ticket, DEAL_PROFIT);
                     return profit;
                     //Print("Profit(",ticket,")TP=",type," :", profit);
                  }
               }
            }
         }
         
      }      
   }
   return 0;
}
Reason: