Obtain the result of the last closing position in mql5

 
Hello to all
I need to get the result of the last closed position in mql5
If you have a function for this ، Please send it code in topic
Thank
 
sinayas :
Hello to all
I need to get the result of the last closed position in mql5
If you have a function for this ، Please send it code in topic
Thank

An example of how to catch the DEAL_ENTRY_OUT DEAL and print all the DEALS that were in the POSITION:

Forum on trading, automated trading systems and testing trading strategies

issue on getting open price using HistoryOrderGetDouble(trans.position,ORDER_PRICE_OPEN)

Vladimir Karputov, 2020.11.03 20:04

Code:

Price of position opening.mq5

//+------------------------------------------------------------------+
//|                                    Price of position opening.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.143
*/
#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)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
      if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
        {
         if(m_deal.Entry()==DEAL_ENTRY_OUT)
           {
            long position_id=m_deal.PositionId();
            if(HistorySelectByPosition(position_id))
              {
               uint     total = HistoryDealsTotal();
               ulong    ticket= 0;
               //--- for all deals
               for(uint i=0; i<total; i++)
                 {
                  //--- try to get deals ticket
                  if((ticket=HistoryDealGetTicket(i))>0)
                    {
                     //--- get deals properties
                     long     lng_time=HistoryDealGetInteger(ticket,DEAL_TIME);
                     datetime dat_time=(datetime)lng_time;
                     double   price    = HistoryDealGetDouble(ticket,DEAL_PRICE);
                     Print(dat_time,", ",DoubleToString(price,8));
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

 
Vladimir Karputov:

An example of how to catch the DEAL_ENTRY_OUT DEAL and print all the DEALS that were in the POSITION:


thank you .

Please advise how to use this code in Expert.

I need to check the status of the last position within a condition and make a decision based on the answer.

Thank you for your help.

I am very inexperienced in coding

 
sinayas:

Please advise how to use this code in Expert.

I am very inexperienced in coding
  1. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  2. or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019.08.21)

 
sinayas :

thank you .

Please advise how to use this code in Expert.

I need to check the status of the last position within a condition and make a decision based on the answer.

Thank you for your help.

I am very inexperienced in coding

I gave you an example. You must learn and use the information from the example. You should at least show your diligence and show your MQL5 code. No code, no diligence == no help!

 

I have same/similar problem.

I'm trying to use HistoryOrderGetDouble to find CLOSED position's Sl/TP!

but it returns ZERO! 

 
Jabir Ismaeili # :

I have same/similar problem.

I'm trying to use HistoryOrderGetDouble to find CLOSED position's Sl/TP!

but it returns ZERO! 

Run the History Deals and Orders code - examine the resulting file. After studying the file, you will understand the relationship between position, deal and order.

History Deals and Orders
History Deals and Orders
  • www.mql5.com
The 'History Deals and Orders' script shows absolutely all properties of deals and orders from the trading history over a given time interval.
 

Example: a 'SELL' position was opened, Stop Loss and Take Profit were immediately indicated upon opening, the magic number of the advisor that opened the position was '200'.

Take Profit worked, result:

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1131968999          |1148686673          |2021.10.08 13:15:01 |1633698901101       |DEAL_TYPE_SELL      |DEAL_ENTRY_IN       |200                 |DEAL_REASON_EXPERT  |1148686673         
|Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.01                |1.45008             |0.00                |0.00                |0.00                |EURCAD              |                                         |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1148686673          |2021.10.08 13:15:01 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2021.10.08 13:15:01 |1633698901101       |1633698901101       |ORDER_FILLING_FOK  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |200                 |ORDER_REASON_EXPERT |1148686673          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.01                |0.00                |1.45008             |1.45052             |1.44975             |1.45008             |0.00000            
|Symbol              |Comment                                  |External id        
|EURCAD              |                                         |                   

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1132007606          |1148724285          |2021.10.08 13:47:58 |1633700878733       |DEAL_TYPE_BUY       |DEAL_ENTRY_OUT      |200                 |DEAL_REASON_TP      |1148686673         
|Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.01                |1.44975             |0.00                |0.00                |0.26                |EURCAD              |[tp 1.44975]                             |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1148724285          |2021.10.08 13:47:58 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2021.10.08 13:47:58 |1633700878732       |1633700878733       |ORDER_FILLING_IOC  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |200                 |ORDER_REASON_TP     |1148686673          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.01                |0.00                |1.44975             |0.00000             |0.00000             |1.44975             |0.00000            
|Symbol              |Comment                                  |External id        
|EURCAD              |[tp 1.44975]                             |                   
 
Vladimir Karputov #:

Example: a 'SELL' position was opened, Stop Loss and Take Profit were immediately indicated upon opening, the magic number of the advisor that opened the position was '200'.

Take Profit worked, result:

hummm

Thanks your reply!

I did know that there are TWO deals for each trade (closed).

Now i found out that ALSO there are two orders for each closed Trade!!! 

so wired! 

Thanks again. 

Reason: