Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Check last closed trade (profit/type/symbol etc.) - MetaTrader 5 Expert - MT4/MT5 Resource

author EAcpu | 3 reads | 0 comments |

With this code block you can get the last closed trade data without using a loop.

 // variables
String day start = "00:00" ; // day start time
Double last close_profit; // Last close trade profit
String trading symbol, transaction type; // Expert initialization--------------------
Integer initialization ()
  {
Return ( initialization successful );
  } // Expert de-initialization------------------
Blank solution initialization ( constant integer reason)
  {

  } //ExpertOnTick-----------------------------------------
Blank check ()
  { // Check for the last closed transaction.  CheckLastClose();

  } //+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Blank check last closed()
  {
Date time history time = string to time (start of day); // History record from "start of day to current time" if ( history selection (history time, time current ()))
    {
Integer total = historical transaction total ();

// Get the last transaction number and select it for further work. Oolong tickets = historical transaction acquisition tickets (all - 1 );

// Get what you need to get.      Last closing profit = normalized double ( historical transactions received double (ticket, transaction profit ), 2 );
      TradeSymbol = HistoryOrderGetString (ticket, orderSymbol );

// Identify sell transactions. if ( Historical transactions get integer (ticket, transaction type ) == DEAL_TYPE_BUY )
        {
        Transaction type = "Sell Trade" ;
        }

// Identify buy transactions if ( Historical transactions get integer (ticket, transaction type ) == transaction type_sell )
        {
        Transaction Type = "Buy Trade" ;
        }

// Chart output. comments ( "\n" , "Total Transactions - : " , all,
"\n" , "Last Coupon-: " , Tickets,
"\n" , "Last closing profit-: " , LastClosed_Profit,
"\n" , "The last transaction was -:" , trade type);

    }
  } //+------------------------------------------------------------------+

//+------------------------------------------------------------------+

You can get the entire trading history (from the very beginning of the account) by using HistorySelect(); running this way.

 // Get the entire history
History selection ( 0 , time current ());



Attachment download

📎 last_closed_trade.mq5 (2 KB)

Source: MQL5 #49374

Verification code Refresh