getting Last Closed Order in MQL5

 

hi guys


i just want to know whats the ticket of last closed order i wrote this just want to know if this way is correct or not

and one more thing why tickets are different  in terminal than actual reports or in mql5!


tnX for help in advance

void OnStart()
  {

      HistorySelect(0,TimeCurrent());
      ulong Ticket = HistoryOrderGetTicket( HistoryOrdersTotal()-1 );
      string mySymb = HistoryOrderGetString( ticket,ORDER_SYMBOL )
      Print("Symbol:", mySymb ," Ticket:",Ticket);
  }
2020.05.12 11:40:58     597378726       AUDNZD  buy     0.1     1.06596 1.06120 1.06834 2020.05.13 05:02:18     1.06834 0.00    -0.15   14.38
2020.05.12 11:46:08     597384231       USDCAD  sell    0.1     1.40156 1.40592 1.39785 2020.05.12 13:26:01     1.39785 0.00    0.00    26.54
2020.05.12 13:31:30     597493760       AUDNZD  sell    0.1     1.06685 1.06846 1.06503 2020.05.12 22:00:34     1.06503 0.00    0.00    11.10
2020.05.12 13:32:08     597494585       USDJPY  sell    0.1     107.453 107.753 107.207 2020.05.12 22:32:36     107.207 0.00    0.00    22.95
Files:
last_order.png  34 kb
 
Amirfakhredin Ghanbari:

hi guys


i just want to know whats the ticket of last closed order i wrote this just want to know if this way is correct or not

and one more thing why tickets are different  in terminal than actual reports or in mql5!


tnX for help in advance

Your code looks at order ticket, you screenshot shows a position ticket. That's not the same.

A "last closed order" is not a thing with MT5.

Read the documentation and/or this article.

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • www.mql5.com
The ultimate goal of a trader is to extract profits through the means of trading operations on the financial markets. This article describes the terms and processes of the MetaTrader 5 trading platform, the knowledge of which is necessary for a proper understanding of the work of trade functions of the MQL5 language. Orders — are the trade...
 
Alain Verleyen:

Your code looks at order ticket, you screenshot shows a position ticket. That's not the same.

A "last closed order" is not a thing with MT5.

Read the documentation and/or this article.

tnX Alain,

so how can i get information of last closed order in MT5"MQL5"!?

it was easy in MT4, im kinda confused

 
Amirfakhredin Ghanbari:

tnX Alain,

so how can i get information of last closed order in MT5"MQL5"!?

it was easy in MT4, im kinda confused

I think that this is a confusion about the correct words. In MT/MQL there is no such thing as a "closed order". You submit an "order". If this order gets executed it results in a "deal". And you end up by having a "position". Then you can place another order, resulting in other deal, and another position. If your trading account is a netting account the second deal will be added/subtracted from your position. So your existing position gets modified in size. If you have a hedging account the second trade will result in a second position, even if it is on the same symbol.


Please read about the specifics of the words on this page: https://www.mql5.com/en/docs/trading

Documentation on MQL5: Trade Functions
Documentation on MQL5: Trade Functions
  • www.mql5.com
An order is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels.A deal is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is...
 
WindmillMQL:

I think that this is a confusion about the correct words. In MT/MQL there is no such thing as a "closed order". You submit an "order". If this order gets executed it results in a "deal". And you end up by having a "position". Then you can place another order, resulting in other deal, and another position. If your trading account is a netting account the second deal will be added/subtracted from your position. So your existing position gets modified in size. If you have a hedging account the second trade will result in a second position, even if it is on the same symbol.


Please read about the specifics of the words on this page: https://www.mql5.com/en/docs/trading

tnX WindMilMQL


the too many namings...

i tried with HistoryDeal still couldn't get what exactly what i needed,

so if i wanted to get exactly last finished Deal in history like my screenshot how to get it?

Last Deal information

 
Amirfakhredin Ghanbari:

tnX WindMilMQL


the too many namings...

i tried with HistoryDeal still couldn't get what exactly what i needed,

so if i wanted to get exactly last finished Deal in history like my screenshot how to get it?


Am I correct that your screenshot is from the History tab in MT? If so, you can get the information about this by using the following code:

ulong LatestTicket = HistoryDealGetTicket(HistoryDealsTotal()-1);
double Profit = HistoryDealGetDouble(LatestTicket,DEAL_PROFIT);

In this example am I using the deal with the highest index number. However, it might be that the deals are not indexed in the way you want. In that case you have to loop through all deals and look for the deal that matches your search criteria (e.g. symbol or date&time of execution). And then get the parameter of that particular deal.

 
WindmillMQL:

Am I correct that your screenshot is from the History tab in MT? If so, you can get the information about this by using the following code:

In this example am I using the deal with the highest index number. However, it might be that the deals are not indexed in the way you want. In that case you have to loop through all deals and look for the deal that matches your search criteria (e.g. symbol or date&time of execution). And then get the parameter of that particular deal.

Thanks WindMilMQL,


yup as you mentioned above i test with this function, Problem is it return latest Deal by the Time the deal opened then closed

not the latest on closed Deal

in screen shot instead of returning :

2020.05.12 11.40.58 audnzd Buy with close on 2020.05.13 05.02.18

it returns

2020.05.12 13.32.08 usdjpy sell closed on 2020.05.12 22.32.36 since its the last deal actually created!

thats my dilemma!

i want to find latest closed by the close time not by the created time of the deal

Latest Deal

 
Amirfakhredin Ghanbari:

Thanks WindMilMQL,


yup as you mentioned above i test with this function, Problem is it return latest Deal by the Time the deal opened then closed

not the latest on closed Deal

in screen shot instead of returning :

2020.05.12 11.40.58 audnzd Buy with close on 2020.05.13 05.02.18

it returns

2020.05.12 13.32.08 usdjpy sell closed on 2020.05.12 22.32.36 since its the last deal actually created!

thats my dilemma!

i want to find latest closed by the close time not by the created time of the deal


I see what you mean. The history is indexed by the time of opening, not by the time of closing. You are looking for a solution to how to get the closing time and then find the most recent one.

I have never tried this myself, but on the documentation page I see an enum called ENUM_DEAL_ENTRY, where you can select between "in" and "out". I don't know whether this can be used to find the timestamp that you are looking for.

See near the bottom of this documentation page:  https://www.mql5.com/en/docs/constants/tradingconstants/dealproperties

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal...
 
WindmillMQL:

I see what you mean. The history is indexed by the time of opening, not by the time of closing. You are looking for a solution to how to get the closing time and then find the most recent one.

I have never tried this myself, but on the documentation page I see an enum called ENUM_DEAL_ENTRY, where you can select between "in" and "out". I don't know whether this can be used to find the timestamp that you are looking for.

See near the bottom of this documentation page:  https://www.mql5.com/en/docs/constants/tradingconstants/dealproperties

yup you are correct,

by default it returns data on Opening Time not actual recent closed...

Enum deal entry let me try oh well its sunday i guess have to wait till tomorrow!


tnX again for help

 

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


 

That's a parameter mismatch please check the inputs.

1. Immediately returns the property value.

long  HistoryDealGetInteger(
   ulong                       ticket_number,     // Ticket
   ENUM_DEAL_PROPERTY_INTEGER  property_id        // Property identifier
   );

2. Returns true or false, depending on the success of the function. If successful, the value of the property is placed into a target variable passed by reference by the last parameter.

bool  HistoryDealGetInteger(
   ulong                       ticket_number,     // Ticket
   ENUM_DEAL_PROPERTY_INTEGER  property_id,       // Property identifier
   long&                       long_var           // Here we accept the property value
   );

I think you can use a loop of simple time > comparison to get the desired results.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal...
Reason: